Nginx (“engine x”) is a high performance HTTP and reverse proxyserver and a
IMAP/POP3/SMTP proxy server. Nginx is the second most visited in Russia by Igor Sysoev
Rambler.ru site, where it has been running for more than two and a half years. Igor releases the source code under a BSD-like license.
1. Why choose Nginx
1. In the case of high concurrent connection, Nginx is a good substitute for Apache server, which can support up to 50,000
In response to the number of concurrent connections, Nginx chose epoll and kqueue as the development model.
2. Nginx as a load balancingserver: Nginx can directly support Rails and PHP internally
The program performs external services, and can also support external services as an HTTP proxy server.
Nginx is written in C, and it is much better than Perlbal in terms of system resource overhead and CPU usage efficiency.
3. As a mail proxy server: Nginx is also a very good mail proxy server.
Why is the performance of Nginx much higher than that of Apache? This is due to the fact that Nginx uses the latest epoll (Linux
2.6 kernel) and kqueue (freebsd) network I/O model, while Apache uses the traditional select model. At present, Linux can withstand high concurrent access
Both Squid and Memcached use the epoll network I/O model.
2. Nginx installation and configuration
System requirements: Linux 2.6+ kernel, the Linux operating system in this article is CentOS 5.2+
1. Obtain relevant open source programs: (for CentOS operating system) use CentOS
The yum command that comes with the Linux system installs and upgrades the required program library.
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel
libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2
bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs
e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl
openssl-devel openldap openldap-devel nss_ldap openldap-clients
openldap-servers
2. Install PHP 5.2.10 (FastCGI mode)
Compile and install the support libraries required for PHP 5.2.10:
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 ../
3. Compile and install MySQL 5.1.38
/usr/sbin/groupadd mysql // create mysql group
/usr/sbin/useradd -g mysql mysql // Create a mysql user and add it to the mysql group
tar zxvf mysql-5.1.38.tar.gz
cd mysql-5.1.38/
./configure prefix=/usr/local/webserver/mysql/
enable-thread-safe-client
make && make install
cp /usr/local/src/mysql/support-files/my-medium.cnf
/etc/my.cnf is in
There are 4 template files in the support-files directory, we choose one of the Mysql configuration files to overwrite /etc/my.cnf (the system default configuration, which sets Performance parameters and some path parameters of Mysql);
cd /usr/local/mysql //Enter the mysql directory
/usr/local/webserver/mysql/bin/mysql_install_db
basedir=/usr/local/webserver/mysql
datadir=/usr/local/webserver/mysql/data user=mysql
// Initialize the table and specify to use the mysql user to access. After initializing the table, start setting access permissions for mysql and root users;
chown -R root . //Set root to be able to access /usr/local/mysql;
chown -R mysql data //Set mystcgi_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-redirect
fastcgi_param REDIRECT_STATUS 200;
5. Start Nginx
ulimit -SHn 65535
/usr/local/webserver/nginx/sbin/nginx
4. Configuration Automatically start Nginx + PHP at startup
vi /etc/rc.local
Add the following at the end:
ulimit -SHn 65535
/usr/local/webserver/php/sbin/php-fpm start
/usr/local/webserver/nginx/sbin/nginx
6. Change Nginx configuration smoothly without stopping Nginx service
1. After modifying the /usr/local/webserver/nginx/conf/nginx.confconfiguration file, please execute the following command to check whether the configuration file is correct:
/usr/local/webserver/nginx/sbin/nginx -t
If the following two lines of information are displayed on the screen, the configuration file is correct:
the configuration file
/usr/local/webserver/nginx/conf/nginx.conf syntax is ok
the configuration file
/usr/local/webserver/nginx/conf/nginx.conf was tested
successfully
2. At this time, enter the following command to view the main process number of Nginx:
ps -ef | grep “nginx: master process” | grep -v “grep” | awk
-F ‘ ‘ ‘{print $2}’
The screen displays the Nginx main process number, for example:
4913
At this time, execute the following command to make the modified Nginx configuration file take effect:
kill -HUP 4913
Or without such trouble, find the Pid file of Nginx:
kill -HUP `cat /usr/local/webserver/nginx/nginx.pid`
7. Write a script that regularly cuts Nginx logs every day
1. Create the script /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
Enter the following:
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path=”/usr/local/webserver/nginx/logs/”
mkdir -p ${logs_path}$(date -d “yesterday” + “%Y”)/$(date -d
“yesterday” + “%m”)/
mv ${logs_path}access.log ${logs_path}$(date -d “yesterday”
+”%Y”)/$(date -d “yesterday” +”%m”)/access_$(date -d “yesterday”
+”%Y%m%d”).log
kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`
2. Set crontab to cut nginx access log every morning at 00:00
crontab -e
Enter the following:
00 00 * * * /bin/bash
/usr/local/webserver/nginx/sbin/cut_nginx_log.sh