Ext: Installation and configuration of Nginx+apache environment

Source: Internet
Author: User
Tags zend

Ext.: http://www.server110.com/nginx/201404/8817.html

We still use Yum as much as we can to install the software we need, the software that is officially maintained by the system, its security and stability are trustworthy, and easy to manage, upgrade is convenient, but in CentOS and Rhel official Yum source for the temporary without nginx and other packages, So we need to use Epel's Yum source. Epel is what, Epel yum source How to add, click here to view.

This article will introduce the NGINX+APACHE structure, in fact, is nginx to do the front end, Apache do the back end, give full play to their respective advantages. Nginx for high concurrency performance, proxy function high efficiency, occupy less system resources, and Apache in high concurrency queue processing than fastcgi (Nginx need to run PHP through FASTCGI, etc.) better, and in the process of dynamic PHP page, mod_ PHP is also more stable and more efficient than php-cgi.

In other words, our purpose is to receive the client's request by Nginx, if it is a dynamic page request, it is given to Apache processing, and then return to the client via Nginx, the rest of the request is handled by Nginx itself, and then return the results to the client. Of course, you can let nginx only do proxy function, all the requests are given to apache,tomcat and other processing, this article uses the former.

However, in this article, we are implemented in a server in a Nginx plus an Apache simple structure, in the actual application, may be the front end is a proxy server consisting of one or more nginx, the backend is more than one Apache or other Web server, Combined with a variety of third-party software to form a cluster.

Prerequisite: Suppose our system default Primary site name is www.server110.com, the site root directory is/var/www/server110

The first step, install and configure Nginx, after installation, the configuration files are in the/etc/nginx directory, the master settings file/etc/nginx/nginx.conf:

[[email protected] ~]# yum-y install Nginx
[Email protected] ~]# vi/etc/nginx/nginx.conf
User 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 $body _bytes_sent ' $http _referer '
' "$http _user_agent" "$http _x_forwarded_for";
Sendfile on;
Tcp_nopush on;
Server_tokens off;
Keepalive_timeout 60;
Server_names_hash_bucket_size 128;
server {
Listen 80;
server_name www.server110.com;
root/var/www/server110;
Location ~*. *\. (gif|jpg|jpeg|png|bmp|ico|css|js|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma) $ {
Expires 2d;
}
# If you need client-side cache content and media, pictures and other files pinned to some directory, put the above
# location commented out, in the following way
# 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/server110-access.log main;
#access_log off
}
# Load config files from the/etc/nginx/conf.d directory
include/etc/nginx/conf.d/*.conf;
}
[Email protected] ~]# vi/etc/nginx/proxy.conf
Proxy_redirect off;
Proxy_hide_header 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 $proxy _add_x_forwarded_for;
Client_max_body_size 10m;
Client_body_buffer_size 128k;
Proxy_connect_timeout 90;
Proxy_send_timeout 90;
Proxy_read_timeout 90;
Proxy_buffer_size 4k;
Proxy_buffers 4k;
Proxy_busy_buffers_size 64k;
[Email protected] ~]# vi/etc/nginx/gzip.conf
gzip 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 16k;
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 simple, I put some of the relevant common settings in the same dedicated file, and then load the main configuration file nginx.conf. If you use VirtualHost to operate multiple sites, you can configure different settings files according to the needs of different sites, and then load them separately in their respective server domains.

Second step, install and configure Apache, config file under/etc/httpd/, we modify the master profile/etc/httpd/conf/httpd.conf, config file is too long, I only write where I need to change:

[Email protected] ~]# yum-y install httpd
[Email protected] ~]# vi/etc/httpd/conf/httpd.conf
Listen 80
↓ Change to
Listen 127.0.0.1:8888
DocumentRoot "Var/www/html"
↓ Change to
DocumentRoot "/var/www/server110"
<directory "/var/www/html" >
↓ Change to
<directory "/var/www/server110" >
Options Indexes FollowSymLinks
↓ change to the following, allowing Cgi,ssi
Options includes execcgi followsymlinks
AllowOverride None
↓ change to the following, support. htaccess
AllowOverride All
Order Allow,deny
Allow from all
</Directory>
Serversignature on
↓ changed to the following, the server and Apache versions are not displayed on the actual error page
Serversignature OFF
#AddHandler Cgi-script. CGI
↓ changed to the following, CGI script used. cgi,.pl,.py
AddHandler cgi-script. cgi. pl. py

Step three, set up VirtualHost (skip this step if you need to operate multiple sites on this machine):
1. Modify/etc/httpd/conf/httpd.conf:

#NameVirtualHost *:80
↓ Change to the following
Namevirtualhost 127.0.0.1:8888
#追加下面4行, www.server110.com is our default master
<virtualhost 127.0.0.1:8888>
ServerName www.server110.com
</VirtualHost>
Include virtual/*.conf

2. Modify/etc/nginx/nginx.conf, add the following sentence after the server{} field:

include/etc/nginx/virtual/*.conf;

3. Suppose we also run a site on this machine called www.cooliter.com:

[Email protected] ~]# mkdir/etc/nginx/virtual/
[Email protected] ~]# mkdir/etc/httpd/virtual/
[Email protected] ~]# vi/etc/nginx/virtual/www.cooliter.com.conf
server {
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 24h;
}
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
}
[Email protected] ~]# vi/etc/httpd/virtual/www.cooliter.com.conf
<virtualhost 127.0.0.1:8888>
ServerAdmin [email protected]
Documentroot/var/www/cooliter
ServerName www.cooliter.com
Usecanonicalname OFF
Errorlog Logs/www.cooliter.com-error_log
Customlog Logs/www.cooliter.com-access_log Common
</VirtualHost>

Fourth step, install MySQL, refer to this tutorial.

Fifth step, install PHP and other common components:

[Email protected] ~]# yum-y install php php-mysql php-gd php-xml php-mbstring php-mcrypt

Sixth step, install the PHP accelerator eaccelerator and php-pecl-memcached modules to improve performance:

[email protected] ~]# Yum install php-eaccelerator php-pecl-memcached

The settings files are/etc/php.d/eaccelerator.ini and/etc/php.d/memcached.ini, and if you use the default settings, you do not need to modify the settings.

Seventh step, install and configure Zend provides PHP acceleration, decryption software, yum Source does not, directly from the official download, do not need to compile, download the time to pay attention to your system according to the choice of 32-bit or 64-bit version.
1: If you are using PHP php5.3.x version, you need to install Zend Guard Loader:

[Email protected] ~]# TAR-XZVF zendguardloader-php-5.3-linux-glibc23-i386.tar.gz
[[email protected] ~]# ls zendguardloader-php-5.3-linux-glibc23-i386/
README.txt php-5.3.x
#小提示, look at the README.txt in the extract directory has instructions for use
[Email protected] ~]# mv-t zendguardloader-php-5.3-linux-glibc23-i386/usr/lib/php/zendguardloader-php-5.3
[Email protected] ~]# Vi/etc/php.d/zend.ini
#添加如下内容, save exit
[Zend Guard Loader]
zend_extension= "/usr/lib/php/zendguardloader-php-5.3/php-5.3.x/zendguardloader.so"
Zend_loader.enable=1
Zend_loader.disable_licensing=0
Zend_loader.obfuscation_level_support=3

2: If you are using PHP php5.2 or earlier, you need to install the Zend Optimizer:

[Email protected] ~]# TAR-XZVF zendoptimizer-3.3.9-linux-glibc23-i386.tar.gz
[[email protected] ~]# ls zendoptimizer-3.3.9-linux-glibc23-i386
Eula-zendoptimizer inventory.xml LICENSE Readme-zendoptimizer data MD5
#小提示1, look at the Readme-zendoptimizer in the extract directory has instructions for use
[[email protected] ~]# ls zendoptimizer-3.3.9-linux-glibc23-i386/data
4_2_0_comp 4_3_x_comp 5_0_x_comp 5_2_x_comp
4_2_x_comp 4_4_x_comp 5_1_x_comp Poweredbyoptimizer.gif
#小提示2, the extracted directory under the data directory according to different PHP version provided a different so file, assuming our version is php5.1.x
[Email protected] ~]# mv-t zendoptimizer-3.3.9-linux-glibc23-i386/usr/lib/php/zendoptimizer-3.3.9
[Email protected] ~]# Vi/etc/php.d/zend.ini
#添加如下内容, save exit
[Zend Optimizer]
Zend_optimizer.optimization_level=1
Zend_extension = "/usr/lib/php/zendoptimizer-3.3.9/data/5_1_x_comp/zendoptimizer.so"

Eighth step, launch Apache and Nginx, and test (MySQL is not tested):

[[email protected] ~]#/etc/init.d/httpd start
[[email protected] ~]#/etc/init.d/nginx start
[Email protected] ~]# chkconfig httpd on
[[email protected] ~]# chkconfig nginx on
[Email protected] ~]# mkdir/var/www/server110
[Email protected] ~]# echo "hello,server110" >/var/www/server110/index.html
[Email protected] ~]# echo-e "<?php\nphpinfo ();" >/var/www/server110/index.php

Open the browser to visit index.html and index.php respectively, and then close Apache, and then visit each (PHP page should not be accessed).

Test gzip is valid, because we set the gzip_min_length to 1100, so less than 1100bytes of the page will not be compressed, you test the content of the page is better than 1100bytes.

[Email protected] ~]# curl-i-H "accept-encoding:gzip,deflate" Http://www.cooliter.com/test.txt
http/1.1 OK
Server:nginx
Date:fri, 04:45:30 GMT
Content-type:text/plain
Last-modified:fri, 04:40:30 GMT
Connection:keep-alive
Vary:accept-encoding
Expires:sun, 04:45:30 GMT
cache-control:max-age=172800
Content-encoding:gzip <---Prove that the server supports gzip compression

Something, the purpose of this article is to introduce the construction method of this structure, so the configuration parameters used less, about more detailed parameter setting and optimization, you need to the reader yourself according to the actual situation combined with official help documents to further test.

Reprint please specify the original address: http://www.server110.com/nginx/201404/8817.html

Ext: Installation and configuration of Nginx+apache environment

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.