CentOS7 編譯安裝 Nginx

一、安裝前準備工做

# gcc 安裝
#安裝 nginx 須要先在官網下載的源碼進行編譯,編譯依賴 gcc 環境,若是沒有 gcc 環境,則須要安裝
yum install gcc-c++

# PCRE pcre-devel 安裝
# nginx 的 http 模塊使用 pcre 來解析正則表達式,pcre-devel 是使用 pcre 開發的一個二次開發庫
yum install -y pcre pcre-devel

# zlib 安裝
# zlib 庫提供了不少種壓縮和解壓縮的方式。由於我是最小安裝,因此須要安裝這些庫。若是已經安裝瞭解壓工具則不須要安裝
yum install -y zlib zlib-devel

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

# 建立組
groupadd nginx 

# 建立帳號到組
useradd -g nginx -d /home/nginx nginx

# 建立密碼
passwd nginx

二、下載最新穩定版的 Ningx,並執行編譯安裝

# 下載最新 .tar.gz 安裝包,官網地址:https://nginx.org/en/download.html 
wget https://nginx.org/download/nginx-1.14.1.tar.gz

# 解壓安裝包
tar -zxvf nginx-1.14.1.tar.gz

# 進入安裝包目錄
cd nginx-1.14.1

# 加載配置文件
./configure \
--user=nginx --group=nginx \          #安裝的用戶組
--prefix=/usr/local/nginx \           #指定安裝路徑
--with-http_stub_status_module \         #監控nginx狀態,需在nginx.conf配置
--with-http_ssl_module \             #支持HTTPS
--with-http_sub_module \             #支持URL重定向
--with-http_gzip_static_module          #靜態壓縮

# 編譯並安裝
make && make install

三、nginx 命令全局執行設置

cd /usr/local/nginx/sbin/
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx

四、設置開機自啓動

cd /etc/rc.d
sed -i '13a /usr/local/nginx/sbin/nginx' /etc/rc.d/rc.local
chmod u+x rc.local

五、關閉防火牆

systemctl stop firewalld.service
systemctl disable firewalld.service

六、Nginx 經常使用命令

# 啓動
systemctl start nginx.service
	
# 停用
systemctl stop firewalld.service
	
# 重啓
systemctl restart firewalld.service

# 測試配置文件 nginx.conf 正確性
nginx -t

# 版本號查看
nginx -v
相關文章
相關標籤/搜索