1.下載源碼
cd /usr/local/src/
前往nginx官網選擇合適版本
wget http://nginx.org/download/
下載成功
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
訪問也成功了