1. 程式人生 > 實用技巧 >Nginx 安裝(LINUX)

Nginx 安裝(LINUX)

Linux 安裝 Nginx

首先判斷系統中知否安裝有nginx的依賴

yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel

下載nginx的安裝包(或者上傳到伺服器)

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.13.0.tar.gz
tar -zxvf nginx-1.13.0.tar.gz

對nginx進行編譯(注意在編譯的時候加入後面兩個模組,後續如果用https的ssl模組沒有加的話很麻煩)

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
# make 安裝
make && make install

把nginx加入到環境變數中

vim /etc/profile

PATH=$PATH:/usr/local/nginx/sbin
export PATH

source /etc/profile
nginx -v

開機啟動配置

vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
 
[Install]
WantedBy=multi-user.target
# 儲存生效
systemctl daemon-reload
# 啟動服務
systemctl start nginx.service
# 設定開機啟動
systemctl enable nginx.service

安裝後Nginx的目錄情況

​ nginx下載目錄: /usr/local/src/nginx

​ nguni 安裝目錄:/usr/local/nginx

Nginx命令:

​ nginx -t //檢查配置檔案格式是否正確

​ nginx //啟動nginx

​ nginx -s stop //停止nginx

​ nginx -s reload //重新啟動

Nginx 初始配置

初始化配置
#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;


events {
    worker_connections  1024;
}


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 {
        listen       8888;
        server_name  chaoran.red;

        #charset utf-8;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            #autoindex           on;
            #autoindex_exact_size        off;
            #autoindex_localtime                 on;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}