In this article I will share my own experience building a web server running CentOS 5.3.
What was required:
- Completely get rid of Apache. The server had to withstand a good load, distributing static and dynamic.
- We needed support for the latest version of libxml, the slightest difference in the versions made the site completely inoperative.
- Needed a gzip
- And some other features that I will talk about in the article itself.
Prehistory
As a “testing ground” I had a physical server onboard, which had 2 Xeon processors and 2 GB of RAM on board. I can’t give a more precise config - I don’t remember, and I didn’t particularly care.
The site required php 5.2.x. When I installed from the standard repository, I ran into a problem like libxml. By default, it was installed with version 2.6.24, which made the project completely inoperative (version 2.6.32 was required). Even with the
CentALT repository
connected, the problem was not solved.
')
It was probably possible to rebuild the finished package and install it already, but I have no experience with this, so I decided to build php manually.
It was of course not possible to correctly assemble php from the first time :). The project required taking into account a small number of subtleties. In particular, it supports pdo and freetype.
Initially, I decided to do everything on the basis of the nginx + apache bundle, the installation and configuration process was no different from what is written in
this article . And the process itself seemed easier to me than setting up work with php-cgi.
Everything worked perfectly, but I decided not to dwell on this, especially since the server had to be optimized. Then the idea came to remove completely apache, after weighing all this on nginx and php-cgi.
PHP build
I had to manage something with the FastCGI processes myself, I decided to use php-fpm - PHP FastCGI Process Manager is a patch for PHP that allows you to more efficiently manage FastCGI processes to ensure high stability. Why precisely he? It seemed easy to set up and generally reviews are not bad.
It is said - done, pulling the latest version from
http://php-fpm.org/downloads/ . At that time I had php version 5.2.10, so I pulled
php-5.2.10-fpm-0.5.13.diff.gz . It would seem, then everything is simple. Shrink, and do everything exactly as indicated in
the project wiki . I will briefly summarize what needs to be done:
$ bzip2 -cd php-5.2.10.tar.bz2 | tar xf -
$ gzip -cd php-5.2.10-fpm-0.5.13.diff.gz | patch -d php-5.2.10 -p1
$ cd php-5.2.10 && ./configure --enable-fastcgi --enable-fpm
As you can see, nothing tricky. Next - I collected php. The assembly process was based on the subtleties that I needed:
./configure --enable-fastcgi --enable-fpm --with-zlib --enable-pdo --with-pdo-mysql --with-mysql --with-config-file-path=/etc --enable-calendar --with-iconv --enable-exif --enable-ftp --enable-wddx --with-zlib --with-bz2 --with-gettext --with-xmlrpc --with-xml --enable-soap --enable-filepro --enable-bcmath --enable-trans-sid --enable-mbstring --enable-dbx --enable-dba --with-openssl --with-mhash --with-mcrypt --with-xsl --with-curl --with-pcre-regex --with-gd --enable-gd-native-ttf --with-ldap --enable-pdo --with-pdo-mysql --with-mysql --with-sqlite --with-pdo-sqlite --enable-sqlite-utf8 --with-pear --with-freetype-dir=/usr --with-jpeg-dir=/usr
Bold marked the required parameters that are required for php-fpm. --with-config-file-path = / etc - I decided to clearly indicate where to keep php-ini.
Well, and then:
make install all
You also need to edit the /etc/php-fpm.conf file. In particular, I edited:
Pid file <value name="pid_file">/var/run/php-fpm.pid</value> Error log file <value name="error_log">/var/log/php-fpm.log</value>
In the first case we set where the process pid-file will be located, in the second where to write logs.
<value name="listen_address">127.0.0.1:9000</value>
Address and port where will listen to php. This is what we will use when configuring nginx. Or, the path to the socket is indicated here - for example:
<value name = "listen_address"> / tmp / php-fpm.sock </ value>
And you need to specify on whose behalf to start the process itself:
<code> Unix user of processes
<value name = "user"> </ value>
Unix group of processes
<value name = "group"> </ value>
This is up to you, as long as the rights are appropriate.
You can even dig a config, each line is provided with a comment, it's easy to figure out.
Nginx
As I said above, installing and installing nginx is almost the same as what is written in the article, the link to which I indicated above. The only point is that we don’t use proxying, since we don’t have apache, so the lines of proxy_ * do not matter to us. Well, the very specifics of the settings, for example worker_connections or worker_processes, etc. depends specifically on your needs. Of course, I collected by hand, because here it was necessary to take into account some subtleties. Without them, it would be quite enough:
yum install nginx
I will give an example of a part of the config from the http branch:
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$gzip_ratio"'
'"$got" "$sent_http_set_coockie"';
access_log /var/log/nginx/access.log main;
'"$ got" "$ sent_http_set_coockie"'; - this is what I need for logging with the custom-nginx-session-module module installed (Module for detecting cookies and setting up a unique session). I can tell you about the installation and configuration process later if it is interesting to someone. And the one above is the usual logging.
The use of virtual hosts was also implied, so for convenience I decided to put it into separate files:
include /etc/nginx/sites-enabled/*;
I will give an example - the file /etc/nginx/sites-enabled/example.com:
server {
listen 80;
server_name example.com www.example.com;
# ,
access_log /var/log/examplecom-access.log;
error_log /var/log/example.com-error.log;
#
root /path/to/www;
# nginx
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|bmp)$ {
root /path/to/www;
}
UPD: Note from AlexeyK
habrauzer :
the lines do not make sense if you have the same root, and indeed nginx will give these files by default, if you don’t hang a handler on them :) it makes sense to put expires more in this location and it is possible to adjust the size of buffers for feedback (if there are a lot of static files)
# ? :)
location ~ /.svn/ {
deny all;
}
# index.php,
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
# , .php fastCGI (php-fpm)
location ~ \.php$ {
# , php-fpm.conf
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /path/to/www$fastcgi_script_name;
include fastcgi_params;
}
}
UPD: Or, as the habrauser
darivush suggested :
fastcgi_pass 127.0.0.1:9000;
replace with
fastcgi_pass unix:/tmp/fastcgi_sock;
, since Unix sockets are preferable to 127.0.0.1. Well, and accordingly the path / tmp / fastcgi_sock should be correct, configured in /etc/php-fpm.conf in the section
<value name = "listen_address">
Of course, this is an example of a standard minimum config, everything else fits \ removes \ edits depending on the tasks.
It seems to have forgotten nothing. Run php-fpm and nginx:
/etc/init.d/php-fpm start
/etc/init.d/nginx start
And then I ran into a problem - I did not patch php as it should, as a result, php-fpm completely refused to start, swearing at the wrong parameter when running php-cgi. The solution to this problem, I could not find at that time. Everything was decided some time later, when php version 5.2.11 was released, which I patched with the same patch, configured and installed as described above. After that, everything started and worked without problems.
Since I needed to build php manually (due to all the subtleties I wrote about above), I did not try to solve the problem by installing everything I needed from the standard repositories. Perhaps this works and the installation process becomes easier.
Thanks for attention. I posted the article in my blog, because in no case do I claim to invent the bicycle in terms of the task at hand. This is more an auxiliary manual for me, which may be needed if necessary. Well, and it will be quite enough for beginners. But, if the article seems useful to you, I will gladly transfer it to the
nginx blog. The plans are to talk about custom-nginx-session-module configuration (I will later tell why I need it), as well as memcached, and
pinba .
And yes, suggestions, comments and valuable advice are welcome.
UPD: moved to nginx.