阿里雲CentOS服務器安裝nginx+開機自啓動

文章轉自:https://www.cnblogs.com/liujuncm5/p/6713784.html

 

安裝所需環境

Nginx 是 C語言 開發,建議在 Linux 上運行,當然,也可以安裝 Windows 版本,本篇則使用 CentOS 7 作爲安裝環境。

 

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

yum install gcc-c++
或
yum -y install gcc gcc-c++ autoconf automake

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

yum install -y pcre pcre-devel

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

yum install -y zlib zlib-devel

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

yum install -y openssl openssl-devel

官網下載

1.直接下載.tar.gz安裝包,地址:https://nginx.org/en/download.html

 

 

2.使用wget命令下載(推薦)。確保系統已經安裝了wget,如果沒有安裝,執行 yum install wget 安裝。--------5步

先定位文件保存目錄:cd /home/local/dyxx/download/

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

 

我下載的是1.12.0版本,這個是目前的穩定版。

解壓

依然是直接命令:--------6步

tar -zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0

配置

其實在 nginx-1.12.0 版本中你就不需要去配置相關東西,默認就可以了。當然,如果你要自己配置目錄也是可以的。
1.使用默認配置--------7步

./configure

2.自定義配置(不推薦)

./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注:將臨時文件目錄指定爲/var/temp/nginx,需要在/var下創建temp及nginx目錄

編譯安裝--------8步

make
make install

查找安裝路徑:

whereis nginx

啓動、停止nginx--------9步

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

啓動時報80端口被佔用:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

 

 解決辦法:1、安裝net-tool 包:yum install net-tools

 

./nginx -s quit:此方式停止步驟是待nginx進程處理任務完畢進行停止。
./nginx -s stop:此方式相當於先查出nginx進程id再使用kill命令強制殺掉進程。

查詢nginx進程:

ps aux|grep nginx

重啓 nginx

1.先停止再啓動(推薦):
對 nginx 進行重啓相當於先停止再啓動,即先執行停止命令再執行啓動命令。如下:

./nginx -s quit
./nginx

2.重新加載配置文件:
當 ngin x的配置文件 nginx.conf 修改後,要想讓配置生效需要重啓 nginx,使用-s reload不用先停止 ngin x再啓動 nginx 即可將配置信息在 nginx 中生效,如下:
./nginx -s reload

啓動成功後,在瀏覽器可以看到這樣的頁面:

nginx-welcome.png

開機自啓動

即在rc.local增加啓動代碼就可以了。--------開機啓動配置1

vi /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx
設置執行權限:--------開機啓動配置2

chmod 755 rc.local

nginx-rclocal.png

 

後面有個文章轉講解nginx無法配置SSL模塊(即配置Https啓動nginx報錯nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:19)

https://blog.csdn.net/m0_37711292/article/details/85163834

 

除了負載均衡,Nginx還能做些什麼?

https://blog.csdn.net/m0_37711292/article/details/105047263

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章