Nginx安装环境配置
安裝依賴包
安裝make:yum -y install gcc automake autoconf libtool make
安裝g++:yum?-y install gcc gcc-c++
安裝pcre和pcre-devel:yum install -y pcre pcre-devel
安裝zlib zlib提供了很多壓縮和解方式,nginx需要zlib對http進(jìn)行g(shù)zip:yum install -y zlib zlib-devel
(?報(bào)錯(cuò)的話可以這樣yum install -y zlib zlib-devel --setopt=protected_multilib=false )
安裝openssl?openssl是一個(gè)安全套接字層密碼庫,nginx要支持https,需要使用openssl:yum install -y openssl openssl-devel
?
安裝Nginx
下載nginx:wget?http://nginx.org/download/nginx-1.17.6.tar.gz
解壓nginx:tar -xzvf?nginx-1.17.6.tar.gz
tar命令詳解
進(jìn)入nginx目錄下:cd?nginx-1.17.6/
安裝Nginx:
./configure --with-http_stub_status_module --with-http_ssl_module?--with-http_v2_module?
make && make install
查看nginx的安裝目錄: whereis nginx(一般是在/usr/local/nginx)
?
環(huán)境變量配置
打開系統(tǒng)環(huán)境變量設(shè)置:vim /etc/profile?
輸入紅色字體部分:
NGINX_HOME=/usr/local/nginx
export PATH=${NGINX_HOME}/sbin:${PATH}?
使設(shè)置生效: source /etc/profile
___________________________________________________
nginx -V?
查看已存在的模塊,返回以下:
nginx version: nginx/1.17.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module
?
配置nginx.conf
nginx的配置目錄:/usr/local/nginx/conf/
啟動(dòng)nginx:nginx -c /usr/local/nginx/conf/nginx.conf
查看并編輯nginx.conf:
vim?nginx.conf
listen:監(jiān)聽端口改為8089(或其他,80端口需要備案)
server_name:站點(diǎn)域名(默認(rèn)本機(jī)ip,可以改為自定義域名如:www.test.com)
root:站點(diǎn)根目錄(html--》/usr/local/nginx/html)
保存更改:“:wq”
?重啟nginx使生效:nginx -s reload
檢查是否重啟成功:ps aux | grep nginx
重啟不成功:“kill -9? 進(jìn)程id ”或者“ nginx -s stop?”
需要kill兩個(gè)進(jìn)程,否則端口被占用無法啟動(dòng)成功
瀏覽器訪問:?
?
在Nginx上配置多個(gè)站點(diǎn)
1、在nginx.conf 目錄下創(chuàng)建文件夾:mkdir conf.d
2、拷貝當(dāng)前nginx.conf到新文件夾目錄下:cp nginx.conf conf.d/site1.conf
3、編輯site1.conf:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | server { ???????listen???????8081; ???????server_name? www.site1.com; ? ???????location / { ???????????root?? html; ???????????index? index.html index.htm; ???????} ? ????????location /error/ { ???????????alias? /sylvia/ErrorPages/; ???????} ??????? ????????location /ErrorPages/ { ???????????alias /sylvia/ErrorPages/; ???????????internal; ????????} ? ???????error_page?400?/ErrorPages/HTTP400.html; ???????error_page?401?/ErrorPages/HTTP401.html; ???????error_page?402?/ErrorPages/HTTP402.html; ???????error_page?403?/ErrorPages/HTTP403.html; ???????error_page?404?/ErrorPages/HTTP404.html; ???????error_page?500?/ErrorPages/HTTP500.html; ???????error_page?501?/ErrorPages/HTTP501.html; ???????error_page?502?/ErrorPages/HTTP502.html; ???????error_page?503?/ErrorPages/HTTP503.html; ? ???} |
引入alias,創(chuàng)建虛擬目錄。(參考:https://www.cnblogs.com/kevingrace/p/6187482.html)
4、編輯根配置nginx.conf
在http{}里面最后一行添加:include conf.d/*.conf;
5、重啟nginx
瀏覽器訪問:域名+端口+path
?
________________________________________________________________________________________
nginx 502錯(cuò)誤解決方案
如下圖:
以下是我的做法,供參考:
1.首先,把后端代碼恢復(fù)到上一次沒報(bào)錯(cuò)之前的版本,因?yàn)閚ginx報(bào)錯(cuò)不一定絕對是nginx的問題,先把系統(tǒng)恢復(fù)成可以運(yùn)行的狀態(tài),這是控制變量法呀。
2.重新加載nginx的配置文件
nginx -c /usr/local/webserver/nginx/conf/nginx.conf如果你不能全局運(yùn)行nginx命令,請加上nginx的路徑。-c后面的參數(shù)為nginx配置文件所在路徑這是要求nginx重新添加配置文件地址。
nginx -s reload重新加載文件
3.重新運(yùn)行,可以看到會(huì)出現(xiàn)如下錯(cuò)誤:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
可以看到,是進(jìn)程端口被占用了,可以看系統(tǒng)是否已有nginx在運(yùn)行。
運(yùn)行命令:ps -aux | grep nginx
然后對照,發(fā)現(xiàn)第一個(gè)和第二個(gè)的pid分別為130616,130635,kill掉即可。
kill 130616 kill 130635問題解決,再次運(yùn)行nginx,一切正常
總結(jié)
以上是生活随笔為你收集整理的Nginx安装环境配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云服务器 搭建单体redis 以及踩
- 下一篇: 【Flask】Nginx / Gunic