1. 程式人生 > 其它 >Web快速入門網站部署

Web快速入門網站部署

Web快速入門網站部署

Web快速入門網站部署

http server location擴充套件瞭解項

## http{}層下允許有多個Server{}層,一個Server{}層下又允許有多個Location 
http{} 標籤主要用來解決使用者的請求 與響應
server{} 標籤主要用來響應具體的某一個網站 
location{} 標籤主要用於匹配網站具體URL路徑

部署nginx網站

# 1.新增nginx虛擬主機配置
[root@web02 ~]# vi /etc/nginx/conf.d/game.drz.com.conf
server{
       # 監聽80埠
       listen 80; 
       # 指定訪問的域名
       server_name game.dsr.com;
       # 配置URL
       location / {
       # 站點目錄
                root /code/h5_games;       
       # 指定主頁面
                index index.html;
       }
}

# 2.建立站點目錄
[root@web02 ~]# mkdir /code

# 3.修改站點目錄許可權
[root@web02 ~]# chown nginx.nginx /code/

# 4.部署程式碼
[root@web02 ~]# mv h5_games.zip /code/

# 5.解壓程式碼
[root@web02 ~]# unzip h5_games.zip

# 6.重新載入nginx的配置檔案
[root@web02 code]# systemctl reload nginx

# 7.本地域名解析
windows開啟:C:\Windows\System32\drivers\etc\hosts檔案
10.0.0.8 game.drz.com

# 8.開啟瀏覽器:http://game.drz.com      

配置Nginx的虛擬主機的三種方式

  • 基於IP方式:新建網絡卡
  • 基於埠方式:修改埠
  • 基於域名方式:多域名

Nginx日誌管理

nginx預設日誌格式語法

log_format main '$remote_addr - $remote_user [$time_local] "$request" $request_time'
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';
                
                
$remote_addr      # 記錄客戶端IP地址
$remote_user      # 記錄客戶端使用者名稱
$time_local       # 記錄通用的本地時間
$time_iso8601     # 記錄ISO8601標準格式下的本地時間
$request          # 記錄請求的方法以及請求的http協議
$status           # 記錄請求狀態碼(用於定位錯誤資訊)
$body_bytes_sent  # 傳送給客戶端的資源位元組數,不包括響應頭的大小
$bytes_sent       # 傳送給客戶端的總位元組數
$msec             # 日誌寫入時間。單位為秒,精度是毫秒。
$http_referer     # 記錄從哪個頁面連結訪問過來的
$http_user_agent  # 記錄客戶端瀏覽器相關資訊
$http_x_forwarded_for #記錄客戶端IP地址
$request_length   # 請求的長度(包括請求行, 請求頭和請求正文)。
$request_time     # 請求花費的時間,單位為秒,精度毫秒

# 注:如果Nginx位於負載均衡器,nginx反向代理之後, web伺服器無法直接獲取到客戶端真實的IP地址。
# $remote_addr獲取的是反向代理的IP地址。 反向代理伺服器在轉發請求的http頭資訊中,
# 增加X-Forwarded-For資訊,用來記錄客戶端IP地址和客戶端請求的伺服器地址。

access_log日誌配置語法

Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default: access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except

Nginx Access日誌配置實踐

server {
    listen 80;
    server_name code.oldboy.com;

    #將當前的server網站的訪問日誌記錄至對應的目錄,使用main格式
    access_log /var/log/nginx/code.oldboy.com.log main;
    location / {
        root /code;
    }

    #當有人請求改favicon.ico時,不記錄日誌
    location /favicon.ico {
        access_log off;
        return 200;
    }
}

nginx日誌切割

[root@nginx conf.d]# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
        daily                   # 每天切割日誌
        missingok               # 日誌丟失忽略
        rotate 52               # 日誌保留52天
        compress                # 日誌檔案壓縮
        delaycompress           # 延遲壓縮日誌
        notifempty              # 不切割空檔案
        create 640 nginx adm    # 日誌檔案許可權
        sharedscripts
        postrotate      # 切割日誌執行的命令
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}

## 日誌切割後的效果
[root@oldboy ~]# ll /var/log/nginx/
total 4044
-rw-r----- 1 www adm  54438 Oct 12 03:28 access.log-20181012.gz
-rw-r----- 1 www adm  28657 Oct 13 03:48 access.log-20181013.gz
-rw-r----- 1 www adm  10135 Oct 12 03:28 error.log-20181130.gz
-rw-r----- 1 www adm   7452 Oct 13 03:48 error.log-20181201.gz

使用fpm打包

# 1.獲取fpm工具
[root@web01 ~]# wget http://test.driverzeng.com/other/fpm-1.3.3.x86_64.tar.gz

# 2.安裝Ruby環境
[root@web01 ~]# yum -y install ruby rubygems ruby-devel

# 3.解壓fpm工具
[root@web01 ~]# tar xf fpm-1.3.3.x86_64.tar.gz

# 4.檢視gem源
[root@web01 ~]# gem source list

# 5.追加阿里雲的gem源
[root@web01 ~]# gem sources -a http://mirrors.aliyun.com/rubygems/

# 6.刪除國外源
[root@web01 ~]# gem sources --remove https://rubygems.org/

# 7.安裝fpm工具
[root@web01 ~]# gem install *.gem

## 儲存nginx的依賴包
yum install -y openssl-devel pcre-devel zlib-devel --downloadonly --downloaddir=/tmp

## 原始碼安裝nginx
[root@web01 ~]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
[root@web01 ~]# mkdir /app
[root@web01 ~]# tar xf nginx-1.20.1.tar.gz
[root@web01 ~]# ./configure --prefix=/app/nginx-1.20.1 --with-compat --with-file-aio -
-with-threads --with-http_addition_module --with-http_auth_request_module --withhttp_dav_module --with-http_flv_module --with-http_gunzip_module --withhttp_gzip_static_module --with-http_mp4_module --with-http_random_index_module --withhttp_realip_module --with-http_secure_link_module --with-http_slice_module --withhttp_ssl_module --with-http_stub_status_module --with-http_sub_module --withhttp_v2_module --with-mail --with-mail_ssl_module --with-stream --withstream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --withcc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protectorstrong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

[root@web01 nginx-1.20.1]# make && make install

## 寫執行指令碼
#!/bin/bash
groupadd www -g 666
useradd www -u 666 -g 666 -s /sbin/nologin -M

echo '
[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
ExecStart=/app/nginx/sbin/nginx
ExecReload=/app/nginx/sbin/nginx -s reload
ExecStop=/app/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target' > /usr/lib/systemd/system/nginx.service

ln -s /app/nginx-1.20.1 /app/nginx