CentOS7 從零開始 搭建服務(wù)器 --- NGINX(一)

一. 安裝與配置 NGINX 并設(shè)置開機(jī)啟動(dòng)

1. http://nginx.org/en/download.html,選擇適合Linux的版本,這里選擇最新的版本,使用wget命令下載。

微信截圖_20201110155700.png

2. 切換到/usr/local目錄,下載軟件包(如果wget未找到命令,先使用yum -y install wget 安裝wget)

# cd /usr/local
# wget http://nginx.org/download/nginx-1.18.0.tar.gz

3. 安裝NGINX

先執(zhí)行以下命令,安裝nginx依賴庫(kù)

# yum install gcc-c++
# yum install pcre
# yum install pcre-devel
# yum install zlib
# yum install zlib-devel
# yum install openssl
# yum install openssl-devel

解壓安裝包

# tar -zxvf nginx-1.18.0.tar.gz

切換到解壓目錄nginx-1.18.0

# cd /usr/local/nginx-1.18.0/

執(zhí)行 ./configure(如果報(bào)錯(cuò)需要將nginx依賴庫(kù)安裝全)

# ./configure --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module

執(zhí)行make和make install編譯nginx

# make
# make install

沒(méi)有出錯(cuò)的話,表示nginx已經(jīng)成功安裝完成,默認(rèn)安裝位置為/usr/local/nginx,之前的/usr/local/nginx-x.x.x/和壓縮安裝包可以刪除掉

4. 配置nginx開機(jī)啟動(dòng)

切換到/lib/systemd/system/目錄,創(chuàng)建nginx.service文件vim nginx.service

# cd /lib/systemd/system/
# vi nginx.service

文件內(nèi)容如下(直接復(fù)制):

[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

退出并保存文件,執(zhí)行systemctl enable nginx.service使nginx開機(jī)啟動(dòng)

# systemctl enable nginx.service

systemctl start nginx.service 啟動(dòng)nginx
systemctl stop nginx.service 結(jié)束nginx
systemctl restart nginx.service 重啟nginx

5. 驗(yàn)證是否安裝成功

向防火墻中添加端口的命令為:

# firewall-cmd --zone=public --add-port=80/tcp --permanent

重新載入防火墻規(guī)則:

# firewall-cmd --reload

輸入http://服務(wù)器IP/ 如果能看到nginx的界面,就表示安裝成功了


image.png

ps: CentOS 7 永久關(guān)閉防火墻(慎用):
查看防火墻狀態(tài)(綠的running表示防火墻開啟):

# systemctl status firewalld.service

執(zhí)行關(guān)閉命令:

# systemctl stop firewalld.service

再次執(zhí)行查看防火墻命令:

# systemctl status firewalld.service

執(zhí)行開機(jī)禁用防火墻自啟命令:

# systemctl disable firewalld.service
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容