1.下載下傳源碼
cd /usr/local/src/
前往nginx官網選擇合适版本
wget http://nginx.org/download/
下載下傳成功
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX10keNpXTE1keJpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2LcRHelR3LcJzLctmch1mclRXY39zNyQjNwMTM5EDMzYDM4EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
2.安裝
解壓
tar -zxvf nginx-1.9.9.tar.gz
進入源碼目錄
cd nginx-1.9.9
配置
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
編譯
make && make install
3.使用
進入安裝目錄
cd /usr/local/nginx/sbin
啟動nginx
./nginx
重新開機nginx
./nginx -s reload
關閉nginx
./nginx -s stop
4.設定開機啟動
vi /lib/systemd/system/nginx.service
寫入下面的資料
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
關閉nginx
/usr/local/nginx/sbin/nginx -s quit
使用以下指令開啟
systemctl start nginx.service
将服務加入開機自啟
systemctl enable nginx.service
5.配置
cd /usr/local/nginx/conf
vi nginx.conf
在檔案末尾增加
include vhost/*.conf
建立vhost檔案夾
mkdir vhost
建立一個測試配置檔案
cd vhost
vi test81.conf
寫入
server {
listen 81;
server_name localhost;
root "/home/wwwroot/test81";
location / {
index index.html index.htm index.php l.php;
autoindex off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
重新開機nginx
/usr/local/nginx/sbin/nginx -s reload
建立配置檔案裡的 root "/home/wwwroot/test81"; 目錄
index.php寫入
通過浏覽器通路失敗??
本機可以用curl 通路到,證明不是nginx 的問題
那麼就可能是防火牆的問題了,如上圖,找不到iptables
停止并屏蔽firewalld服務
systemctl stop firewalld
systemctl mask firewalld
安裝iptables-services軟體包
yum -y install iptables-services
開機自啟動iptables服務
systemctl enable iptables
啟動服務
systemctl start iptables
儲存防火牆規則
service iptables save
測試一下停用防火牆看能通路不
systemctl stop iptables
通路成功了,但這并不是我們想要的(因為我們把防火牆關閉了)
開啟防火牆
systemctl start iptables
配置開放端口
vi /etc/sysconfig/iptables
加入
-A INPUT -p tcp -m tcp --dport 80-81 -j ACCEPT
到檔案内(80-81代表 80到81的端口,這裡隻包含了80和81)
重新開機
systemctl restart iptables
通路也成功了