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進行gzip:yum install -y zlib zlib-devel
(?報錯的話可以這樣yum install -y zlib zlib-devel --setopt=protected_multilib=false )
安裝openssl?openssl是一個安全套接字層密碼庫,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命令詳解
進入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)
?
環境變量配置
打開系統環境變量設置:vim /etc/profile?
輸入紅色字體部分:
NGINX_HOME=/usr/local/nginx
export PATH=${NGINX_HOME}/sbin:${PATH}?
使設置生效: 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/
啟動nginx:nginx -c /usr/local/nginx/conf/nginx.conf
查看并編輯nginx.conf:
vim?nginx.conf
listen:監聽端口改為8089(或其他,80端口需要備案)
server_name:站點域名(默認本機ip,可以改為自定義域名如:www.test.com)
root:站點根目錄(html--》/usr/local/nginx/html)
保存更改:“:wq”
?重啟nginx使生效:nginx -s reload
檢查是否重啟成功:ps aux | grep nginx
重啟不成功:“kill -9? 進程id ”或者“ nginx -s stop?”
需要kill兩個進程,否則端口被占用無法啟動成功
瀏覽器訪問:?
?
在Nginx上配置多個站點
1、在nginx.conf 目錄下創建文件夾:mkdir conf.d
2、拷貝當前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,創建虛擬目錄。(參考:https://www.cnblogs.com/kevingrace/p/6187482.html)
4、編輯根配置nginx.conf
在http{}里面最后一行添加:include conf.d/*.conf;
5、重啟nginx
瀏覽器訪問:域名+端口+path
?
________________________________________________________________________________________
nginx 502錯誤解決方案
如下圖:
以下是我的做法,供參考:
1.首先,把后端代碼恢復到上一次沒報錯之前的版本,因為nginx報錯不一定絕對是nginx的問題,先把系統恢復成可以運行的狀態,這是控制變量法呀。
2.重新加載nginx的配置文件
nginx -c /usr/local/webserver/nginx/conf/nginx.conf如果你不能全局運行nginx命令,請加上nginx的路徑。-c后面的參數為nginx配置文件所在路徑這是要求nginx重新添加配置文件地址。
nginx -s reload重新加載文件
3.重新運行,可以看到會出現如下錯誤:
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()
可以看到,是進程端口被占用了,可以看系統是否已有nginx在運行。
運行命令:ps -aux | grep nginx
然后對照,發現第一個和第二個的pid分別為130616,130635,kill掉即可。
kill 130616 kill 130635問題解決,再次運行nginx,一切正常
總結
以上是生活随笔為你收集整理的Nginx安装环境配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云服务器 搭建单体redis 以及踩
- 下一篇: Nginx —— 检查配置文件ngi