The nginx + Apache structure to be introduced in this article is actually nginx as the frontend, Apache as the backend, and give full play to their respective advantages. Nginx has outstanding performance in high concurrency, a powerful proxy function, and a small amount of system resources. Apache processes queues in high concurrency better than FastCGI (nginx needs to run php through FastCGI and other methods, mod_php is more stable and efficient than PHP-CGI when processing dynamic PHP pages.
That is to say, our goal is to use nginx to receive client requests. If it is a dynamic page request, it will be handed over to Apache for processing, and then sent back to the client through nginx. Other requests, nginx processes the results and returns the results to the client ,. Of course, you can make nginx only use the proxy function. All requests are handled by Apache and tomcat. This article uses the former.
However, in this article, we implement the simple structure of adding an Apache to an nginx server. in actual application, it is possible that the front end is a proxy server composed of one or more nginx servers, while the back end is a cluster composed of Multiple Apache or other Web servers, coupled with a variety of third-party software.
Prerequisites: assume that the default main site name is www.litvip.com and the website root directory is/var/www/litvip.
Step 1: install and configure nginx. After installation, the configuration files are in the/etc/nginx directory. The main configuration file/etc/nginx. conf is as follows:
[Root @ test ~] # Yum-y install nginx [root @ test ~] # Vi/etc/nginx. confuser nginx; worker_processes 2; error_log/var/log/nginx/error. log; PID/var/run/nginx. PID; events {use epoll; worker_connections 2048;} HTTP {include/etc/nginx/mime. types; Include/etc/nginx/proxy. conf; Include/etc/nginx/gzip. conf; default_type application/octet-stream; index index.html index.htm index. PHP; log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request" ''$ Status $ response "$ http_referer" ''" $ http_user_agent "" $ http_x_forwarded_for "'; sendfile on; tcp_nopush on; server_tokens off; keepalive_timeout 60; timeout 128; server {Listen 80; SERVER_NAME www.litvip.com; root/var/www/litvip; location ~ *. *\. (GIF | JPG | JPEG | PNG | BMP | ICO | CSS | JS | TXT | FLV | SWF | mid | Doc | PPT | XLS | PDF | TXT | MP3 | WMA) $ {expires 2D ;}# comment out the above # location if you need to place the content cached by the client, the media, images, and other files in a fixed directory, use the following method # location ~ ^/(Images | JavaScript | JS | CSS | flash | media | static)/{# expires 2D ;#} location ~. * \. (PhP? | CGI | PL | Py) $ {proxy_pass http: // 127.0.0.1: 8888;} location ~ /\. Ht {deny all;} location ~ /. + \. (INC | conf | CNF) {deny all;} access_log/var/log/nginx/litvip-access.log main; # access_log off} # Load config files from the/etc/nginx/CONF. d directoryinclude/etc/nginx/CONF. d /*. conf;} [root @ test ~] # Vi/etc/nginx/proxy. export off; Specify vary; proxy_set_header accept-encoding ''; proxy_set_header host $ host; proxy_set_header X-real-IP $ remote_addr; proxy_set_header X-forwarded-for $ hour; limit 10 m; client_body_buffer_size 128 K; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4 K; proxy_buffers 32 4 K; proxy _ Busy_buffers_size 64 K; [root @ test ~] # Vi/etc/nginx/gzip. confgzip on; gzip_http_version 1.0; gzip_disable "MSIE [1-6] \. "; gzip_disable" Mozilla/4 "; gzip_comp_level 3; gzip_proxied any; gzip_vary on; gzip_buffers 4 16 K; gzip_min_length 1100; gzip_types text/plain text/XML text/CSS application/XML application/XHTML + XML application/RSS + XML application/atom_xml application/JavaScript Application/X-JavaScript;
To make nginx. conf concise, I put some common settings in the same specialized file and load them in the main configuration file nginx. conf. If you use virtualhost to operate multiple sites, you can configure different configuration files based on the needs of different sites and load them in their respective server domains.
Step 2: install and configure Apache. The configuration file is in the/etc/httpd/directory. Modify the main configuration file/etc/httpd/CONF/httpd. conf: the configuration file is too long. I only need to change the configuration file:
[Root @ test ~] # Yum-y install httpd [root @ test ~] # Vi/etc/httpd/CONF/httpd. change conflisten 80 connector to listen 127.0.0.1: 8888 change the DocumentRoot "var/www/html" variable to DocumentRoot "/var/www/litvip" <directory "/var/www/html"> variable to <directory "/var/WWW /litvip "> options indexes followsymlinks indexes are changed to the following, CGI and SSI options should be allowed to execcgi followsymlinks AllowOverride none should be changed to the following. htaccess AllowOverride all order allow, deny allow from all </directory> serversignature on login is changed to the following, the server and Apache version serversignature off # addhandler CGI-script will not be displayed on the actual error page. the CGI plugin is changed to the following, and the CGI script is used. CGI ,. pl ,. pyaddhandler CGI-script. CGI. pl. PY
Step 3: Set virtualhost (if you need to operate multiple sites on the local machine, otherwise skip this step ):
1. Modify/etc/httpd/CONF/httpd. conf:
# Change namevirtualhost *: 80 hosts to the following namevirtualhost 127.0.0.1: 8888 # append the following four lines. www.litvip.com is our default main site <virtualhost 127.0.0.1: 8888> servername www.litvip.com </virtualhost> include virtual /*. conf
2. Modify/etc/nginx. conf and append the following sentence to the server {} domain:
Include/etc/nginx/virtual/*. conf;
3. Suppose we want to run a site named www.cooliter.com on the local machine:
[Root @ test ~] # Mkdir/etc/nginx/virtual/[root @ test ~] # Mkdir/etc/httpd/virtual/[root @ test ~] # Vi/etc/nginx/virtual/www.cooliter.com. confserver {Listen 80; SERVER_NAME www.cooliter.com; root/var/www/cooliter; location ~ *. *\. (GIF | JPG | JPEG | PNG | BMP | ICO | CSS | JS | TXT | FLV | SWF | mid | Doc | PPT | XLS | PDF | TXT | MP3 | WMA) $ {expires 24 h;} location ~ . * \. (PhP? | CGI | PL | Py) $ {proxy_pass http: // 127.0.0.1: 8888;} location ~ /\. Ht {deny all;} location ~ /. + \. (INC | conf | CNF) {deny all;} access_log/var/log/nginx/cooliter-access.log main; # access_log off} [root @ test ~] # Vi/etc/httpd/virtual/www.cooliter.com. conf <virtualhost 127.0.0.1: 8888> serveradmin webmaster@cooliter.com DocumentRoot/var/www/cooliter servername www.cooliter.com uninstall off errorlog logs/your-error_log customlog logs/www.cooliter.com-access_log common </virtualhost>
Step 4: Install mysql. Refer to this tutorial.
Step 5: install PHP and other common components:
[Root @ test ~] # Yum-y install PHP-mysql PHP-Gd PHP-xml php-mbstring PHP-mcrypt
Step 6: Install the PHP accelerator eaccelerator and PHP-PECL-memcached modules to improve performance:
[Root @ test ~] # Yum install PHP-eaccelerator PHP-PECL-memcached
The configuration files are/etc/PHP. d/eaccelerator. ini and/etc/PHP. d/memcached. ini. If you use the default settings, you do not need to modify the configuration file.
Step 7: install and configure the PHP acceleration and decryption software provided by Zend, which is not in the yum source and can be directly downloaded from the official website without compilation, when downloading, select a 32-bit or 64-bit version based on your system.
1: If the PHP version you are using is php5.3.x, you need to install Zend guard Loader:
[Root @ test ~] # Tar-xzvf ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz [root @ test ~] # Ls ZendGuardLoader-php-5.3-linux-glibc23-i386/readme.txt php-5.3.xsmall tips, see the instructions for use in readme.txt under the decompression directory [root @ test ~] # Mv-T ZendGuardLoader-php-5.3-linux-glibc23-i386/usr/lib/PHP/ZendGuardLoader-php-5.3 [root @ test ~] # Vi/etc/PHP. d/Zend. INI # Add the following content, save and exit [Zend guard loader] zend_extension = "/usr/lib/PHP/ZendGuardLoader-php-5.3/php-5.3.x/zendguardloader. so "zend_loader.enable = 1zend_loader.disable_licensing = 0zend_loader.obfuscation_level_support = 3
2: If the PHP version you are using is php5.2 or earlier, you need to install Zend optimizer:
[Root @ test ~] # Tar-xzvf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz [root @ test ~] # Ls ZendOptimizer-3.3.9-linux-glibc23-i386EULA-ZendOptimizer inventory. xml license readme-zendoptimizer data MD5 # Tips 1, see the extract directory readme-zendoptimizer instructions for use [root @ test ~] # Ls ZendOptimizer-3.3.9-linux-glibc23-i386/data4_2_0_comp 4_3_x_comp 5_0_x_comp rj4_4_x_comp 5_rjx_comp poweredbyoptimizer.gif # Tips 2. extract the data directory under the directory to provide different so files according to different PHP versions, assume that our version is [root @ test ~] of php5.1.x. # Mv-T ZendOptimizer-3.3.9-linux-glibc23-i386/usr/lib/PHP/ZendOptimizer-3.3.9 [root @ test ~] # Vi/etc/PHP. d/Zend. INI # Add the following content, save and exit [Zend optimizer] zend_optimizer.optimization_level = 1zend_extension = "/usr/lib/PHP/ZendOptimizer-3.3.9/data/5_1_x_comp/zendoptimizer. so"
Step 8: start Apache and nginx and test (MySQL is not tested ):
[Root @ test ~] #/Etc/init. d/httpd start [root @ test ~] #/Etc/init. d/nginx start [root @ test ~] # Chkconfig httpd on [root @ test ~] # Chkconfig nginx on [root @ test ~] # Mkdir/var/www/litvip [root @ test ~] # Echo "Hello, litvip">/var/www/litvip/index.html [root @ test ~] # Echo-e "<? PHP \ nphpinfo (); ">/var/www/litvip/index. php
Open the browser into index.html and index. php, disable Apache, and access Apache separately (the PHP page cannot be accessed ).
Test whether Gzip is valid. Because we set gzip_min_length to 1100, the page below 1100bytes will not be compressed. It is recommended that the content of the page you test be greater than 1100 bytes.
[Root @ test ~] # Curl-I-h "Accept-encoding: gzip, deflate" http://www.cooliter.com/test.txtHTTP/1.1 200 okserver: nginxdate: Fri, 22 Jul 2011 04:45:30 gmtcontent-type: text/plainlast-modified: Fri, 22 Jul 2011 04:40:30 gmtconnection: Keep-alivevary: Accept-encodingexpires: Sun, 24 Jul 2011 04:45:30 gmtcache-control: Max-age = 172800content-encoding: gzip <--- prove that the server supports gzip Compression
Later, this article aims to introduce the construction method of this structure, so there are few configuration parameters used. For more detailed parameter settings and optimization, you need to use the official help documentation for further testing based on your actual situation.