Linux環境安裝nginx方法及nginx配置教程
LINUX安裝nginx詳細步驟
1.安裝依賴包
//一鍵安裝上面四個依賴
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2.下載并解壓安裝包
//創建一個文件夾
cd /usr/local
mkdir nginx
cd nginx
//下載tar包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz
3.安裝nginx
//進入nginx目錄
cd /usr/local/nginx
//進入目錄
cd nginx-1.13.7
//執行命令 考慮到后續安裝ssl證書 添加兩個模塊
./configure --with-http_stub_status_module --with-http_ssl_module
//執行make命令
make
//執行make install命令
make install
4.啟動nginx服務
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
配置nginx.conf
打開配置文件
vi /usr/local/nginx/conf/nginx.conf
將端口號改成8089(隨便挑個端口),因為可能apeache占用80端口,apeache端口盡量不要修改,我們選擇修改nginx端口。
將localhost修改為你服務器的公網ip地址。
5.重啟nginx
/usr/local/nginx/sbin/nginx -s reload
查看nginx進程是否啟動:
ps -ef | grep nginx
6.若想使用外部主機訪問nginx,需要關閉服務器防火墻或開放nginx服務端口,端口為上一步nginx.conf的配置端口:
centOS6及以前版本使用命令: systemctl stop iptables.service
centOS7關閉防火墻命令: systemctl stop firewalld.service
關閉防火墻會導致服務器有一定風險,所以建議是單獨開放服務端口 :
開放80端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent
查詢端口號80 是否開啟:
firewall-cmd --query-port=80/tcp
重啟防火墻:
firewall-cmd --reload
隨后訪問該ip:端口 即可看到nginx界面。
7.訪問服務器ip查看(備注,由于我監聽的仍是80端口,所以ip后面的端口號被省略)
安裝完成一般常用命令
進入安裝目錄中,
命令: cd /usr/local/nginx/sbin
啟動,關閉,重啟,命令:
./nginx 啟動
./nginx -s stop 關閉
./nginx -s reload 重啟
報錯
進入objs目錄下修改Makefile文件
CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter **- Werror** -g
將 **- Werror** 去掉
錯誤
安裝nginx源
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
先安裝
yum -y install epel-release
#應該是缺少modules模塊
yum -y install nginx-all-modules.noarch
然后在用nginx -t就好了
[root@k8s-node2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
添加使用–with-stream=dynamic 配置
./configure --prefix=/usr/local/nginx/ --with-stream=dynamic
然后make make install
make
make install
香港空間主機首選嘉裕云
嘉裕云(www.jyhost.com)提供簡單好用,價格厚道的香港/美國云服務器和獨立服務器。IDC+ISP+ICP資質。ARIN和APNIC會員。成熟技術團隊16年行業經驗。