Nginx安装

2021-09-30  本文已影响0人  SmartBin
  1. 安装nginx需要的linux环境:

    yum install   gcc-c++   -y  pcre pcre-devel  zlib zlib-devel  openssl openssl-devel
  1. 下载nginx

    cd /opt

    wget [http://nginx.org/download/nginx-1.10.3.tar.gz](http://nginx.org/download/nginx-1.10.3.tar.gz)

    tar xvf nginx-1.10.3.tar.gz
  1. 编译安装

    cd nginx-1.10.3

    #   /home/service/nginx 作为nginx安装目录

    ./configure --prefix=/home/service/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-http_stub_status_module --with-pcre --with-stream

    make && make install
  1. 使用nginx代理 ,修改配置文件

    vi   /home/service/nginx/conf/nginx.conf

    在http的{}外部添加以下内容,例如:

    stream {
    server {

    listen 9001;

    proxy_connect_timeout 1s;
    proxy_timeout 3s;
    proxy_pass 172.51.20.180:9001;
    }
  1. 启动nginx
    先检查配置文件是否有错误: /home/service/nginx/sbin/nginx -t
    启动nginx: /home/service/nginx/sbin/nginx -c /home/service/nginx/conf/nginx.conf
  1. 重载配置文件

    /home/service/nginx/sbin/nginx -s reload
  1. 设置为开启自启动

    vim /usr/lib/systemd/system/nginx.service # 内容如下

    [Unit]
    Description=nginx
    After=network.target

    [Service]
    Type=forking
    ExecStart=/home/service/nginx/sbin/nginx
    ExecReload=/home/service/nginx/sbin/nginx -s reload
    ExecStop=/home/service/nginx/sbin/nginx -s quit
    PrivateTmp=true

    [Install]
    WantedBy=multi-user.target

设置开机自启动

systemctl enable nginx.service

查看nginx状态

systemctl status nginx.service

显示Active: inactive (dead),说明nginx已经被启动了,杀死进程: pkill -9 nginx

重启: systemctl start nginx

[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

上一篇 下一篇

猜你喜欢

热点阅读