Linux安裝Nginx並配置啟動命令

哈哈哈hh發表於2022-07-20

映象下載、域名解析、時間同步請點選  阿里雲開源映象站

安裝前準備工作

因為Nginx依賴於gcc的編譯環境,所以,需要安裝編譯環境來使Nginx能夠編譯起來

yum install gcc-c++

Nginx的http模組需要使用pcre來解析正規表示式,需要安裝pcre

yum install -y pcre pcre-devel

安裝依賴的解壓包

yum install -y zlib zlib-devel

ssl 功能需要 openssl 庫,安裝 openssl

yum install -y openssl openssl-devel

下載Nginx

可以自己建立一個包,將nginx下載到這個路徑,我設定的路徑/opt/crm/nginx

如果需要其他nginx版本的可以參考 nginx倉庫

wget 

下載完之後解壓

tar zxvf nginx-1.10.2.tar.gz

進入到解壓之後的nginx目錄

[root@localhost src]# cd nginx-1.10.2
[root@localhost nginx-1.10.2]# ./configure && make && make install

如果要使用ssl

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

注意:如果配置了ssl,檢查配置檔案時報錯

nginx -t
nginx:[emerg]unknown directive ssl錯誤
去到nginx安裝的目錄
./configure --with-http_ssl_module
注意要把新生成的檔案複製到對應目錄
cp objs/nginx /usr/local/nginx/sbin/nginx
顯示成功就搞定
[root@iZ2ze02hshpth1x0vxo8r6Z sbin]# ./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@iZ2ze02hshpth1x0vxo8r6Z sbin]#

安裝完之後檢視安裝目錄

[root@izbp10k7vskcf4soxxbp5gz /]# whereis nginx
nginx: /usr/local/nginx
[root@izbp10k7vskcf4soxxbp5gz /]#

透過查詢檔名方式

[root@izbp10k7vskcf4soxxbp5gz /]# find / -name nginx
/opt/crm/nginx
/opt/crm/nginx/nginx-1.10.2/objs/nginx
/usr/local/nginx
/usr/local/nginx/sbin/nginx
[root@izbp10k7vskcf4soxxbp5gz /]#

直接執行

[root@izbp10k7vskcf4soxxbp5gz /]# /usr/local/nginx/sbin/nginx
[root@izbp10k7vskcf4soxxbp5gz /]# ps -ef | grep nginx
root      4666     1  0 09:32 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    4667  4666  0 09:32 ?        00:00:00 nginx: worker process
root      5028 29443  0 09:40 pts/0    00:00:00 grep --color=auto nginx
[root@izbp10k7vskcf4soxxbp5gz /]#

在瀏覽器輸入伺服器IP地址

file

增加systemctl命令方式啟動

直接啟動和關閉nginx的方式

啟動nginx的命令為     /usr/local/nginx/sbin/nginx  
停止nginx的命令為    /usr/local/nginx/sbin/nginx -s stop
重啟nginx的命令為    /usr/local/nginx/sbin/nginx -s reload

配置方式 去到/usr/lib/systemd/system/目錄新建一個nginx服務,給予執行許可權

vim /usr/lib/systemd/system/nginx.service
chmod +x /usr/lib/systemd/system/nginx.service

開啟檔案nginx.service新建內容

[Unit]                                                                                      
Description=nginx - high performance web server              
After=network.target remote-fs.target nss-lookup.target   
[Service]                                                                                 
Type=forking                                                                        
PIDFile=/usr/local/nginx/logs/nginx.pid                               
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf   
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf           
ExecReload=/usr/local/nginx/sbin/nginx -s reload                                                 
ExecStop=/usr/local/nginx/sbin/nginx -s stop                                                       
ExecQuit=/usr/local/nginx/sbin/nginx -s quit                                                        
PrivateTmp=true                                                                  
[Install]
WantedBy=multi-user.target

儲存之後過載Ststemctl命令

在啟動服務之前,需要先過載systemctl命令
systemctl daemon-reload

配置完之後

systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart 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  65535;
}
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;
    #允許壓縮的最小位元組數
    gzip_min_length 1k;
    #4個單位為16k的記憶體作為壓縮結果流快取
    gzip_buffers 4 16k;
    #設定識別HTTP協議版本,預設是1.1
    gzip_http_version 1.1;
    #gzip壓縮比,可在1~9中設定,1壓縮比最小,速度最快,9壓縮比最大,速度最慢,消耗CPU
    gzip_comp_level 2;
    #壓縮的型別
    gzip_types text/plain application/x-javascript text/css application/xml;
    #讓前端的快取伺服器混村經過的gzip壓縮的頁面
    gzip_vary   on;
	# 配置轉發到8700 埠
    upstream  huida{
      server  127.0.0.1:8700;
    }
    server {
        listen       80;
        listen       443 ssl;  					 # 配置https,監聽433埠
        server_name  xxx.xxx;                    # 注意如果申請了域名配置再此,如果配置了證照才能https訪問
        error_page 405 =200 $request_uri;
       
        ssl_certificate  cert/7629385.pem;
        ssl_certificate_key cert/7629385.key;
        client_max_body_size 50m;
        underscores_in_headers on;
        proxy_set_header Host      $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        index index.htm index.html index.php;
        proxy_connect_timeout 60; #建立tcp協議的連線時間
        proxy_send_timeout 60;    #傳送介面的時間
        proxy_read_timeout 60;    #讀取時間(介面響應時間)
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        
		# 配置轉發
      location /huida/ {
                 add_header 'Access-Control-Allow-Origin' '*';
                 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                 add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
                 add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
              proxy_pass 
         }
        location / {
            root   /home/html/huida/;
            index  index.html index.htm;
        }
        #靜態檔案交給nginx處理 代理前端靜態資源
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
        {
         root /home/html/huida/;
                expires 12;
        }
        #靜態檔案交給nginx處理
        location ~ .*\.(js|css)?$
        {
          root /home/html/huida/;
            expires 15d;
        }
        #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   
        #}
        # 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;
    #    }
    #}
    }

原文連結:https://blog.csdn.net/YMZ8848/article/details/123438614


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70003733/viewspace-2906811/,如需轉載,請註明出處,否則將追究法律責任。

相關文章