How Linux+Nginx+Php Build a High Performance WEB Server

11-03-2023

Nginx ("engine x ") is a high-performance http and reverse proxy server, and also an imap/pop3/smtp proxy server. Nginx was developed by igor sysoev for rambler.ru, the second most visited site in Russia. It has been running on this site for more than two and a half years. Igor publishes the source code as a bsd-like license. Although it is still in beta, nginx is already known for its stability, rich feature set, sample configuration files and low system resource consumption.

Installation Step 1: Compile and install the supporting libraries required for php5.2.9.

tar zxvf libiconv-1.13.tar.gzcd libiconv-1.13/./configure --prefix=/usr/localmakemake installcd ../

tar zxvf libmcrypt-2.5.8.tar.gzcd libmcrypt-2.5.8/./configuremakemake install/sbin/ldconfigcd libltdl/./configure --enable-ltdl-installmakemake installcd ../../

tar zxvf mhash-0.9.9.9.tar.gzcd mhash-0.9.9.9/./configuremakemake installcd ../

ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.laln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.soln -s /usr/local/lib/libmcrypt.so.4 / usr/lib/libmcrypt.so.4ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.aln -s / usr/local/lib/libmhash.la /usr/lib/libmhash.laln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.soln -s /usr/local/lib/libmhash.so.2 /usr/lib/ libmhash.so.2ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

tar zxvf mcrypt-2.6.8.tar.gzcd mcrypt-2.6.8//sbin/ldconfig./configuremakemake installcd ../

Second, compile and install mysql 5.1.34 extension library.

/usr/sbin/groupadd mysql/usr/sbin/useradd -g mysql mysqltar zxvf mysql-5.1.34.tar.gzcd mysql-5.1.34/./configure --prefix=/usr/local/webserver/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --ena ble-local-infile --with-plugins=innobasemake && make installchmod +w /usr/local/webserver/mysqlchown -r mysql:mysql /usr/local/webserver/mysqlcd ../

Mysql installation is omitted here.

Third, compile and install php(fastcgi mode)

tar zxvf php-5.2.9.tar.gzgzip -cd php-5.2.9-fpm-0.5.10.diff.gz | patch -d php-5.2.9 -p1cd php-5.2.9/./configure --prefix=/usr/local/webserver/ php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/mysql/bin/ mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/ usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd -- enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-saslmake zend_extra_libs='-liconv'make install cp php.ini-dist /usr/local/webserver/php/etc/php.inicd ../

Fourth, modify the php-fpm configuration file

rm -f /usr/local/webserver/php/etc/php-fpm.confvi /usr/local/webserver/php/etc/php-fpm.conf

Enter the following:

/usr/local/webserver/php/logs/php-fpm.pid/usr/local/webserver/php/logs/php-fpm.lognotice101m5syesdefault127.0.0.1:9000-10666/usr/sbin/ sendmail -t -i1wwwwwwstatic128205350s0slogs/slow.log512000yes500127.0.0.1$hostname/usr/local/bin:/usr/bin:/bin/tmp/tmp/tmp$ostype$machtype2

V. Creating www User Groups and www Users

/usr/sbin/groupadd www/usr/sbin/useradd -g www www

Sixth, compile and install nginx

Tarzxvf nginx-0.6.35.tar.gzcd nginx-0.6.35/Default installation:. /configuremake && make install By default, nginx will be installed in /usr/local/nginx. You can change this setting by setting the compilation option.

Specify compilation options for installation, such as:./configure-user = www-group = www-prefix =/usr/local/webserver/nginx-with-http _ stub _ status _ module-with-http _ SSL _ modulemake & & makeinstallcd ../

Seven, create fcgi.conf file.

rm -f /usr/local/webserver/nginx/conf/fcgi.confvi /usr/local/webserver/nginx/conf/fcgi.conf

Enter the following:

fastcgi_param gateway_interface cgi/1.1; fastcgi_param server_software nginx;

fastcgi_param query_string $query_string; fastcgi_param request_method $request_method; fastcgi_param content_type $content_type; fastcgi_param content_length $content_length;

fastcgi_param script_filename $document_root$fastcgi_script_name; fastcgi_param script_name $fastcgi_script_name; fastcgi_param request_uri $request_uri; fastcgi_param document_uri $document_uri; fastcgi_param document_root $document_root; fastcgi_param server_protocol $server_protocol;

fastcgi_param remote_addr $remote_addr; fastcgi_param remote_port $remote_port; fastcgi_param server_addr $server_addr; fastcgi_param server_port $server_port; fastcgi_param server_name $server_name;

# php only, required if php was built with --enable-force-cgi-redirectfastcgi_param redirect_status 200;

Eight, edit nginx configuration file

rm -f /usr/local/webserver/nginx/conf/nginx.confvi /usr/local/webserver/nginx/conf/nginx.conf

Enter the following:

user www www; worker_processes 8; error_log /usr/local/webserver/nginx/logs/logs/nginx_error.log crit; pid /usr/local/webserver/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; events {use epoll; worker_connections 51200; }

http {include mime.types; default_type application/octet-stream; charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on;

fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 8 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k;

gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on;

#limit_zone crawler $binary_remote_addr 10m; # Multiple site settings, the site folder must be placed in the html directory of nginx, otherwise multi-site server {listen 80; server_name www.v-ec.com; root /usr/local/webserver/nginx/html/www.v-ec.com; index index.php index.html ;

location ~ .*.(php|php5)? $ {#fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; }location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {expires 30d; }

location ~ .*.(js|css)? $ {expires 6h; }

log_format veclog '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" $http_x_forwarded_for'; access_log logs/vec.log veclog; } # Multiple site settings, the site folder must be placed in the html directory of nginx, otherwise multi-site server {listen 80; server_name www.w3cgroup.com; root /usr/local/webserver/nginx/html/www.w3cgroup.com; index index.php index.html ;

location ~ .*.(php|php5)? $ {#fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; }location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {expires 30d; }

location ~ .*.(js|css)? $ {expires 6h; }

log_format w3cclog '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" $http_x_forwarded_for'; access_log logs/w3c.log w3clog; }}

Nine, configure the boot to automatically start nginx+php

vi /etc/rc.local

Add the following at the end:

ulimit -shn 51200/usr/local/webserver/php/sbin/php-fpm start/usr/local/webserver/nginx/sbin/nginx

X. Optimizing linux kernel parameters

vi /etc/sysctl.conf

Add the following at the end:

net.ipv4.tcp_fin_timeout = 30net.ipv4.tcp_keepalive_time = 300net.ipv4.tcp_syncookies = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle = 1

Make the configuration effective immediately:

/sbin/sysctl –p

At this point, you can reboot linux, and you should be able to open nginx smoothly without any accident.

Restart nginx Psaux smoothly | grepnginx Find the master process of Nginx, if it is 5800, then kill kill–hup5800.

Restart PHP/usr/local/webserver/PHP/sbin/PHP-fpmrestart smoothly.

Other commands of php-fpm: start | stop | quit | restart | reload | logrotate.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us