Nginx is specially developed for performance optimization. Its biggest advantage is its stability and low system resource consumption, as well as high processing capacity for http concurrent connections. A single physical server can support 20,000 to 50,000 concurrent requests. A large number of companies that provide services such as social networking, news information, e-commerce and virtual hosting have chosen Nginx to provide web services. At present, users of nginx websites in mainland China include Sina, Netease, Tencent, and the well-known microblog Plurk also uses nginx. .
The difference between Apache and Nginx: https://blog.51cto.com/14227204/2435423
Now start to install Nginx:
1. Preparation: **
Centos 7 system and CD
Compile and install the software package: https://pan.baidu.com/s/1-GaLSYxt4fP5R2gCVwpILA Extraction code: kph5 **
You can also download it from the official website https://nginx.org/
2. Start to build Nginx website:
Install the required dependency packages and uninstall the current httpd service (if you are sure that it is not, you can omit it):
[ root@mysql yum.repos.d]# yum -y erase httpd
[ root@mysql /]# yum -y install pcre-devel zlib-devel #Install required dependencies
Compile, install and configure to optimize 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 for easy command use
**Nginx operation control: **
1、 Check the configuration file
[ root@mysql /]# nginx -t #Check the configuration file, if OK or successful appears, it means no problem
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 options-s HUP is equivalent to-1[root@mysql /]# killall -s QUIT nginx #Close option-s QUIT is equivalent to-3
Note: The minimally installed centos 7 does not have the killall command installed by default, you can use "yum -y install psmisc"
In order to make the Nginx service start, stop, reload and other operations more convenient, you can write a Nginx service script, which is more in line with the management habits of the Centos system:
[ root@mysql /]# vim /etc/init.d/nginx
#! /bin/bash
# chkconfig:-9920
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 //Granted permission[root@mysql /]# chkconfig --add nginx //Add as system service[root@mysql /]# systemctl start nginx //Start Nginx
[ root@mysql /]# vim /usr/local/nginx/conf/nginx.conf
...................
# user nobody;//Running user
worker_processes 1;//Number of work processes
# error_log logs/error.log;//The location of the 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;//4096 connections per process}
The above optimizations are implemented based on global configuration, and the meaning of each optimization is as follows:
1、 worker_processes: Represents the number of worker processes. If the server has multiple CPUs or multi-core processors, you can refer to the total number of CPU cores to specify the number of worker 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 below 10000 (default is 1024), and is associated with the configuration item of the number of worker processes above. For example: if the number of worker processes is 8, each process processes 4096 connections, the number of connections that allow Nginx to provide services normally has exceeded 30,000 (4096*8=32768). Of course, it 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:
The Nginx configuration file uses the "http {}" mark to set the HTTP server, including access log, http port, web page directory, default character set, connection retention, virtual web host, php parsing and other global website settings. Part of it is contained in the sub-definition tag "server {}". "Server {}" represents a specific website setting.
[ root@mysql /]# vim /usr/local/nginx/conf/nginx.conf
................... Omit part of the content
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '//Remove 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;//Enable efficient file transfer mode
# tcp_nopush on;
# keepalive_timeout 0;
keepalive_timeout 65;//Connection keep timeout
# gzip on;
server {//Web service monitoring configuration
listen 80;//Listening address and port
server_name www.test1.com;//Site name FQDN
charset koi8-r;//The default character set of the web page
access_log logs/host.access.log main;
location /{//Root directory configuration
root html;//The location of the website root directory, relative to the installation directory
index index.html index.php;//Default homepage, index page}
error_page 500502503504/50x.html;//Internal error feedback page
location =/50x.html {//Error page configuration
root html;}
The above configuration is just to build a website service. If you want to run more than one, 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. To avoid errors, it is necessary to copy 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 configuration of the virtual host is completed at this point, and then restart the service to make the configuration effective, DNS is configured by itself, please refer to the blog post: **https://blog.51cto.com/14227204/2384462
[ root@mysql /]# nginx -t #Before restarting the service, it is best to check
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 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 file
[ root@mysql named]# echo www.test2.com >/var/www/test2/index.html
Client starts to verify:
View current status information:
Test another domain name:
Recommended Posts