linux之Nginx安裝部署


Nginx web 使用

一、Nginx介紹

1.什么是nginx

Nginx是一個開源且高性能、可靠的http web服務、代理服務

開源:直接獲取源代碼
高性能:支持海量開發
可靠:服務穩定

2.nginx的特點

  • nginx支持很高的並發,nginx在處理大量並發的情況下比其他web服務要快
  • 功能模塊少,只保留核心模塊,其他代碼模塊化 (易讀,便於二次開發,對於開發人員非常友好)
  • 需要什么模塊再安裝模塊,不需要全部安裝,並且還支持第三方模塊
  • 其他的web服務需要每隔一段時間進行重啟,nginx不需要
  • nginx可以再運行期間,更新迭代,代碼部署
  • 大多數公司都在用nginx
  • Nginx使用的是Epool網絡模型(當用戶發起一次請求,epoll模型會直接進行處理,效率高效,並無連接限制.)
    image
    image
    image
    image

二、Nginx部署

1.官方源安裝部署

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo  # 把下面的代碼寫入該文件保存
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@web01 ~]# yum install nginx -y
[root@web01 ~]# systemctl start nginx  # 如果有httpd服務開啟的話在這之前要執行systemctl stop httpd

2.編譯安裝部署

[root@web01 ~]#  wget https://nginx.org/download/nginx-1.20.2.tar.gz  # 下載安裝包
[root@web01 ~]# tar -xf nginx-1.20.2.tar.gz  # 解壓源碼包
[root@web01 nginx-1.20.2]# ./configure
[root@web01 nginx-1.20.2]# make
[root@web01 nginx-1.20.2]# make install
[root@web01 nginx-1.20.2]# /usr/local/nginx/sbin/nginx  # 啟動nginx服務

image

三、優化編譯安裝(模仿yum安裝)

# 優化
[root@lb01 nginx]# mkdir /etc/nginx
[root@lb01 nginx]# mv /usr/local/nginx/conf/* /etc/nginx/
[root@lb01 nginx]# mkdir /etc/nginx/conf.d
[root@lb01 nginx]# vi /etc/nginx/nginx.conf  # 把之前里面的全部刪除,添加下面內容.
user  www;  # 這個原本是nginx,根據實際情況修改user
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}



[root@lb01 nginx]# groupadd www -g 666
[root@lb01 nginx]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin

[root@lb01 nginx]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target

[root@lb01 sbin]# ln -s /etc/nginx/nginx.conf /usr/local/nginx/conf/nginx.conf
[root@lb01 sbin]# mv /usr/local/nginx/sbin/nginx /usr/sbin/
[root@lb01 sbin]# mkdir /var/log/nginx

# 測試是否成功
[root@lb01 sbin]# systemctl start nginx

四、平滑增加Nginx模塊

[root@web01 nginx-1.20.2]# yum -y install openssl openssl-devel  # 安裝依賴
在上面./configure的時候,可以加--help查看有哪些模塊  然后根據需要在后面添加模塊名
[root@web01 nginx-1.20.2]#./configure  --with-http_ssl_module
[root@web01 nginx-1.20.2]#make 
[root@web01 nginx-1.20.2]#make install 

五、編譯安裝nginx添加新模塊

先確定需要添加的新模塊,比如要新添加--with-http_sub_module模塊

[root@lb01 ~]# rm -rf nginx-1.20.2
[root@lb01 ~]# tar -xf nginx-1.20.2.tar.gz
[root@lb01 ~]# cd nginx-1.20.2
# 查看之前安裝的模塊
[root@lb01 nginx-1.20.2]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_gzip_static_module --with-stream --with-http_ssl_module #這是之前的模塊
# 安裝舊模塊和新模塊
[root@lb01 nginx-1.20.2]# ./configure --with-http_gzip_static_module --with-stream --with-http_ssl_module --with-http_sub_module
[root@lb01 nginx-1.20.2]# make && make install
[root@lb01 nginx-1.20.2]# rm -rf /usr/sbin/nginx
[root@lb01 nginx-1.20.2]# cd /usr/local/nginx/sbin/
[root@lb01 sbin]# mv nginx /usr/sbin/
# 配置成功

六、Nginx的命令

nginx #啟動nginx。 等價於systemctl start nginx
 
nginx -s reopen #重啟Nginx。   等價於systemctl restart nginx
 
nginx -s reload #重新加載Nginx配置文件,然后以優雅的方式重啟Nginx。 等價於systemctl reload nginx
 
nginx -s stop #強制停止Nginx服務。 等價於systemctl stop nginx
 
nginx -s quit #優雅地停止Nginx服務(即處理完所有請求后再停止服務)
 
nginx -t #檢測配置文件是否有語法錯誤,然后退出
 
nginx -?,-h #打開幫助信息
 
nginx -v #顯示版本信息並退出
 
nginx -V #顯示版本和配置選項信息,然后退出
 
nginx -V 2>&1 | sed "s/\s\+--/\n --/g" #模塊分行輸出,格式化輸出
 
killall nginx #殺死所有nginx進程
 
systemctl enable nginx  #加入開機自啟
    Centos6:
        啟動:nginx
            service nginx start
            /etc/init.d/nginx start
        加入開機自啟:
            chkconfig nginx on
 
nginx -T #檢測配置文件是否有語法錯誤,轉儲並退出
 
nginx -q #在檢測配置文件期間屏蔽非錯誤信息
 
nginx -p prefix #設置前綴路徑(默認是:/usr/share/nginx/)
 
nginx -c filename #設置配置文件(默認是:/etc/nginx/nginx.conf)
 
nginx -g directives #設置配置文件外的全局指令

image

七、Nginx配置文件

[root@web01 ~]# cat /etc/nginx/nginx.conf 
#########################核心模塊####################
#指定啟動的用戶
user  www;
#nginx的worker進程的數量
worker_processes  1;
#指定錯誤日志存放的路徑以及記錄的級別 debug/info/notice/warn/error/emerg
error_log  /var/log/nginx/error.log warn;
#指定pid文件
pid        /var/run/nginx.pid;
 
########################事件驅動模塊#################
events {
    #每個worker工作進程的最大連接數
    worker_connections  1024;
}
 
######################http內核模塊###################
# web服務的模塊
http {
    # 加載外部的配置文件   好處是可以把該文件簡單化,分散開來寫
    include       /etc/nginx/mime.types;
    #當nginx不識別文件類型的時候,默認下載
    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"';
                      # 可以替換成json格式的日志,把下面代碼替換上去就行了
                      log_format json '{"@timestamp":"$time_iso8601",'
                      '"host":"$server_addr",'
                      '"service":"nginxTest",'
                      '"trace":"$upstream_http_ctx_transaction_id",'
                      '"log":"log",'
                      '"clientip":"$remote_addr",'
                      '"remote_user":"$remote_user",'
                      '"request":"$request",'
                      '"http_user_agent":"$http_user_agent",'
                      '"size":$body_bytes_sent,'
                      '"responsetime":$request_time,'
                      '"upstreamtime":"$upstream_response_time",'
                      '"upstreamhost":"$upstream_addr",'
                      '"http_host":"$host",'
                      '"url":"$uri",'
                      '"domain":"$host",'
                      '"xff":"$http_x_forwarded_for",'
                      '"referer":"$http_referer",'
                      '"status":"$status"}';
                      access_log /var/log/nginx/access.log json ;
    #指定訪問日志存儲路徑與格式
    access_log  /var/log/nginx/access.log  main;
    #高效讀取文件
    sendfile        on;
    #高效傳輸
    #tcp_nopush     on;
    #開啟長連接
    keepalive_timeout  65;
    #開啟壓縮
    #gzip  on;
    #包含網站的配置文件
    include /etc/nginx/conf.d/*.conf;
    ###############打開上面的文件就是下面的內容###############
 
    #一個server表示一個網站,可以有多個
    server {
        #監聽的端口
        listen       80;
        #網站提供的域名
        server_name  localhost;
        #字符集
        charset utf8;
        #控制訪問的網站路徑
        location / {
            #指定指定網站路徑
            root   /usr/share/nginx/html;
            #指定網址的索引文件
            index  index.html index.htm;
        }
    }
}

image

八、搭建超級瑪麗和象棋小游戲

1.准備工作

1、上傳代碼
	mkdir /opt/Super_Mary創建游戲目錄
	把游戲壓縮包解壓

2.編輯配置文件

[root@web01 conf.d]# vim /etc/nginx/conf.d/supermary.conf
server {
    listen 80;
    server_name game001.com;
    location / {
        root /opt/Super_Mary;
        index index.html;
    }
}

3.測試配置文件是否正常

[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4.重啟Nginx

[root@web01 conf.d]# systemctl restart nginx 

5.域名解析

C:\Windows\System32\drivers\etc\hosts
172.16.1.7  game001.com   # ip是服務器的ip

6.開玩!

在網站上輸入game001.com開始你的表演!!!
image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM