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

How to build Nginx Server in Centos7 system

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces "how to build a Nginx server in the Centos7 system". In the daily operation, I believe many people have doubts about how to build the Nginx server in the Centos7 system. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about how to build the Nginx server in the Centos7 system. Next, please follow the editor to study!

Here's how to install Nginx:

1. Preparatory work:

Centos 7 system and CD-ROM

Compile and install the software package: https://pan.baidu.com/s/1-GaLSYxt4fP5R2gCVwpILA

Extraction code: kph6

It can also be downloaded from the official website https://nginx.org/.

2. Start building the Nginx website:

Install the required dependency packages and uninstall the existing httpd services (if you are sure they are not available, omit them):

[root@mysql yum.repos.d] # yum-y erase httpd [root@mysql /] # yum-y install pcre-devel zlib-devel # required dependency package for installation

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 special 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 a link file Easy to use commands

Nginx operation control:

1. Check the configuration file

[root@mysql /] # nginx-t # check the configuration file. If the words OK or successful appear, there is no problem. Nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful

2. Start and stop Nginx

[root@mysql /] # nginx # launch [root@mysql /] # killall-s HUP nginx # restart option-s HUP equals-1 [root@mysql /] # killall-s QUIT nginx # shutdown option-s QUIT equals-3

Note: minimized installation of centos 7 does not install the killall command by default, and you can use "yum-y install psmisc"

To make it easier for Nginx services to start, stop, reload, etc., 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:-99 20PROG = "/ 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 1esacexit 0 [root@mysql /] # chmod + x / etc/init.d/nginx / / Grant permissions [root@mysql /] # chkconfig-- add nginx / / add as a system service [root@mysql /] # systemctl start nginx / / start Nginx [root@mysql / / ] # vim / usr/local/nginx/conf/nginx.conf.#user nobody / / run 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 events {use epoll / / use the epoll model to improve performance worker_connections 4096; / / handle 4096 connections per process}

The above optimizations are based on the global configuration, and the implications of each optimization are as follows:

1. Worker_processes: indicates the number of working 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 working processes. The specific meaning is reflected in the worker_connections configuration item

2. 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 working processes above. For example, if the number of working processes is 8 and each process handles 4096 connections, the number of connections that allow Nginx to provide services normally has exceeded 30,000 (409632768). 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'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@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 the 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 snooping configuration listen 80; / / snooping address and port server_name www.test1.com; / / website name FQDN charset koi8-r / / the default character set of the web page is access_log logs/host.access.log main; location / {/ / Root directory configuration root html / / location of the root directory of the website, relative to the installation directory index index.html index.php; / / default home page, index page} error_page 500502 503504 / 50x.html / / Internal error feedback page location = / 50x.html {/ / error page configuration root html;}

The above configuration only builds a website service. If you want to run more than one, copy the template provided at the end of the configuration file and paste it into the "server {}" configuration, because there are too many "{}" in the configuration file, so you need to copy it to the original "server {}" in order to avoid errors, as follows:

[root@mysql /] # vim / usr/local/nginx/conf/nginx.confserver {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 }

At this point, the virtual host is configured, and then restart the service to make the configuration effective. DNS can configure itself. Please refer to the blog post: https://blog.51cto.com/14227204/2384462

[root@mysql /] # nginx-t # before restarting the service It's best to check 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@mysql /] # systemctl restart nginx [root@mysql named] # mkdir-p / var/www/test1 # create the website root [root@mysql named] # mkdir-p / var/www/test2 [root@mysql named] # echo www.test1.com > / var/www / test1/index.html # create a test file [root@mysql named] # echo www.test2.com > / var/www/test2/index.html

The client begins to validate:

View current status information:

Test another domain name:

At this point, the study on "how to build a Nginx server in Centos7 system" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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