CentOS7 安裝 nginx 及基本配置


Ø  簡介

本文記錄 CentOS7 環境下安裝 nginx 的具體步驟,以及相關配置和使用。

1.   安裝所需環境

2.   安裝 nginx

3.   nginx 管理命令

4.   使用 systemctl 命令管理 nginx

 

n  注意事項

為了避免麻煩,建議使用 root 用戶進行安裝。因為 nginx 默認監聽80端口,但是在是 Linux 中只有 root 才能使用 1024 以下的端口。

 

1.   安裝所需環境

1)   安裝 gcc

安裝的 nginx C 語言開發的源碼包,所以需要一個 C 編譯器才能安裝。

yum install gcc-c++

 

2)   安裝 pcre pcre-devel

PCRE(Perl Compatible Regular Expressions)是一個 perl 庫,包括 perl 兼容的正則表達式庫。nginx http 模塊使用 pcre 來解析正則表達式,所以需要先安裝 pcre 庫。

pcre-devel 是使用 pcre 開發的一個二次開發庫,nginx 也需要此庫。

yum install -y pcre pcre-devel

 

3)   安裝 zlib

zlib 庫提供了很多種壓縮和解壓縮的方式,nginx 使用 zlib http 包的內容進行 gzip

yum install -y zlib zlib-devel

 

4)   安裝 OpenSSL

OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。

nginx 不僅支持 http 協議,還支持 https(即在 ssl 協議上傳輸 http),但是需要在 OpenSSL 庫的支持。

yum install -y openssl openssl-devel

 

2.   安裝 nginx

1)   下載源碼包

wget -P /opt/soft https://nginx.org/download/nginx-1.18.0.tar.gz

官網下載地址:https://nginx.org/en/download.html

目前的穩定版本:nginx-1.18.0

 

2)   解壓

cd /opt/soft

# 指定 --no-same-owner 參數防止改變所有者、所屬組

tar --no-same-owner -zxvf nginx-1.18.0.tar.gz

 

3)   配置 nginx

1.   使用默認配置安裝(root 用戶安裝)【推薦】

cd nginx-1.18.0

./configure

./configure --with-http_ssl_module     #如果需要使用 HTTPS Server

#默認的安裝配置信息

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"

 

2.   或者指定配置參數(應該對應於普通用戶安裝)

mkdir -p /opt/nginx

cd nginx-1.18.0

./configure \

--prefix=/usr/local/nginx \     #指定安裝路徑

--conf-path=/usr/local/nginx/conf/nginx.conf \

--pid-path=/usr/local/nginx/conf/nginx.pid \

--lock-path=/var/lock/nginx.lock \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/temp/nginx/client \

--http-proxy-temp-path=/var/temp/nginx/proxy \

--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \

--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \

--http-scgi-temp-path=/var/temp/nginx/scgi

 

4)   編譯安裝

make

make install

 

/usr/local/nginx/sbin/nginx     #安裝成功,啟動 ngixn

就可以正常被訪問了,端口采用默認80端口

clip_image002

 

5)   設置命令別名

設置別名是為了方便使用,否則每次執行命令必須輸入絕對路徑,比如:

/usr/local/nginx/sbin/nginx     #啟動 ngixn

 

vi ~/.bash_profile             #編輯 .bash_profile 文件

alias nginx='/usr/local/nginx/sbin/nginx'   #加入別名

source ~/.bash_profile         #使之生效

 

6)   檢查 nginx 配置文件

nginx -t

clip_image004

 

7)   設置 nginx 配置文件的路徑

nginx -t /usr/local/nginx/conf/nginx.conf

 

3.   nginx 管理命令

nginx                  #啟動 nginx

nginx -s quit           #停止(待任務處理完畢后停止)

nginx -s stop           #停止(相當於 pkill nginx

nginx -s reload         #重啟(重新載入配置文件)

 

1)   查看 nginx 進程

ps -aux | grep nginx

clip_image006

 

2)   查看 nginx 版本

nginx -v

nginx version: nginx/1.18.0

 

3)   查看/編輯 nginx 配置文件

vi /usr/local/nginx/conf/nginx.conf

clip_image008

 

4.   使用 systemctl 命令管理 nginx

1)   首先,在系統服務目錄中創建 nginx.service 文件

vi /usr/lib/systemd/system/nginx.service    #寫入以下配置

[Unit]

Description=nginx service

After=network.target

 

[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s quit

PrivateTmp=true

 

[Install]

WantedBy=multi-user.target

 

配置說明:

[Unit]          服務的說明

Description     描述服務

After           描述服務類別

[Service]       服務運行參數的設置

Type=forking    是后台運行的形式

ExecStart       服務的運行命令(絕對路徑)

ExecReload      服務的重啟命令(絕對路徑)

ExecStop        服務的停止命令(絕對路徑)

PrivateTmp      是否給服務分配獨立的臨時空間

[Install]       運行級別下服務安裝的相關設置,可設置為多用戶,即系統運行級別為3

 

2)   設置開機自啟動

systemctl enable nginx          #開啟自啟動

systemctl disable nginx         #禁止自啟動

 

3)   systemctl 相關命令

systemctl status nginx          #查看服務狀態

clip_image010

systemctl start nginx           #啟動服務

systemctl stop nginx            #停止服務

systemctl restart nginx         #重啟服務

systemctl list-units --type=service     #查看所有已啟動的服務

 

n  注意事項

發現一個問題,如果使用 nginx 命令去啟動了 nginx,就不能使用 systemctl 命令去管理,否則可能出現以下錯誤:

clip_image012

但此時,nginx 是正常運行的,且可以訪問的。

結論:要么使用 nginx 命令,要么使用 systemctl 命令去管理,最好不要混用。


免責聲明!

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



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