码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx部署!

时间:2019-09-14 11:07:05      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:exce   star   click   tables   clu   gre   int   remote   监听配置   

一:使用Nginx搭建虚拟主机服务器时,每个虚拟WEB站点拥有独立的"server {}"配置段,各自监听的IP地址、端口号可以单独指定,当然网站名称也是不同的。

二:虚拟机的分类:

  1:基于域名虚拟主机(一个ip地址对应多个域名,不同域名就是不同的站点,其内容也不一样)

  2:基于端口虚拟主机(服务器只有一个ip地址,不同端口就是不同的站点,其内容也不一样)

  3:基于ip虚拟主机(服务器有多个ip地址,不同ip就是不同的站点,其内容也不一样)

二:nginx -t 用于检测配置文件语法

如下报错1:配置文件43行出现错误

[root@www ~]# nginx -t
nginx: [emerg] "location" directive is not allowed here in /usr/local/nginx/conf/nginx.conf:43
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

如下错误2:worker里面工作区出现问题

[root@www ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

解决办法:

[root@www ~]# ulimit -n 10240

=============================================================

创建单个基于域名的虚拟主机并测试

[root@localhost ~]# rpm -q httpd                         //有httpd软件必须删除

未安装软件包 httpd

安装支持软件
[root@localhost ~]# rpm -q gcc gcc-c++ zlib-devel pcre-devel make
[root@localhost ~]# yum -y install gcc gcc-c++ zlib-devel pcre-devel make

完毕!

创建运行用户、组
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tail -l /etc/passwd ;tail -l /etc/group

导入nginx软件包
[root@localhost ~]# rz -E                                       
rz waiting to receive.
[root@localhost ~]# ls
anaconda-ks.cfg   initial-setup-ks.cfg  nginx-1.16.0.tar.gz   original-ks.cfg
[root@localhost ~]# tar xf nginx-1.16.0.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/nginx-1.16.0/

编译安装 Nginx
[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

 --prefix 设定Nginx的安装目录

--user和--group 指定Nginx运行用户和组

--with-http_stub_status_module 启用http_stub_status_module模块以支持状态统计

--with-http_ssl_module 启用SSL模块

--with-http_flv_module 启用FLV模块,提供寻求内存使用基于时间的偏移量文件

[root@localhost nginx-1.16.0]#make

[root@localhost nginx-1.16.0]#make install

[root@localhost nginx-1.16.0]# cd

为主程序 nginx  创建链接文件
[root@localhost ~]# ls /usr/local/nginx/
conf   html  logs     sbin

[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost ~]# ll /usr/local/sbin/nginx
lrwxrwxrwx. 1 root root 27 9月 10 17:12 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx

Nginx 的运行控制方法
手动方法控制 Nginx:
nginx -t 检测配置文件语法
执行 nginx 主程序启动 Nginx

[root@localhost ~]# 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@localhost ~]# nginx               //启动nginx服务
[root@localhost ~]# netstat -anpt | grep nginx
tcp     0           0 0.0.0.0:80              0.0.0.0:*               LISTEN 64726/nginx: master

[root@localhost ~]# killall -l nginx
HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM
STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS
UNUSED
[root@localhost ~]# killall -s HUP nginx               //平滑重启
[root@localhost ~]# killall -s QUIT nginx              //正常停止
[root@localhost ~]# nginx -s reload
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
[root@localhost ~]# nginx -s stop

编写 nginx  服务脚本

[root@localhost ~]# vim /etc/init.d/nginx

技术图片

#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Server Control Scripts shell
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@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig --list nginx

nginx       0:关        1:关         2:开        3:开          4:开        5:开      6:关

[root@localhost ~]# /etc/init.d/nginx status
Nginx is stopped
[root@localhost ~]# /etc/init.d/nginx stop
[root@localhost ~]# /etc/init.d/nginx start
[root@localhost ~]# /etc/init.d/nginx status
Nginx is running

[root@localhost ~]# /etc/init.d/nginx reload

Nginx  配置文件分析

[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# cp nginx.conf nginx.conf.origin
[root@localhost conf]# vim nginx.conf

技术图片
user  nginx nginx;                           //nginx的程序账户及程序组
worker_processes  2;                         //指定进程数一般与cpu数量一致
worker_cpu_affinity 00000001 00000010;       //为每个进程分配核心数
error_log  logs/error.log  info;             //全局错误日志文件位置
pid        logs/nginx.pid;                   //PID文件的位置
events {
   use epoll;                                //使用epoll模型
    worker_connections  10240;               //每个进程允许的最多的连接数默认为1024一般10000以下
}
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;                       //支持文件发送超时
    keepalive_timeout  65;
server {                                      //web服务的监听配置
        listen       80;                      //监听地址及端口(IP:PORT)
        server_name  www.crushlinux.com;     //网站名称(FQDN)        
charset utf-8;
//网页的默认字符集
access_log logs
/www.crushlinux.com.access.log main; location / { //根目录配置
root
html; //网站根目录的位置安装位置的html中
index index.html index.htm; //默认首页
}
l
ocation /status {
stub_status on; //打开状态统计状态
access_log off; //关闭此位置的日志记录 }

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

 
技术图片

[root@localhost conf]# pwd
/usr/local/nginx/conf

[root@localhost conf]# mkdir ../html/mailcom
[root@localhost conf]# echo "<h1>www.crushlinux.com</h1>" > ../html/index.html

[root@localhost conf]# nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost conf]# ulimit -n 65536
[root@localhost conf]# 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@localhost conf]# systemctl stop firewalld
[root@localhost conf]# iptables -F
[root@localhost conf]# setenforce 0

[root@localhost conf]# systemctl restart nginx
Warning: nginx.service changed on disk. Run ‘systemctl daemon-reload‘ to reload units.

[root@localhost conf]# /etc/init.d/nginx reload

技术图片

技术图片

 

Active connections 表示当前活跃的连接数,

第三行的三个数字表示Nginx当前总共处理了2个连接,成功创建3次握手,总共处理了2个请求。

=======================================================================

虚拟主机应用:(创建多个基于域名的虚拟主机并测试)

要创建两个站点www.crushlinux.com和www.cloud.com

为两个虚拟WEB主机分别建立根目录,并准备测试首页

 

[root@localhost~]#mkdir /usr/local/nginx/html/crushlinux

 

[root@localhost~]#mkdir /usr/local/nginx/html/cloud

 

[root@localhost~]# echo "<h1>www.crushlinux.com</h1>" >/usr/local/nginx/html/crushlinux/index.html

[root@localhost~]# echo "<h1>www.cloud.com</h1>" > /usr/local/nginx/html/cloud/index.html

[root@localhost~]# vim /usr/local/nginx/conf/nginx.conf

技术图片
user  nginx nginx;
worker_processes  2;
worker_cpu_affinity 00000001 00000010;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
   use epoll;
    worker_connections  10240;
}
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;
    keepalive_timeout  65;
server {
        listen       80;
        server_name  www.crushlinux.com;
        charset utf-8;
        access_log  logs/www.crushlinux.com.access.log  main;
location / {
            root   html/crushlinux;
            index  index.html index.htm;
        }

location ~ /status {
              stub_status on;
             access_log off;
}

 error_page  500 502 503 504   /50x.html;

          location = /50x.html {
             root html;
   }
}

  server {
         listen 80;
         server_name www.cloud.com;
         charset utf-8;
         access_log logs/cloud.access.log main;
 location / {
         root html/cloud;
         index index.html index.html;
}
         error_page 500 502 503 504 /50x.html;
  location = /50x.html {
         root html;
    }
  }
}

技术图片

[root@www ~]# killall -3 nginx               //正常停止
[root@www ~]# nginx                            //正常启动

[root@www ~]# yum -y install elinks
[root@www ~]# elinks --dump http://www.crushlinux.com
www.crushlinux.com
[root@www ~]# elinks --dump http://www.cloud.com
www.cloud.com

技术图片

 

技术图片

Nginx部署!

标签:exce   star   click   tables   clu   gre   int   remote   监听配置   

原文地址:https://www.cnblogs.com/L1-5551/p/11518444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!