nginx 下載:
http://nginx.org/en/download.html
Nginx的版本有:
Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以說是開發版。
Stable version:最新穩定版,生產環境上建議使用的版本。
Legacy versions:遺留的老版本的穩定版。
一般就選最新版本,Stable version:最新穩定版。
ngixn安裝:
http://nginx.org/download/nginx-1.20.0.tar.gz
①.安裝如下3個依賴軟件:
yum -y install pcre pcre-devel
yum -y install openssl openssl-devel
yum -y install zlib zlib-devel
②.上傳文件/解壓文件/ 安裝為服務
tar -zxvf nginx-1.20.0.tar.gz
cd nginx-1.20.0
./configure --prefix=/usr/local/nginx --with-http_ssl_module
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
make && make install
make[1]: 離開目錄“/root/test/nginx-1.20.0”
啟動nginx
/usr/local/nginx/sbin/nginx
參數 "-c" 指定了配置文件的路徑,如果不加 "-c" 參數,Nginx 會默認加載其安裝目錄的 conf 子目錄中的 nginx.conf 文件。
nginx 添加環境變量
①設置環境變量,使得在任何地方都可以直接訪問php-fpm
vim /etc/profile
export PATH=/usr/local/nginx/sbin:$PATH
:wq保存退出
#使得環境變量立即生效
source /etc/profile
# 查看 設置的環境變量
echo $PATH
/usr/local/nginx/sbin:/usr/local/php-8.0.6/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
②檢查是否啟動
netstat -tunpl | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5410/nginx: master
## 沒有添加path 用絕對路徑
nginx 啟動nginx
nginx –s stop 停止nginx的服務
nginx –s reload 不停止nginx的服務,重新加載配置文件。
nginx –t 檢測配置文件是否有錯誤。
外界訪問/防火牆開放端口
firewall-cmd --zone=public --list-ports # 查看開放端口
firewall-cmd --zone=public --add-port=80/tcp --permanent # 開啟端口
firewall-cmd --reload # 重啟防火牆
瀏覽器 輸入 linux 服務器 ip 地址即可
nginx 修改日志配置:
① 進入配置文件
/usr/local/nginx/conf
vim nginx.conf
② 日志配置
在http 斷定義格式{
log_format mylog '$remote_addr - $request - $status - $http_user_agent - $http_referer';
}
在server 指定目錄 server {
access_log logs/nihao.log mylog;
}
重啟 : /usr/local/nginx/sbin/nginx -s reload
瀏覽器訪問ngix/進入log查看日志文件
cd /usr/local/nginx/logs
nginx 定時任務,完成日志分割:
① 編寫sh腳本
在/date目錄下面新建一個腳本runlog.sh
mkdir /date
vim runlog.sh
#!/bin/bash
logname=/usr/local/nginx/logs/nihao.log
newlog=/date/$(date -d yesterday +%Y%m%d%H%M).nihao.log
mv $logname $newlog
touch $logname
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
② 編寫定時任務 運行① sh腳本
crontab -e
*/1 * * * * sh /date/runlog.sh
查看定時任務crontab -l
編輯定時任務crontab –e
刪除定時任務crontab -r
注:如果沒有安裝定時任務,可以使用
yum install -y vixie-cron 命令進行安裝;
啟動定時任務 service crond start