CentOS 7.9 安裝 nginx-1.22.0

一、CentOS 7.9 安裝 nginx-1.22.0


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

2 安裝前的準備

# 操作系統內核版本
uname -a
# 操作系統發行版本
cat /etc/redhat-release

在安裝Nginx之前,我們需要確保安裝Nginx所依賴的其他程序,執行下面的命令,安裝或更新相應的程序。

yum install -y make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel libxslt-devel geoip-devel gd gd-devel

執行完成後,如果之前未安裝的,則會自動安裝,如果之前已經安裝有舊的版本,則會被新的版本代替。

 

3 wget下載

# 推薦wget下載
yun install -y wget
wget //nginx.org/download/nginx-1.22.0.tar.gz

4 創建用戶和組

useradd nginxxyz -s /sbin/nologin
id nginxxyz

 

二、解壓


tar -zxvf /opt/software/nginx-1.22.0.tar.gz -C /opt/    # 解壓
cd /opt/nginx-1.22.0    # 進入nginx目錄

 

三、配置編譯模塊

使用 ll 可以看到目錄下有 configure 的可執行文件,這個文件的作用,就是根據你系統的情況,生成makefile的,以便於下一步的編譯和安裝

cd /opt/nginx-1.22.0
./configure   # 不帶參數,默認會安裝到 /usr/local/nginx 目錄,也可以 指定參數。
./configure --prefix=/usr/local/nginx  # 則會在安裝的時候,安裝到 /usr/data/nginx 的目錄  。
./configure \
--user=nginxxyz \
--group=nginxxyz \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module=dynamic \
--with-stream_ssl_preread_module \
--with-compat  \
--with-pcre-jit \
--prefix=/usr/local/nginx

 

四、編譯&安裝


make
make install         # 這兩行可以分開執行,也可以在同一行執行
make && make install # 同一行執行

 

五、修改環境變量


將nginx服務加入環境變量

在文件中添加 nginx 的安裝路徑下的bin 目錄

vim /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin
# 使配置文件生效
source /etc/profile

 

六、啟動


# 啟動nginx
nginx
# 重啟nginx
nginx -s reload
# 停止nginx
nginx -s stop

 

七、自啟動


很多時候,我們為了方便管理,在服務器重啟後,需要nginx自動啟動,那麼我們可以添加 nginx 的服務

# 創建 nginx 服務文件
vim /lib/systemd/system/nginx.service

nginx 的服務文件配置可參考如下:

[Unit]
Description=nginx
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 stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

完成後,按ESC鍵,輸入:wq 保存並退出,上面的nginx 相應的目錄,需要改為你自己的目錄。

服務文件配置好了,接下來要把它添加到服務裏面。

systemctl enable nginx.service

執行完後,系統會在下方提示:

Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

看到這個,nginx 的服務就已經完成添加,但這個時候,還沒有啟動的,我們可以通過下面的命令來操作nginx。

# 查看運行狀態
systemctl status nginx

其他命令

# 啟動 nginx
systemctl start nginx
# 停止 nginx
systemctl stop nginx
# 重新加載 nginx
systemctl reload nginx

如果重新修改 nginx.service 配置文件後,則需要使用下面的命令來重新加載服務的配置文件。

# 重新加載服務的配置文件
systemctl daemon-reload

 

八、nginx 配置


使用源碼安裝方式,nginx的配置文件,通常會在  /usr/local/nginx/conf/nginx.conf  這個位置,可以通過 vi 或 vim 修改。 

# 打開配置文件
vim /usr/local/nginx/conf/nginx.conf

 

九、防火牆


# 關閉防火牆
systemctl stop firewalld

# 開放3306端口命令
firewall-cmd --zone=public --add-port=3306/tcp --permanent
# 配置立即生效
firewall-cmd --reload

 

雲主機需配置安全組(默認已放行,可省略

在入方向規則,允許80放行

 

 

十、問題記錄


錯誤:./configure: error: the HTTP rewrite module requires the PCRE library.

解決:安裝pcre-devel:yum -y install pcre-devel

 

錯誤:./configure: error: the HTTP cache module requires md5 functions 

from OpenSSL library. You can either disable the module by using 
–without-http-cache option, or install the OpenSSL library into the system, 
or build the OpenSSL library statically from the source with nginx by using 
–with-http_ssl_module –with-openssl= options.

解決:yum -y install openssl openssl-devel

 

錯誤:./configure: error: the HTTP XSLT module requires the libxml2/libxslt    缺少libxml2

解決:yum -y install libxml2 libxml2-devel && yum -y install libxslt-devel

 

錯誤信息:./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries.

解決方法:http_image_filter_module是nginx提供的集成圖片處理模塊,需要gd-devel的支持   yum -y install gd-devel

 

錯誤信息:./configure: error: perl module ExtUtils::Embed is required 缺少ExtUtils

解決方法:yum -y install perl-devel perl-ExtUtils-Embed

 

錯誤信息:./configure: error: the GeoIP module requires the GeoIP library. You can either do not enable the module or install the library. 缺少GeoIP

解決方法:yum -y install GeoIP GeoIP-devel GeoIP-data

 

錯誤信息:./configure: error: the Google perftools module requires the Google perftools
library. You can either do not enable the module or install the library.

解決方法:yum -y install gperftools

 

Tags: