In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
With the rapid development of computer and Internet technology, various Web sites have become the backbone of user-oriented. In a variety of website server software. In addition to Apache, there is also a lightweight HTTP server software-Nginx.
To build a Web server based on Apache, please refer to the blog article: building a Web server based on Apache
A brief introduction to Nginx services
Developed by lgor Sysoev in Russia and developed specifically for performance optimization, Nginx is best known for its stability and low system resource consumption, as well as its high standing capability 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.
If the Web service is built to parse static web pages, dynamic web pages, etc., and does not require too many functions, then Nginx is definitely the first choice.
II. Compile and install Nginx service
The latest stable version of Nginx is 1.12.0, which can be accessed through the official website http://nginx.org/ or the network disk link: https://pan.baidu.com/s/1H5DHcVWMPGDWFQ-kDsS7XA
Extraction code: 1zyi
Download and use it.
1. Compile and install Nginx service 1) install support software
The configuration and operation of Nginx requires the support of software packages such as pcre (support for regular expressions), zlib (support for compression), etc., so you should first install the development packages of these software in order to provide the appropriate functions to ensure the smooth completion of the installation of Nginx:
[root@localhost ~] # yum-y install pcre-devel zlib-devel2) create and run users, Group [root@localhost ~] # useradd-M-s / sbin/nologin nginx3) compile and install Nginx [root @ localhost ~] # tar zxf nginx-1.12.0.tar.gz-C / usr/src [root@localhost ~] # cd / usr/src/nginx-1.12.0/ [root@localhost nginx-1.12.0] #. / configure-- prefix=/usr/local/nginx\-- user=nginx-- group=nginx-- with-http_stub_status_module// specifies the directory where the Nginx service is installed, Run users and groups to enable http_stub_status_module module to support status statistics Easy to view the connection information of the server [root@localhost nginx-1.12.0] # make & & make install4) optimize the path [root@localhost nginx-1.12.0] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin2.Nginx service running control 1) check the Nginx service configuration file
Similar to Apache's main program httpd, Nginx uses the "- t" option to check its configuration file syntax. To check the configuration file located in another location, use the "- c" option to specify the path.
[root@localhost nginx-1.12.0] # nginx- tnginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful2) launch, Stop the Nginx service [root@localhost ~] # nginx// directly run the nginx command to start the Nginx service [root@localhost ~] # netstat-anpt | grep nginxtcp 00 0.0.0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. * LISTEN 46231nginx: master / / is also port 80 of the TCP protocol by default If you have other Web service software, you should modify its port to avoid conflicts [root@localhost ~] # lynx http://127.0.0.1// can use the lynx command for text browser check (need to install lynx software package) [root@localhost ~] # killall-s HUP nginx// reload Nginx configuration file [root@localhost ~] # killall-s QUIT nginx// stop Nginx service 3) add Nginx service to the system service
In order to make the Nginx service start, stop, reload and other operations more convenient, you can write Nginx service scripts. The script reads as follows:
[root@localhost ~] # vim / ETC QUIT init.dGinx kill HUP chkconfig:-99 20PROG = "/ usr/local/sbin/nginx" PIDF= "/ case" $1 "instart) $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 1esac exit 0 [root@localhost ~] # chmod + x / etc/init.d/nginx [root@localhost ~] # chkconfig-- add nginx [root@localhost ~] # systemctl start nginx// can be used to manage Nginx service 3.Nginx service profile details
The main configuration file for the Nginx service is: / usr/local/nginx/conf/nginx.conf.
1) Global configuration [root@localhost ~] # vim / usr/local/nginx/conf/nginx.conf#user nginx; / / running user worker_processes 1; / / number of worker 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; / / location of PID file
The worker_processes configuration item indicates the number of worker processes. If the server has multiple CPU or uses multi-core processors, you can refer to the total number of CPU cores to specify the number of worker processes (you can use the command cat / proc/cpuinfo | grep "processor" | wc-l
If the number of visitors to the website is not large, it can be set to 1 (adjust itself according to the situation).
2) root@localhost O event configuration [root@localhost ~] # vim / usr/local/nginx/conf/nginx.conf … / / omit part of the content events {use epoll; / / use epoll model worker_connections 1024; / / handle 1024 connections per process}
For kernels of version 2.6 and above, it is recommended to use the epoll model to improve performance.
3) HTTP configuration [root@localhost ~] # vim / usr/local/nginx/conf/nginx.conf … / / omit part of the content http {include mime.types; / / support multimedia format 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"; / / log master format 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 timeout (default is seconds) # gzip on Server {/ / Web service snooping configuration listen 80; / / snooping address and port server_name localhost; / / website name charset utf-8 / / default character set for web pages location / {/ / Root configuration (must exist) root html; / / site root location index index.html index.php / / default home page} error_page 500502 503504 / 50x.hml; / / feedback page with internal errors location = / 50x.html {/ / error page configuration root html;}
The root statement is used to set the document path of the web page at a specific access location, which defaults to the Nginx subdirectory under the HTML installation directory. Modify it according to the actual situation.
Access status statistics and 1.Nginx access status statistics of virtual host applications
Nginx has a built-in HTTP_STUB_STATUS status statistics module, which is used to feedback the current Web access. When compiling and installing Nginx, you need to add-- with-http_stub_status_module to start the module. In addition, you have to change the configuration file for the second time:
[root@localhost] # vim / usr/local/nginx/conf/nginx.conf... / / omit part of the content server {. / / omit part of the content and add the following content location / status {stub_status on; access_log off;}} [root@localhost ~] # systemctl restart nginx
User test access:
Where "Active connections" represents the current number of active connections
"server accepts handled requests" represents the connection information processed:
The three numbers in turn represent the number of connections processed, the number of successful TCP handshakes, and the number of requests processed.
two。 Virtual Web host based on domain name
Domain name-based virtual Web hosts distinguish different Web sites by domain names. When using Nginx to build a virtual host server, each virtual Web site has its own IP address, port number and domain name of its own "server {}" configuration segment. This example creates a virtual host for different domain names.
The basic steps for creating a virtual host:
(1) build a DNS service to resolve two domain names to the same IP address.
To build a DNS service, please refer to the blog post: Linux build DNS service
(2) prepare website directory and test file [root@localhost ~] # mkdir-p / var/www/benet [root@localhost ~] # mkdir-p / var/www/accp [root@localhost ~] # echo "www.benet.com" > / var/www/benet/index.html [root@localhost ~] # echo "www.accp.com" > / var/www/accp/index.html (3) adjust the main configuration file of Nginx service [root@localhost ~] # vim / usr/local/nginx/conf/nginx.conf... / / omit part of the content http {… / / omit part of server {listen 80; server_name www.benet.com; charset utf-8; location / {root / var/www/benet; index index.html index.php;} location / status {stub_status on; access_log off } error_page 500 502 503 504 / 50x.htl; location = / 50x.html {root html;}} server {listen 80; server_name www.accp.com; charset utf-8; location / {root / var/www/accp; index index.html index.php } location / status {stub_status on; access_log off;} error_page 500 502 503 504 / 50x.html; location = / 50x.html {root html;}} [root@localhost ~] # systemctl restart nginx (4) access the virtual host
-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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.