Nginx安裝

  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

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容