Centos 7 Builds Nginx Website Server and Configures Virtual Host

Keywords: Web Server Nginx yum CentOS vim

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.

Nginx is an excellent high-performance Web and reverse proxy server. It has many excellent features:

High concurrent connections: Official tests can support 50,000 concurrent connections, running to 2,~3W concurrent connections in the actual production environment.

Less memory consumption: With 3W concurrent connections, only 150M memory is consumed by the 10 NGINX processes open (15M*10=150M)

The configuration file is very simple: the style is as easy to understand as the program.

Low cost: Nginx as an open source software can be used free of charge, while the purchase of F5 BIG-IP, NetScaler and other hardware load balancing switches requires more than 100,000 to hundreds of thousands of RMB.

Support rewrite rewrite rules: HTTP requests can be distributed to different back-end server groups according to different domain names and URL s.

Built-in health check function: If the back-end web server of Nginx Proxy goes down, it will not affect the front-end access.

Bandwidth savings: Support GZIP compression, you can add browser local cache Header.

High stability: For reverse proxy, the probability of downtime is very small.

The theoretical knowledge of Nginx is not much to say here. Next, we will start to do business and build a Nginx website server.
1. One centos 7 server;
2. One centos 7 system disk;
3. Need to use the software package, here is ready to extract links:
Extract connections

First, start building Nginx website:
1. Mount the system CD and initialize the yum source

[root@Centos02 ~]# mount /dev/cdrom /mnt/     #Mounting System CD

[root@centos02 ~]# mkdir /etc/yum.repos.d/bak  #Create system yum backup directory

[root@centos02 ~]# mv /etc/yum.repos.d/CentOS-* /etc/yum.repos.d/bak/        
                #Copy all the yum configuration files that come with the system starting with Centos-to the bak directory

[root@centos02 ~]# vim /etc/yum.repos.d/local.repo            #Create yum configuration file
[local]
name=centos
baseurl=file:///mnt
enabeld=1
gpgcheck=0

2. Installing nginx

[root@centos02 ~]# yum -y install pcre pcre-devel zlib-devel        #Installing Dependent Programs

[root@centos02 ~]# umount /mnt/           #Unloading System CD, Switching Software Package CD
[root@centos02 ~]# mount /dev/cdrom /mnt/       #Mount package CD-ROM

[root@centos02 ~]# tar zxvf /mnt/nginx-1.6.0.tar.gz -C /usr/src/  
                                            #Unzip programs in mnt directory to / src

[root@centos02 ~]# useradd -M -s /sbin/nologin nginx         # Create managed nginx users

[root@centos02 ~]# cd /usr/src/nginx-1.6.0/            #Compile and install nginx
[root@centos02 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module 

[root@centos02 nginx-1.6.0]# make && make install        #Compile and install nginx

[root@centos02 nginx-1.6.0]# ln -s /usr/local/nginx/sbin/nginx* /usr/local/sbin/       
                                                     #Optimizing Execution Command

[root@centos02 ~]# echo "www.benet.com" > /usr/local/nginx/html/index.html    
                                              #Modify the new page to cover the old page

[root@centos02 ~]# vim /usr/local/nginx/conf/nginx.conf      #Setting Nginx maximum concurrency
12 events {
13     worker_connections  4096;
14 }

Configuration of nginx virtual host

[root@Centos02 ~]# vim /usr/local/nginx/conf/nginx.conf   
                              #nginx Configuration Virtual Host www.benet.com
35     server {                            #Server represents the virtual host
 36         listen       80;               #Virtual host listening port
 37         server_name  www.benet.com;             #Virtual Host Domain Name
 38         charset utf-8;                      #Supporting character encoding
 39         access_log  logs/www.benet.com.access.log;        
                                        #Success log location / usr/local/nginx/logs/
 40         error_log  logs/www.benet.com.error.log;     #Error log / usr/local/nginx/logs/
 41         location / {
 42             root   /var/www/benetcom/;          #Website root directory (default / usr/local/nginx/html/)
 43             index  index.html index.htm;          #Home page index.html or index.htm
 44         }
 45                 }

[root@Centos02 ~]# mkdir -p /var/www/benetcom   
                            #Create the root directory of www.benet.com virtual host website

[root@Centos02 ~]# echo "www.benet.com" > /var/www/benetcom/index.html       
                             #Create www.benet.com virtual host home page

[root@Centos02 ~]# vim /usr/local/nginx/conf/nginx.conf     
                                       #Configure www.accp.com virtual host
47     server {                     #Server represents the virtual host
48         listen       80;            #Virtual host listening port
49         server_name  www.accp.com;         #Virtual Host Domain Name
50         charset utf-8;           #Supporting character encoding
51         access_log  logs/www.accp.com.access.log;   
                                          #Success log location / usr/local/nginx/logs/
52         error_log  logs/www.accp.com.error.log;    #Error log / usr/local/nginx/logs/
53         location / {
54             root   /var/www/accpcom/;           #Website root directory (default / usr/local/nginx/html/)
55             index  index.html index.htm;           #Home page index.html or index.htm
56         }
57                 }

[root@Centos02 ~]# mkdir -p /var/www/accpcom         
                                 #Create the root directory of www.accp.com virtual host website

[root@Centos02 ~]# echo "www.accp.com" > /var/www/accpcom/index.html    
                                #Set up the home page of www.accp.com virtual host website

III. Start Nginx

[root@centos02 ~]# nginx        #start nginx
[root@centos02 ~]# killall -s QUIT nginx      #Stop nginx
[root@centos02 ~]# killall -s HUP nginx       #Restart nginx

4. Client validates Nginx to ensure that client and centos 7 server network are interoperable, otherwise, nothing will happen. (There is no need to manually add host file to build DNS here, and subsequently update LNMP and LAMP to configure in detail.)
1. Add host file

2. Client access

Posted by heropage on Tue, 20 Aug 2019 20:14:41 -0700