Centos7下安裝 配置Nginx

2022-07-20 06:03:16 字數 3031 閱讀 8392

本文簡要介紹下如何在centos7下安裝nginx,以及nginx配置簡單說明。

一、安裝外掛程式

1.安裝gcc

yum install gcc-c++
2.安裝pcre、pcre-devel

pcre是乙個perl庫,包括perl相容的正規表示式庫,nginx的http模組使用pcre來解析正規表示式,所以需要安裝pcre庫。命令如下:

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,所以需要安裝 openssl 庫。命令如下:

yum install -y openssl openssl-devel
二、安裝nginx

3.使用nginx預設配置,命令如下:(當然也可以使用自定義配置,有興趣的小夥伴可以自行研究下)

./configure
4.編譯安裝,命令如下:

make

make install

5.查詢安裝路徑,命令如下:

whereis nginx
6.啟動、停止、重啟nginx

cd /usr/local/nginx/sbin/
啟動

./nginx
停止

./nginx -s quit  -- 此方式停止步驟是待nginx程序處理任務完畢進行停止。

./nginx -s stop  -- 此方式相當於先查出nginx程序id再使用kill命令強制殺掉程序。

重啟

./nginx -s quit

./nginx

當 nginx的配置檔案 nginx.conf 修改後,要想讓配置生效,使用-s reload 命令則不用先停止 nginx再啟動 nginx 即可將配置資訊在 nginx 中生效。命令如下:

./nginx -s reload
檢視nginx程序。命令如下:

ps aux|grep nginx
啟動成功後,可以在瀏覽器中看到以下頁面,至此,nginx安裝就完成了!

三、將nginx配置為系統服務

切換到/lib/systemd/system/目錄,建立nginx.service檔案。命令如下:

cd /lib/systemd/system/vim nginx.service
檔案內容如下:

[unit]

description=nginx

after=network.target remote-fs.target nss-lookup.target

[service]

type=forking

execstart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

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

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

privatetmp=true

[install]

wantedby=multi-user.target

儲存檔案並退出,執行以下命令使nginx開機啟動

systemctl enable nginx.service
配置檔案的解釋:

[unit]:服務的說明

description:服務描述

after:服務類別

[service]服務執行引數的設定

type=forking後台執行

execstart啟動命令

execreload重啟命令

execstop停止命令

privatetmp=true表示給服務分配獨立的臨時空間

注意:[service]的啟動、重啟、停止命令全部要求使用絕對路徑

[install]執行級別下服務安裝的相關設定,可設定為多使用者,即系統執行級別為3

最後,順便做個備註:

啟動nginx服務:

systemctl start nginx.service
停止nginx服務:

systemctl stop nginx.service
重啟nginx服務

systemctl restart nginx.service
服務自動啟動

systemctl enable nginx.service
服務禁止自動啟動

systemctl disable nginx.service
檢視服務狀態

systemctl status nginx.service
顯示所有已啟動的服務

systemctl list-units --type=service

CentOS 7下安裝配置FTP

ftp工具使用flashfxp,不推薦使用filezilla。yum install y vsftpdvi etc vsftpd vsftpd.conf anonymous enable no anonymous enable yes chroot local user yes 去掉前面的注釋 ch...

CentOS7下安裝配置vncserver

centos7下安裝配置 centos7下安裝配置 centos7安裝配置vncserver centos上vnc 啟動和關閉 常見問題彙總 vnc 連線不上,出現warning zhouziqi 1 is taken because of tmp x11 unix x1 vnc和主機之間共享剪下板...

CentOS7下安裝配置vncserver

首先試試伺服器裝了vnc沒 root wic rpm q tigervnc tigervnc server沒安裝的話會直接出現 package tigervnc is not installed package tigervnc server is not installed第一步,安裝vnc pa...