Nginx is specially developed for performance optimization. Its greatest advantage is its stability and low system resource consumption, as well as its high processing capacity for concurrent http connections. A single physical server can support 20,000-50,000 concurrent requests. This is why it provides a large number of services such as social networks, news information, e-commerce and virtual hosts. Companies are choosing Nginx to provide web services. At present, users of nginx websites in mainland China include Sina, Netease and Tencent. Plurk, a well-known microblog, also uses nginx.
The difference between Apache and Nginx: https://blog.51cto.com/14227204/2435423
Next, start installing Nginx:
I. Preparations:
Centos 7 System and CD
Compile and install packages: https://pan.baidu.com/s/1-GaLSYxt4fP5R2gCVwpILA
Extraction code: kph5
It can also be accessed from the official website. https://nginx.org/ Download and use
2. Start building Nginx website:
Install the required dependency packages and uninstall the existing httpd services (omitted if you are sure not):
[root@mysql yum.repos.d]# yum -y erase httpd [root@mysql /]# yum -y install pcre-devel zlib-devel # Installation of required dependency packages
Compile, install and configure optimized Nginx:
[root@mysql /]# useradd -M -s /sbin/nologin nginx # Nginx runs as nobody by default. It is recommended to create a dedicated user account [root@mysql /]# tar zxf nginx-1.12.0.tar.gz -C /usr/src/ [root@mysql /]# cd /usr/src/nginx-1.12.0/ [root@mysql nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx \ > --group=nginx --with-http_stub_status_module && make && make install [root@mysql /]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ # Create link files to facilitate command use
Nginx operation control:
1. Check configuration files
[root@mysql /]# nginx -t # Check the configuration file. If OK or success appears, it's OK. 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
2. Start and stop Nginx
[root@mysql /]# nginx # start-up [root@mysql /]# killall -s HUP nginx # Restart option - s HUP is equivalent to - 1 [root@mysql /]# killall -s QUIT nginx # Close option - s QUIT is equivalent to - 3
Note: Minimizing installation of centos 7 does not install killall command by default. You can use "yum-y install psmisc"
In order to make the start, stop and overload of Nginx service more convenient, we can write a Nginx service script, which is more in line with the management habits of Centos system:
[root@mysql /]# vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 99 20 PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "USAGE:$0 {start | stop | restart | reload}" exit 1 esac exit 0 [root@mysql /]# Chmod +x/etc/init.d/nginx//grant permissions [root@mysql /]# chkconfig --add nginx // add as system service [root@mysql /]# System CTL start nginx // start Nginx
[root@mysql /]# vim /usr/local/nginx/conf/nginx.conf ................... #Usenobody; // Running User worker_processes 1; // Number of work processes #Error_log logs/error.log;//location of error log file #error_log logs/error.log notice; #error_log logs/error.log info; #Pid logs/nginx.pid; //PID file location events { use epoll; // Use epoll model to improve performance worker_connections 4096; // Each process handles 4096 connections }
The above optimization is based on the implementation of global configuration. The implications of each optimization are as follows:
1. worker_processes: Represents the number of working processes. If the server is composed of multiple CPUs or uses multi-core processors, you can refer to the total number of CPUs to specify the number of working processes. The specific meaning is reflected in the worker_connections configuration item.
2. worker_connections: This configuration item specifies the connections processed by each process, generally less than 10,000 (default is 1024), associated with the number of working processes above. For example, if the number of working processes is 8 and each process processes 4096 connections, the number of connections that Nginx can normally provide services has exceeded. Over 30,000 (4096*8 = 32768). Of course, it also depends on the performance of physical conditions such as server hardware and network bandwidth.
Build a virtual web host based on domain name:
HTTP configuration:
Nginx configuration files use "http {}" definition tags to set up HTTP servers, including access logs, http ports, web directories, default character sets, connection retention, virtual web hosts, php parsing and other site global settings, most of which are included in the sub-definition tag "server {}". "Server {}" represents a specific site setting.
[root@mysql /]# vim /usr/local/nginx/conf/nginx.conf ................... Omit part of content http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' // Get rid of these three lines# '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; // Access log location sendfile on; // Open efficient file transfer mode #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; // Connection retention timeout #gzip on; server { // Listening configuration of web Services listen 80; // Monitor address and port server_name www.test1.com; // Website Name FQDN charset koi8-r; // Default Character Set for Web Pages access_log logs/host.access.log main; location / { // Root directory configuration root html; // Location of the website root directory relative to the installation directory index index.html index.php; // Default Home Page, Index Page } error_page 500 502 503 504 /50x.html; // Internal error feedback page location = /50x.html { // Error Page Configuration root html; }
If you want to run more than one website service, you can copy the template provided at the end of the configuration file and paste it on the "server {}" configuration, because there are too many "{}" in the configuration file. In order to avoid errors, you need to copy it to the original "server {}", as follows:
[root@mysql /]# vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.test1.com; charset utf-8; access_log logs/host.access.log main; location / { root /var/www/test1; index index.html index.php; } location /status { stub_status on; access_log off; } } server { listen 80; server_name www.test2.com; charset utf-8; access_log logs/host.access.log main; location / { root /var/www/test2; index index.html index.php; }
The virtual host is configured at this point, and then restart the service to make the configuration effective. DNS configures itself. You can refer to the blog: https://blog.51cto.com/14227204/2384462
[root@mysql /]# nginx -t # Before restarting the service, it's better to check it out 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@mysql /]# systemctl restart nginx [root@mysql named]# mkdir -p /var/www/test1 # Create the website root directory [root@mysql named]# mkdir -p /var/www/test2 [root@mysql named]# echo www.test1.com > /var/www/test1/index.html # Create test files [root@mysql named]# echo www.test2.com > /var/www/test2/index.html
Client begins validation:
View the current status information:
Test another domain name: