1, Source installation
Step 1: unzip the nginx source package to the / opt directory, customize the configuration, and then compile and install
[root@server opt]# tar xzfv nginx-1.12.2.tar.gz [root@server opt]# cd nginx-1.12.2/ [root@server nginx-1.12.2]#./configure \ --prefix=/usr/local/nginx \ <-----Specify the installation directory --user=nginx \ <-----Specify administrative user name --group=nginx \ <-----Specify the management group name --with-http_stub_status_module <---This module must be added to traffic statistics [root@server nginx-1.12.2]#make && make install
Step 2: add an nginx user to the nginx program, and soft link the nginx command to the system environment variable for easy use and management
[root@server nginx-1.12.2]# useradd -M -s /sbin/nologin nginx [root@server nginx-1.12.2]# [root@server nginx-1.12.2]# [root@server nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
Step 3: write system CTL startup script
[root@server conf]# vim /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile =/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/bin/kill -S HUP $MAINPID ExecStop=/usr/bin/kill -S QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
Step 4: modify the permission and others cannot start it
[root@server conf]# chmod 754 /lib/systemd/system/nginx.service
Step 5: yum install elink package, which can test nginx web pages directly on linux
[root@server nginx-1.12.2]# yum -y install elinks Plug in loaded: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Resolving dependencies --> Checking transactions ---> software package elinks.x86_64.0.0.12-0.37.pre6.el7.0.1 Will be installed
2, Flow statistics
Step 1: modify the nginx configuration file and add the configuration of the traffic statistics module in the server configuration block
vim /usr/local/nginx/conf/nginx.conf server { listen 192.168.245.120:80; server_name www.cloud.com:80; charset utf-8; access_log logs/www.cloud.com.access.log; location / { root /var/www/html/cloud; index index.html index.htm; } location ~/status { stub_status on; <----Turn on flow statistics access_log off; <----Close log }
Step 2: restart nginx service
[root@server conf]# systemctl restart nginx.service
Step 3: verify on the client
The access address should be followed by/status: http://192.168.245.120/status
3, Virtual host
1. Based on different domain names
Step 1: add a new server configuration block in nginx configuration file
[root@server conf]# vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.yjs.com; <---Specify domain name charset utf-8; access_log logs/www.yjs.com.access.log; <---Specify the access log when accessing the domain name location / { root /var/www/html/yjs; <---Specify home page site index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } server { listen 80; server_name www.cloud.com; <---Specify domain name charset utf-8; access_log logs/www.cloud.com.access.log; <---Specify the access log when accessing the domain name location / { root /var/www/html/cloud; <---Specify home page site index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
Step 2: create the home page sites of two domain names and create their own home pages
[root@server conf]# mkdir -p /var/www/html/yjs [root@server conf]# mkdir -p /var/www/html/cloud [root@server conf]# [root@server conf]# [root@server conf]# echo "this is yjs" > /var/www/html/yjs/index.html [root@server conf]# echo "this is cloud" > /var/www/html/cloud/index.html
Step 3: modify the DNS zone configuration file
[root@server conf]# vim /etc/named.rfc1912.zones zone "cloud.com" IN { <---Specify domain name type master; file "cloud.com.zone"; <---Specify area data file allow-update { none; }; }; zone "yjs.com" IN { <---Specify domain name type master; file "yjs.com.zone"; <---Specify area data file allow-update { none; }; };
Step 4: configure the area data file. Both domain names point to the address 192.168.245.120
[root@server conf]# cd /var/named [root@server named]# ls cloud.com.zone data dynamic named.ca named.empty named.localhost named.loopback slaves [root@server named]# cp -p cloud.com.zone yjs.com.zone [root@server conf]# vim /var/named/cloud.com.zone $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1 www IN A 192.168.245.120
Step 5: restart the domain name service
[root@server named]# systemctl restart named
Step 6: check the syntax of nginx configuration file and start nginx
[root@server conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@server conf]# [root@server conf]# [root@server conf]# systemctl restart nginx.service
Step 7: visit different domain names on the client and different pages will appear
2. Based on different ports
Step 1: modify the nginx configuration file and modify the server configuration block
[root@server conf]# vim /usr/local/nginx/conf/nginx.conf server { listen 192.168.245.120:80; server_name www.cloud.com; charset utf-8; access_log logs/www.cloud.com.access.log; location / { root /var/www/html/cloud; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 192.168.245.120:8080; <---The listening address is the same, and the port is set to 8080 server_name www.cloud.com:8080; <---You should also add 8080 to the domain name you visit charset utf-8; access_log logs/www.cloud8080.com.access.log; <---Specify access log location / { root /var/www/html/cloud8080; <---Specify site index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
Step 2: create a web site and web content accessing port 8080
[root@server conf]# mkdir -p /var/www/html/cloud8080 [root@server conf]# [root@server conf]# [root@server conf]# echo "this is cloud 8080" > /var/www/html/cloud8080/index.html
Step 3: check nginx syntax and restart the service
[root@server conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@server conf]# [root@server conf]# systemctl restart nginx.service
Step 4: verify that another page is available when you visit port 8080 on the client
3. Different domain names based on different ip addresses
That is, different ip addresses point to different domain names and different web pages
Step 1: create a new area data file. The ip of the domain name is 192.168.245.130
[root@server ~]# vim /var/named/yjs.com.zone $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1 www IN A 192.168.245.130
Step 2: restart DNS Service
[root@server ~]# systemctl restart named
Step 3: successful parsing on the client
Step 4: modify nginx configuration file
[root@server ~]# vim /usr/local/nginx/conf/nginx.conf server { listen 192.168.245.130:80; <---appoint ip server_name www.yjs.com:80; <---Specify domain name charset utf-8; access_log logs/www.yjs.com.access.log; <---Specify access log location / { root /var/www/html/yjs; <---Specify site directory index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 192.168.245.120:80; <---appoint ip server_name www.cloud.com:80; <---Specify domain name charset utf-8; access_log logs/www.cloud.com.access.log; <---Specify access log location / { root /var/www/html/cloud; <---Specify site directory index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
Step 5: enter their respective sites and write their own home page content
[root@server conf]# vim /var/www/html/yjs/index.html [root@server conf]# echo "this is yjs web" > /var/www/html/yjs/index.html [root@server conf]# [root@server conf]# [root@server conf]# vim /var/www/html/cloud/index.html [root@server conf]# echo "this is cloud web" > /var/www/html/cloud/index.html
Step 6: check nginx syntax and restart the service
[root@server ~]# cd /usr/local/nginx/conf/ [root@server conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@server ~]# systemctl restart nginx.service
Step 7: access authentication on the client