Based on Centos 7 build Nginx (virtual hosts)

Nginx developed specifically for performance optimization, its biggest advantage is its low stability and consumption of system resources, as well as high processing capacity http concurrent connections, a single physical server can support concurrent requests 20,000 to 50,000, is the case, providing a large number of social networking, news, e-commerce and web hosting, and service companies have chosen to provide Nginx web services, mainland China use nginx web site users are: Sina, Netease, Tencent, another well-known micro-blog Plurk also use nginx .
The difference between the Apache and Nginx: https://blog.51cto.com/14227204/2435423
below to start the installation Nginx:
First, the preparatory work:
Centos 7 systems and CD-ROM
package compiled and installed: https://pan.baidu.com/ s / 1-GaLSYxt4fP5R2gCVwpILA
extraction code: kph5
may be from the official website https://nginx.org/ download
II began to build Nginx website:
installation require some dependencies and unload the current httpd service (if not determined, can be omitted ):

[root@mysql yum.repos.d]# yum -y erase httpd
[root@mysql /]# yum -y  install pcre-devel zlib-devel        # 安装所需依赖包

Installation and configuration optimization compile Nginx:

[root@mysql /]# useradd -M -s /sbin/nologin nginx         # Nginx 默认以 nobody 身份运行,建议创建一个专门的用户账号
[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/       # 创建链接文件,方便命令使用

Nginx operation control:
1, check the configuration file

[root@mysql /]# nginx -t          # 检查配置文件,如果出现 OK 或 successful 字样说明没问题
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, stop Nginx

[root@mysql /]# nginx                                   # 启动
[root@mysql /]# killall -s HUP nginx              # 重启    选项 -s HUP 等同于  -1
[root@mysql /]# killall -s QUIT nginx             # 关闭    选项 -s QUIT 等同于 -3

Note: The minimum installation of centos 7 killall command is not installed by default, you can use "yum -y install psmisc"
In order to start Nginx services, stop, reload and other operations more convenient, you can write a script Nginx service, this sub-sub more management system in line with Centos habits:

[root@mysql /]# vim /etc/init.d/nginx 
#!/bin/bash
# chkconfig: - 99 20
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             // 授予权限
[root@mysql /]# chkconfig --add nginx                   // 添加为系统服务
[root@mysql /]# systemctl start nginx                   // 启动 Nginx
[root@mysql /]# vim /usr/local/nginx/conf/nginx.conf
...................
#user  nobody;             //  运行用户
worker_processes  1;             // 工作进程数量

#error_log  logs/error.log;               // 错误日志文件的位置
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;                  // PID 文件的位置

events {
        use epoll;                                       // 使用 epoll 模型,以提高性能
    worker_connections  4096;               // 每个进程处理 4096 个连接
}

The above is based on a global optimization of the configuration of the embodiment, the optimization of the following meanings:

1, worker_processes: represents the number of work processes, if the server CPU or by a plurality of multi-core processor, with reference to the total number of CPU cores to specify the number of work processes. Embodied in the specific meaning worker_connections configuration item out,
2, worker_connections: this configuration is connected to each of the specified item processing process, generally less than 10000 (default is 1024), the number of processes associated with the work items above configuration, chestnut give : If the number of worker processes is 8, each process connection processing 4096, the number of connections allowed Nginx normal service has more than 30,000 (4096 * 8 = 32768). Of course, the performance depends on the specific physical conditions of the server hardware, network bandwidth.

Build a web-based virtual host domain name:
HTTP configuration:
Nginx configuration file using the "http {}" tag is used to define the HTTP server settings, including access logs, http port, web directory, the default character set, the connection remains, as well as virtual web host, php resolved global settings and other sites, which comprises the majority of the markers defining "server {}" in the subset. "Server {}" on behalf of a specific site settings.

[root@mysql /]# 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;         // 访问日志位置

    sendfile        on;                                       // 开启高效传输文件模式
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;                      // 连接保持超时

    #gzip  on;

    server {                                           // web 服务的监听配置
        listen       80;                              // 监听地址及端口
        server_name  www.test1.com;           // 网站名称 FQDN

        charset koi8-r;                                    // 网页的默认字符集
        access_log  logs/host.access.log  main;

        location / {                                   // 根目录配置
            root   html;                              // 网站根目录的位置,相对于安装目录
            index  index.html index.php;         // 默认首页,索引页
        }

        error_page   500 502 503 504  /50x.html;          // 内部错误的反馈页面
        location = /50x.html {                                      // 错误页面配置
            root   html;
        }

The above configuration is only set up a Web site service, if you want to run more, you can copy the template profile rearmost provided and paste it into "server {}" above configuration, because there are too many in the configuration file "{}", in order avoid errors, so it to be copied to the "server {}" over the original, 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;
        }

Hosting this on the configuration, and then restart the service enable the configuration, DNS self-configuring, refer Bowen: https://blog.51cto.com/14227204/2384462

[root@mysql /]# nginx -t                   # 重启服务之前,最好是检测一下
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             # 创建网站根目录
[root@mysql named]# mkdir -p /var/www/test2
[root@mysql named]# echo www.test1.com > /var/www/test1/index.html          # 创建测试文件
[root@mysql named]# echo www.test2.com > /var/www/test2/index.html

The client begins verification:
Based on Centos 7 build Nginx (virtual hosts)
view the current status information:
Based on Centos 7 build Nginx (virtual hosts)
test another domain:
Based on Centos 7 build Nginx (virtual hosts)

Guess you like

Origin blog.51cto.com/14227204/2435579