Centos6、7下安裝Nginx的兩種方法


一,通過yum命令安裝

1、操作系統版本的確認很重要,因為下一步我們要依靠這個來創建yum源文件

2、創建 nginx.repo

vi  /etc/yum.repos.d/nginx.repo 

文件內容如下(如果是centos7,則對應的把數字6改成7):

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1

3、清除緩存並建立新的緩存

yum clean all

yum makecache

4、使用yum install nginx命令進行安裝,輸入y並回車

5、使用service nginx start命令啟動nginx

service nginx start

6、查看Nginx進程是否存在

二、通過下載源碼包進行安裝

 https://blog.csdn.net/darkdragonking/article/details/79034654

 https://www.cnblogs.com/yaoximing/p/6068622.html

一、安裝依賴

1. gcc 安裝
安裝 nginx 需要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:

yum install gcc-c++

2. PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。命令:

yum install -y pcre pcre-devel

3. zlib 安裝
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫

yum install -y zlib zlib-devel

4. OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫

yum install -y openssl openssl-devel

二、下載源碼(去官網下載最新穩定版)

wget -c https://nginx.org/download/nginx-1.14.2.tar.gz

三、解壓、編譯

tar -zxvf nginx-1.14.2.tar.gz 
cd nginx-1.14.2/
./configure
make && make install

四、啟動以及停止

cd /usr/local/nginx/sbin/
./nginx 
./nginx -s stop
./nginx -s quit
./nginx -s reload

五、設置開機自啟

即在rc.local增加啟動代碼就可以了

vim /etc/rc.local

增加一行

/usr/local/nginx/sbin/nginx 

設置執行權限:

chmod 755 rc.local

六、用systemctl管理

創建配置文件 

源碼安裝的nginx在/etc/systemd/system/multi-user.target.wants/目錄下是沒有nginx.service這個文件的,所以要新建

[root@localhost nginx-1.14.0]#vim /usr/lib/systemd/system/nginx.service

寫入內容(全部復制進去,然后修改)

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

修改 [Service]內容

 將:
     ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf,
 改為:
     ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

設置開機啟動

[root@localhost nginx-1.14.0]# systemctl enable nginx.service

關閉之前啟動的nginx服務

[root@localhost nginx-1.14.0]# pkill -9 nginx

重載修改過的所有配置文件

[root@localhost nginx-1.14.0]#systemctl daemon-reload

重新啟動nginx服務

[root@localhost nginx-1.14.0]#systemctl start nginx


免責聲明!

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



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