linux+nginx+php set up high-performance Web server _linux

Source: Internet
Author: User
Tags diff fpm ldap mcrypt php web server

Nginx ("Engine X") is a high-performance HTTP and reverse proxy server and a IMAP/POP3/SMTP proxy server. Nginx was developed by Igor Sysoev, the second rambler.ru site for Russian traffic, which has run over 2.5 at the site. Igor releases the source code in the form of a BSD-like license. Although still beta, Nginx has been known for its stability, rich feature sets, sample configuration files, and low system resource consumption.

This article is w3cgroup in the successful erection of nginx+php Web server after a summary, successfully set Nginx open multiple domain name multi-site, and will not appear 502 Bad Gateway Error!

This test environment is as follows:

System environment

Linux version 2.6.9 (CentOS release 4.7 (Final))

Software Environment

Nginx-0.6.35.tar.gz
Current stable version: Nginx 0.6.35 (January 26, 2009)
Official wiki:http://wiki.nginx.org
Official Download: http://sysoev.ru/nginx/nginx-0.6.35.tar.gz

Php-5.2.9.tar.gz
Official Download: Http://www.php.net/get/php-5.2.9.tar.gz/from/this/mirror

Php-5.2.9-fpm-0.5.10.diff.gz
Official Download: http://php-fpm.anight.org/downloads/archive/

Mysql-5.1.34.tar.gz

Libiconv-1.13.tar.gz

Libmcrypt-2.5.8.tar.gz

Mcrypt-2.6.8.tar.gz

Mhash-0.9.9.9.tar.gz

nginx0.6.35+php5.2.9 all related installation packages download Http://www.w3cgroup.com/code/nginx_php.rar


Installation steps

The support library required to compile and install PHP5.2.9

Tar zxvf libiconv-1.13.tar.gz
CD libiconv-1.13/
./configure--prefix=/usr/local
Make
Make install
Cd.. /

Tar zxvf libmcrypt-2.5.8.tar.gz
CD libmcrypt-2.5.8/
./configure
Make
Make install
/sbin/ldconfig
CD libltdl/
./configure--enable-ltdl-install
Make
Make install
Cd.. /.. /

Tar zxvf mhash-0.9.9.9.tar.gz
CD mhash-0.9.9.9/
./configure
Make
Make install
Cd.. /

Ln-s/usr/local/lib/libmcrypt.la/usr/lib/libmcrypt.la
Ln-s/usr/local/lib/libmcrypt.so/usr/lib/libmcrypt.so
Ln-s/usr/local/lib/libmcrypt.so.4/usr/lib/libmcrypt.so.4
Ln-s/usr/local/lib/libmcrypt.so.4.4.8/usr/lib/libmcrypt.so.4.4.8
Ln-s/USR/LOCAL/LIB/LIBMHASH.A/USR/LIB/LIBMHASH.A
Ln-s/usr/local/lib/libmhash.la/usr/lib/libmhash.la
Ln-s/usr/local/lib/libmhash.so/usr/lib/libmhash.so
Ln-s/usr/local/lib/libmhash.so.2/usr/lib/libmhash.so.2
Ln-s/usr/local/lib/libmhash.so.2.0.1/usr/lib/libmhash.so.2.0.1

Tar zxvf mcrypt-2.6.8.tar.gz
CD mcrypt-2.6.8/
/sbin/ldconfig
./configure
Make
Make install
Cd.. /


Ii. compiling and installing the MySQL 5.1.34 extension library

/usr/sbin/groupadd MySQL
/usr/sbin/useradd-g MySQL MySQL
Tar zxvf mysql-5.1.34.tar.gz
CD 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--enable-local-infile --with-plugins=innobase
Make && make install
chmod +w/usr/local/webserver/mysql
Chown-r Mysql:mysql/usr/local/webserver/mysql
Cd.. /

MySQL installation is omitted here


Iii. Compiling and installing PHP (fastcgi mode)

Tar zxvf php-5.2.9.tar.gz
gzip-cd php-5.2.9-fpm-0.5.10.diff.gz | patch-d php-5.2.9-p1
CD 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-- 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-sockets--with-ldap--WITH-LDAP-SASL
make zend_extra_libs= '-liconv '
make install
CP php.ini-dist/ Usr/local/webserver/php/etc/php.ini
CD ... /


Iv. Modify the PHP-FPM configuration file

Rm-f/usr/local/webserver/php/etc/php-fpm.conf
Vi/usr/local/webserver/php/etc/php-fpm.conf

Enter the following content:

<?xml version= "1.0"?>
<configuration>
<section name= "Global_options" >
<value name= "Pid_file" >/usr/local/webserver/php/logs/php-fpm.pid</value>
<value name= "Error_log" >/usr/local/webserver/php/logs/php-fpm.log</value>
<value name= "Log_level" >notice</value>
<value name= "Emergency_restart_threshold" >10</value>
<value name= "Emergency_restart_interval" >1m</value>
<value name= "Process_control_timeout" >5s</value>
<value name= "Daemonize" >yes</value>
</section>
<workers>
<section name= "Pool" >
<value name= "Name" >default</value>
<value name= "Listen_address" >127.0.0.1:9000</value>
<value name= "Listen_options" >
<value name= "Backlog" >-1</value>
<value name= "owner" ></value>
<value name= "group" ></value>
<value name= "mode" >0666</value>
</value>
<value name= "Php_defines" >
<value name= "Sendmail_path" >/usr/sbin/sendmail-t-i</value>
<value name= "Display_errors" >1</value>
</value>
<value name= "User" >www</value>
<value name= "group" >www</value>
<value name= "PM" >
<value name= "Style" >static</value>
<value name= "Max_children" >128</value>
<value name= "Apache_like" >
<value name= "Startservers" >20</value>
<value name= "Minspareservers" >5</value>
<value name= "Maxspareservers" >35</value>
</value>
</value>
<value name= "Request_terminate_timeout" >0s</value>
<value name= "Request_slowlog_timeout" >0s</value>
<value name= "Slowlog" >logs/slow.log</value>
<value name= "Rlimit_files" >51200</value>
<value name= "Rlimit_core" >0</value>
<value name= "Chroot" ></value>
<value name= "ChDir" ></value>
<value name= "Catch_workers_output" >yes</value>
<value name= "Max_requests" >500</value>
<value name= "Allowed_clients" >127.0.0.1</value>
<value name= "Environment" >
<value name= "HOSTNAME" > $HOSTNAME </value>
<value name= "PATH" >/usr/local/bin:/usr/bin:/bin</value>
<value name= "TMP" >/tmp</value>
<value name= "Tmpdir" >/tmp</value>
<value name= "TEMP" >/tmp</value>
<value name= "OSType" > $OSTYPE </value>
<value name= "Machtype" > $MACHTYPE </value>
<value name= "Malloc_check_" >2</value>
</value>
</section>
</workers>
</configuration>

V. Create WWW user group and WWW user

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


Vi. Compiling and installing Nginx

Tar zxvf nginx-0.6.35.tar.gz
CD nginx-0.6.35/
Default installation:
./configure
Make && make install
By default, Nginx will be installed in/usr/local/nginx. By setting the compilation options, you can change this setting.

Specify compilation option installation, such as:
./configure--user=www--group=www--prefix=/usr/local/webserver/nginx--with-http_stub_status_module--with-http_ Ssl_module
Make && make install
Cd.. /

Vii. Creating fcgi.conf Files

Rm-f/usr/local/webserver/nginx/conf/fcgi.conf
Vi/usr/local/webserver/nginx/conf/fcgi.conf

Enter the following content:

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 is built with--enable-force-cgi-redirect
Fastcgi_param Redirect_status 200;


Viii. Edit Nginx configuration file

Rm-f/usr/local/webserver/nginx/conf/nginx.conf
Vi/usr/local/webserver/nginx/conf/nginx.conf

Enter the following content:

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;
#多个站点设置, site folders must be placed in the Nginx HTML directory, or multiple sites are not supported
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;
}
#多个站点设置, site folders must be placed in the Nginx HTML directory, or multiple sites are not supported
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 boot 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 = 30
Net.ipv4.tcp_keepalive_time = 300
Net.ipv4.tcp_syncookies = 1
Net.ipv4.tcp_tw_reuse = 1
Net.ipv4.tcp_tw_recycle = 1

To have the configuration take effect immediately:

/sbin/sysctl–p

Do here, you can reboot restart Linux, no accident should be able to open the nginx smoothly

Smooth restart Nginx

Ps aux | grep nginx
Find Nginx Master process, if: 5800, then kill
Kill–hup 5800

Smooth reboot PHP

/USR/LOCAL/WEBSERVER/PHP/SBIN/PHP-FPM restart

Other commands for PHP-FPM: Start | Stop | Quit | Restart | Reload | Logrotate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.