Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Centos 7 deploys Nginx Web Services

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

I. Foundation of Nginx services

Developed specifically for performance optimization, Nginx is best known for its stability and low system resource consumption, as well as high processing power for HTTP concurrent connections (a single physical server can support 30,000 to 50,000 concurrent requests). Because of this, a large number of enterprises that provide social networks, news and information, e-commerce and virtual hosting services have chosen Nginx to provide Web services.

1. Advantages of Nginx services

Nginx is a powerful high-performance Web and reverse proxy server with many excellent features:

High concurrent connections: official tests can support 50, 000 concurrent connections, running up to 2 million concurrent connections in the actual production environment; low memory consumption: under 3W concurrent connections, 10 NGINX processes open consume only 150 megabytes of memory (15M*10=150M); configuration files are very simple: the style is as easy to understand as the program Low cost: Nginx, as open source software, can be used free of charge, while the purchase of F5 BIG-IP, NetScaler and other hardware load balancing switches costs more than 100,000 to hundreds of thousands of RMB; support rewrite rewriting rules: can distribute HTTP requests to different backend server groups according to different domain names and URL; built-in health check function: if the backend web server of the Nginx Proxy backend goes down, the front-end access will not be affected. Save bandwidth: support GZIP compression, you can add headers of browser local cache; high stability: used for reverse proxy, the probability of downtime is minimal; second, installation and deployment of Nginx1, prerequisite preparation

The latest stable version of Nginx is 1.12.0, and its installation files can be downloaded from the official website Nginx /.

1) one Centos 7 server

2) one Windows client:

3) Centos 7 operating system image

4) Nginx image

All images and software packages used to install Nginx can be accessed from the network disk extraction: https://pan.baidu.com/s/18iRCuiMEyGbEFSeBp17uVQ

Extraction code: qszt

2. Start the installation and deployment of Nginx server 1) Mount the Linux CD and copy the nginx dependency program to the / usr/src/ directory

[root@centos02 ~] # mount / dev/cdrom / mnt/ mount: / dev/sr0 write protection. [root@centos02 ~] # cp / mnt/nginx-1.6.0.tar.gz / usr/src/ 2) switch LAMP disc and copy all data under the mnt directory to the / usr/src/ directory.

[root@centos02 ~] # umount / mnt/

[root@centos02 ~] # mount / dev/cdrom / mnt/ mount: / dev/sr0 write protection, mount read-only [root@centos02 ~] # cp / mnt/* / usr/src/ 3) to the operating system CD, install the nginx dependency program

[root@centos02 ~] # umount / mnt/

[root@centos02 ~] # mount / dev/cdrom / mnt/ mount: / dev/sr0 write protection [root@centos02 ~] # rm-rf / etc/yum.repos.d/CentOS-* [root@centos02 ~] # yum-y install pcre-devel zlib-devel [root@centos02 ~] # useradd-M-s / sbin/nologin nginx [root@centos02 ~] # tar zxvf / usr/src/nginx-1.6.0.tar.gz-C / usr/src/ [root@centos02 ~] # cd / usr/src/nginx-1.6.0/ will be mounted read-only [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 [root@centos02 ~] # ln-s / usr/local/nginx/sbin/* / usr/local/sbin/ 3, Basic management of Nginx service [root@centos02 ~] # nginx [root@centos02 ~] # netstat-anptu | grep nginx tcp 000.0.0.0nginx 80 0.0.0.0nginx * LISTEN 4663/nginx: master [root@centos02 ~] # killall-s QUIT nginx [root@centos02 ~] # killall-3 nginx [root@centos02 ~] # killall-1 nginx [root@centos02 ~] # killall-s HUP nginx [root@centos02 ~] # vim / etc/init.d/nginx #! / bin/bash#chkconfig: 35 90 30#description:nginx serverPROG= "/ usr/local/nginx/sbin/nginx" PIDF= / usr/local/nginx/logs/nginx.pid "case" $1 "instart) $PROG ; stop) kill-s QUIT $(cat $PIDF);; restart) $0 stop$0 start;;reload) kill 0s HUP $(cat $PIDF) *) echo "Usage:$0 (start | stop | restart | reload)" exit 1esacexit 0 [root@centos02 ~] # chmod + x / etc/init.d/nginx [root@centos02 ~] # chkconfig-- add nginx [root@centos02 ~] # chkconfig-- level 35 nginx on [root@centos02 ~] # / etc/init.d/nginx stop [root@centos02 ~] # / etc/init.d/nginx start [root@centos02 ~] # / etc/init.d/nginx restart

The client is configured with the same network card and network segment as the Nginx server, and the Nginx website server can be accessed by setting the gateway.

4 、 Nginx common configuration file [root@centos02 ~] # ls-ld / usr/local/nginx/conf/nginx.conf-rw-r--r-- 1 root root 2656 November 28 17:22/usr/local/nginx/conf/nginx.conf [root@centos02 ~] # ls-ld / usr/local/nginx/sbin/drwxr-xr-x 2 root root 19 November 28 17:22/usr/local/nginx/sbin/ [root@centos02 ~] # ls-ld / usr/local/nginx/html/drwxr-xr-x 2 root root 40 November 28 17:22/usr/local/nginx/html/ [root@centos02 ~] # ls-ld / usr/local/nginx/logs/drwxr-xr-x 2 root root 58 November 28 17:47 / usr/local/nginx/logs/ 5, Modify the Nginx main configuration file [root@centos02 ~] # cp / usr/local/nginx/conf/nginx.conf/usr/local/nginx/conf/nginx.conf.bak [root@centos02 ~] # vim / usr/local/nginx/conf/nginx.conf 3 user nginx 4 worker_processes 1; 6 error_log logs/error.log; 12 pid logs/nginx.pid; 16 use epoll; 17 worker_connections 1024; 29 # access_log logs/access.log main; 31 sendfile on; 35 keepalive_timeout 65; 39 server {40 listen 80; 41 server_name localhost; 44 charset utf-8 48 location / {49 root html; 50 index index.html index.html;51} 84}

Listen: limit the port while allowing the IP address to be defined, in the form of "IP address: port number". The root statement is used to set the document path of the web page at a specific access location. The default is the html/ directory under the Nginx installation directory, and it can be changed to / var/www/html and other paths as needed, but after the change, you need to ensure that nginx users have read access to it.

Worker_processes: indicates the number of worker processes. If the server consists of multiple CPU blocks or uses 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.

Worker_connections: this configuration item specifies the connections handled by each process, which is generally less than 10000 (default is 1024). It is associated with the configuration item of the number of worker processes above. For example, if the number of worker processes is 8 and each process handles 4096 connections, the number of connections that allow Nginx to provide services normally has exceeded 30,000. Of course, it also depends on the performance of physical conditions such as server hardware and network bandwidth. 6. Configure Nginx access status statistics [root@centos02 ~] # vim / usr/local/nginx/conf/nginx.conf 52 location / status {53 stub_status on; 54 access_log off; 55} [root@centos02 ~] # / etc/init.d/nginx restart

Client access status statistics page:

Active connections: indicates the current number of active connections

Server accepts handled requests: indicates the connection information that has been processed, and the three numbers in turn represent the number of connections processed, the number of successful TCP handshakes, and the number of requests processed. Configure virtual host 1, install DNS server [root@centos02 ~] # yum-y install bind bind-chroot bind-utils [root@centos02 ~] # echo "> / etc/named.conf [root@centos02 ~] # vim / etc/named.conf options {listen-on port 53 {192.168.100.20;}; directory" / var/named " } zone "benet.com" IN {type master; file "benet.com.zone";} zone "accp.com" IN {type master; file "accp.com.zone" } [root@centos02 ~] # named-checkconf-z / etc/named.conf [root@centos02 ~] # vim / var/named/benet.com.zone $TTL 86400 @ SOA benet.com. Root.benet.com (2019112801 1H 15M 1W 1D) @ NS centos02.benet.com.centos02 A 192.168.100.20www A 192.168.100.20 [root@centos02 ~] # chmod + x / var/named/benet.com.zone [root@centos02 ~] # chown named:named/var/named/benet.com.zone [root@centos02 ~ ] # named-checkzone benet.com/var/named/benet.com.zone zone benet.com/IN: loaded serial 2019112801OK [root@centos02 ~] # cp / var/named/benet.com.zone/var/named/accp.com.zone [root@centos02 ~] # vim / var/named/accp.com.zone $TTL 86400 @ SOA accp.com Root.accp.com (2019112801 1H 15M 1W 1D) @ NS centos02.accp.com.centos02 A 192.168.100.20www A 192.168.100.20 [root@centos02 ~] # named-checkzone accp.com/var/named/accp.com.zone [root@centos02 ~] # vim / etc/sysconfig/network-scripts/ifcfg-ens32 DNS1=192.168.100.20 [root@centos02 ~] # systemctl restart network [root@centos02 ~] # systemctl start named [root@centos02] # systemctl enable named [root@centos02 ~] # nslookup www.benet.com Server: 192.168.100.20Address: 192.168.100.20#53Name: www.benet.comAddress: 192.168.100.20 [root@centos02] # nslookup www.accp.com Server: 192.168. 100.20Address: 192.168.100.20#53Name: www.accp.comAddress: 192.168.100.202 、 Configure a domain name-based virtual host

Nginx's configuration file uses the "http {}" delimiter to set the HTTP server, including access log, http port, web page directory, default character set, connection retention, and site global settings such as virtual web host, php parsing, and so on, most of which are contained in the subdelimiter "server {}". "server {}" represents a specific site setting.

[root@centos02 ~] # mkdir-p / var/www/benetcom [root@centos02 ~] # mkdir-p / var/www/accpcom [root@centos02 ~] # echo "www.benet.com" > / var/www/benetcom/index.html [root@centos02 ~] # echo "www.accp.com" > / var/www/accpcom/index.html [root@centos02 ~] # vim / usr/local/nginx/conf/nginx.conf Http {include mime.types Default_type application/octet-stream; # log_format main'$remote_addr-$remote_user [$time_local] "$request" #'$status $body_bytes_sent "$http_referer" # "$http_user_agent" $http_x_forwarded_for "; # access_log logs/access.log main Server {listen www.benet.com:80; server_name www.benet.com; charset utf-8; access_log logs/www.benet.com.access.log Error_log logs/www.benet.com.error.log; location / {root / var/www/benetcom/ Index index.html;}} server {listen www.accp.com:80; server_name www.accp.com; charset utf-8; access_log logs/www.accp.com.access.log Error_log logs/www.accp.com.error.log; location / {root / var/www/accpcom/; index index.html }} [root@centos02 ~] # nginx-t nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful [root@centos02 ~] # systemctl restart named [root@centos02 ~] # / etc/init.d/nginx restart

The client adds a DNS address, and tests whether it is successful to access the domain name.

-this is the end of this article. Thank you for reading-

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report