Nginx installation notes of contains PHP support virtual hosting reverse proxy load balancing

  • 2020-05-06 12:12:17
  • OfStack

System environment: RHEL5 [2.6.18-8.el5xen]
Software environment:
nginx-0.7.17
lighttpd-1.4.20.tar.gz
pcre-6.6-1.1
pcre-devel-6.6-1.1
php-5.1.6-5.el5
Reference download address:
http: / / sysoev ru nginx/nginx - 0.7.17. tar. gz
(the latest stable version of 0.6.32) http://www.lighttpd.net/download/lighttpd-1.4.20.tar.gz
##########################################################################
Install support software
1. Install lighttpd to extract spawn-fcgi (if the site does not contain php pages, you may not install spaw-fcgi, PHP)
shell > tar zxvf lighttpd-1.4.20.tar.gz
shell > cd lighttpd-1.4.20/
shell > ./configure && make
shell > cp -p src/spawn-fcgi /usr/sbin/spawn-fcgi
2. Install pcre and php (the following software)
RHEL5 can be installed with the rpm package, the process is short.

Install nginx
shell > tar zxvf nginx-0.7.17.tar.gz
shell > cd nginx-0.7.17/
shell > ./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module
shell > make && make install
shell > ln -sf /opt/nginx/sbin/nginx /usr/sbin/

nginx operation control
1. Check the configuration file for syntax errors shell > nginx -t
2. Start (run directly without any parameters)
shell > nginx
3. Reload nginx configuration
shell > Es103en-s HUP nginx #// or killall-1 nginx
4. Exit nginx
after processing the current request shell > Es114en-s QUIT nginx #// or killall-3 nginx

nginx configuration use case
1. Conventional configuration of
shell > vi /opt/nginx/conf/nginx.conf
worker_processes 1; #// number of worker processes
events {
use epoll; #// add this event to improve I/O performance
work_connections 4096;
}
http {
include mime.types;
default_types application/octet-stream;
sendfile on;
tcp_nodelay on
keepalive_timeout 60;
server {
listen 80; #// set the listening port, be careful not to conflict with other Web programs such as Apache,
server_name www. linux. org; #// specify the hostname
to use charset utf - 8; #// specifies the default code for the site file,
location / {
root html; #// set the website root directory
index index.html index.html;
}
error_page 500 502 503 504 /50x.html
location = /50x.html {
root html;
}
}
}
2. Add status monitoring
shell > vi/opt/nginx/conf/nginx conf
# / / add the following content location ~ ^/NginxStatus/ {
stub_status on;
access_log off;
}
shell > killall -1 nginx
#// use a browser to access http://nginx_server_ip/NginxStatus/ to see the status statistics page. (three Numbers represent: total number of connections processed, number of handshakes successfully created, and total number of requests processed.)
3. PHP language
is supported by FastCGI 1) start FastCGI service (use php-cgi to do the actual processing of php pages, spawn-fcgi to facilitate the simultaneous opening of multiple php-cgi processes -- "-C" option to control the number of child processes)
shell > /usr/sbin/spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi -C 10
2) modify/opt nginx/conf/nginx conf configuration file, add the following content:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
3) reload configuration
shell > killall -1 nginx
4. Set
for the virtual host Modify the nginx.conf file by adding an server {... Can be configured, each virtual host parameters can be configured independently.
http {
server {
listen 80;
server_name www.vhost1.com;
access_log logs/vhost1.access.log main;
location / {
index index.html;
root/var/www/vhost1; #// web root directory
for the first web host }
}
server {
listen 80;
server_name www.vhost2.com;
access_log logs/vhost2.access.log main;
location / {
index index.html;
root/var/www/vhost2; The web root of the second web host is
}
}
}
5. Load balancing
based on reverse agent Modify the nginx.conf file, add the upstream configuration, specify the IP and weight of the corresponding server group, and adjust the configuration of the web root directory in the server segment. Scatter HTTP requests to nginx servers to servers in the Web cluster for processing.
http {
upstream my_web_cluster {
server 192.168.2.11:8000 weight=3;
server 192.168.2.12:8000 weight=3;
server 192.168.2.13:8000 weight=3;
server 192.168.2.14:8000 weight=3;
server 192.168.2.15:8000 weight=3;
}
server {
listen 80;
server_name www.domain.com;
location / {
proxy_pass http://my_web_cluster;
proxy_set_header x-real-IP $remote_addr;
}
#// note: other location configuration segments (such as those for.php files) need to be commented out or the redirection of such files may be affected.
}
}


Related articles: