一、安裝Nginx
1.安裝gcc
gcc(GNU Compiler Collection,GNU編譯器套件),是由 GNU 開發的編程語言編譯器。它是以GPL許可證所發行的自由軟件,也是 GNU計劃的關鍵部分。
用gcc編譯C/C++代碼時,它會試着用最少的時間完成編譯並且編譯後的代碼易於調試。易於調試意味着編譯後的代碼與源代碼有同樣的執行順序,編譯後的代碼沒有經過優化。
nginx是C語言開發的,安裝過程需要將官網的代碼進行編譯,所以需要先安裝gcc環境。
使用 yum install gcc-c++ 進行安裝
2.安裝pcre
pcre(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫,具有解析速度快的優點。nginx對正則的解析依賴於該庫,所以安裝nginx之前需要安裝pcre。
使用 yum install -y pcre pcre-devel安裝
3.安裝zlib
zlib庫提供了很多種解壓和壓縮的方式,nginx 使用 zlib 對 http 包的內容進行 gzip。
使用 yum install -y zlib zlib-devel安裝
4.安裝openssl
OpenSSL是一個強大的安全套接字層密碼庫,Apache使用它加密HTTPS,OpenSSH使用它加密SSH,但是,你不應該只將其作爲一個庫來使用,它還是一個多用途的、跨平臺的密碼工具。由於nginx支持HTTPS,所以需要提前安裝。
使用 yum install -y openssl openssl-devel安裝
5.安裝nginx
1)下載安裝包安裝
1. wget下載安裝包
wget -c https://nginx.org/download/nginx-1.15.1.tar.gz
2.解壓安裝包
tar -zxvf nginx-1.15.1.tar.gz
3.配置nginx(進入nginx解壓目錄)
執行 ./configure
4.執行命令
make && make install
安裝成功後,在local中會多出來一個nginx文件夾,其中包括nginx的啓動腳本,配置文件等。
相關操作命令
./nginx 啓動nginx
./nginx -s stop 停止服務
./nginx -s reload 重啓服務
./nginx -s quit 此方式停止步驟是待nginx進程處理任務完畢進行停止。
./nginx -s stop 此方式相當於先查出nginx進程id再使用kill命令強制殺掉進程。
以上操作都必須在目錄nginx/sbin/下面操作,非常的不方便。如果想要使用systemctl進行管理,可以將nginx加入服務項。由於過程比較複雜,所以我們推薦使用第二種安裝方式。
2)yum安裝
1.安裝epel倉庫
yum -y install epel-release
2.安裝nginx
yum -y install nginx
3.啓動nginx
systemctl start nginx
4.允許http通信
firewall-cmd --permanent --zone=public --add-service=http
5.允許https通信
firewall-cmd --permanent --zone=public --add-service=https
6.重啓防火牆
firewall-cmd --reload
二、卸載Nginx
1.停止nginx服務
systemctl nginx stop
2.刪除Nginx的自動啓動
chkconfig nginx off
3.查找nginx相關文件夾
find -name "nginx"
4.刪除相關文件夾
rm -rf XXX
5.使用yum清理
yum remove nginx
三、安裝過程中遇到的問題
由於我之前是使用安裝包安裝的nginx,每次啓動和重啓都很不方便,所以我改成了使用yum安裝,當我將之前的nginx.conf文件複製過來重啓的時候,報了一個錯。
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
然後用 systemctl status nginx查看詳情
剛開始以爲是端口占用報的錯,但是發現不是端口的問題。之後又仔細的看了看報錯信息,注意倒數第二行, nginx: [emerg] open() “/usr/share/nginx/logs/hp_access.log” failed (2: No such file or directory)。
這是之前的項目存放的日誌,現在不存在了,所以會報錯,將配置文件中的配置項刪掉,啓動成功。
調錯過程中常用的一些命令
1.查看端口進程
netstat -ntpl
2.列出正在運行的服務
systemctl
3.查看服務列表狀態(static 它是指對應的 Unit 文件中沒有定義[Install]區域,因此無法配置爲開機啓動服務。)
systemctl list-units --type=service
4.列出所有已經安裝的服務及狀態
systemctl list-unit-files
5.在開機時啓用一個服務(service可以省略)
systemctl enable xxx.service
6.在開機時禁用一個服務
systemctl disable xxx.service
7.查看服務是否開機啓動
systemctl is-enabled xxx.service
8.查看已啓動的服務列表
systemctl list-unit-files | grep enabled
9.查看啓動失敗的服務列表
systemctl --failed
10.列出nginx的進程號
ps -ef | grep nginx
11.禁止/開啓一個服務
systemctl disable/enable xxx
四、我的nginx.conf文件
user root;
#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;
worker_rlimit_nofile 65535;
events {
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
client_header_buffer_size 512k;
large_client_header_buffers 4 512k;
proxy_read_timeout 240s;
#gzip on;
upstream backend {
server localhost:8080; # Solo 監聽端口
}
server {
listen 80;
server_name bolg.huyaxing.site; # 博客域名
access_log off;
location / {
proxy_pass http://backend$request_uri;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 10m;
}
}
server {
listen 80; #監聽的端口
server_name weixin.huyaxing.site; #監聽的域名
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://weixin.huyaxing.site:9980; #跳轉的url和接口
}
access_log logs/hp_access.log; #生成的日誌,只需修改:jira_access.log,文件自動生成。
}
}