👉 寫在前面:Nginx 爲了更好發揮其併發性能,一般使用在 Linux 上。
1、準備工作
(1)打開虛擬機,使用遠程連接工具連接 linux 操作系統 。
如何在windows上使用 Xshell 連接 Linux,可以參照我這篇博客:Xshell連接到 Linux
(2)到 nginx 官網下載軟件:http://nginx.org/
(3)需要準備的素材:
pcre-8.37.tar.gz openssl-1.0.1t.tar.gz zlib-1.2.8.tar.gz nginx-1.11.1.tar.gz
2、開始進行 nginx 安裝
(1)安裝 pcre 依賴
第一步:聯網下載 pcre 壓縮文件依賴
wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
下載後的大小,
如果你發現你的下載文件大小不對,訪問:
https://sourceforge.net/p/pcre/activity/?page=1&limit=100#553fce622718464538c5fad4
選擇對應版本進行下載,
第二步: 解壓壓縮文件
使用命令,
tar –xvf pcre-8.37.tar.gz
完成後,
第三步:編譯
進入 pcre-8.37 執行./configure
,完成後,回到 pcre 目錄下執行 ,
make && make install
第四步:查看
查看 pcre 依賴是否安裝成功:
pcre-config --version
(2)安裝 openssl 、zlib 、 gcc 依賴
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
(3)安裝 nginx
第一步:解壓 nginx-1.12.2.tar.gz
tar -xvf nginx-1.12.2.tar.gz
第二步:進入解壓後的目錄
此時,裏面有一個文件 configure,執行
./configure
第三步:編譯
make && make install
3、啓動 Nginx
進入目錄 /usr/local/nginx/sbin/nginx
,執行【要換 root 用戶下執行】
./nginx
瀏覽器訪問:http://192.168.77.130:80
此時要在 linux 中關閉防火牆 。如果你不知道咋關閉防火牆可以接着往下看,參照下面命令進行關閉。
到這一步,就代表 Nginx 安裝成功 。
4、防火牆設置
windows 系統中訪問 linux 中 nginx,默認不能訪問的,因爲防火牆問題,需要設置
(1)關閉防火牆
(2)開放訪問的端口號,80 端口
Centos 6
查看防火牆狀態
service iptables status
開啓防火牆
service iptables start
關閉防火牆
service iptables stop
重啓防火牆
service iptables restart
防火牆開放某個端口
(1)查看防火牆當前設置,具體開發的端口號信息
/etc/init.d/iptables status
(2)配置防火牆策略
vim /etc/sysconfig/iptables
例如我要開22,80端口:
# Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT -A INPUT -j REJECT –reject-with icmp-host-prohibited -A FORWARD -j REJECT –reject-with icmp-host-prohibited COMMIT
保存退出!!!
重啓防火牆:service iptables restart
Centos 7
查看開放的端口號 firewall-cmd --list-all 設置開放的端口號 firewall-cmd --add-service=http –permanent firewall-cmd --add-port=80/tcp --permanent 重啓防火牆 firewall-cmd –reload
防火牆設置完成後,便可以在 windows 這邊進行訪問 nginx了 。