目錄
1、下載nginx安裝包
wget http://nginx.org/download/nginx-1.17.0.tar.gz
2、解壓
tar -zxvf nginx-1.17.0.tar.gz
3、安裝依賴
yum -y install pcre-devel
yum -y install openssl openssl-devel
4、編譯安裝nginx
進入nginx-1.17.0目錄執行
./configure
make
make install
如果make命令出錯:"make:*** No targets specified and no makefile found.Stop."
①更新版本系統軟件:yum update
②編譯缺失關聯軟件:yum install gcc build-essential
然後在執行./configure生成makefile,再執行make,即可正常運行。
5、nginx的默認安裝位置爲
/usr/local/nginx
執行相關命令則需要進入sbin目錄
conf爲nginx配置文件位置
6、nginx啓動
進入安裝目錄,/usr/local/nginx/sbin,執行./nginx即可
7、測試是否安裝成功
瀏覽器輸入服務器ip看能否正常訪問
看到如上界面,說明nginx安裝成功!
如果修改了配置文件,只需執行./nginx -s reload即可,不需要停了再啓。
8、nginx相關命令
相關命令都是在/usr/local/nginx/sbin下執行
nginx -s reopen #重啓Nginx
nginx -s reload #重新加載Nginx配置文件,然後以優雅的方式重啓Nginx
nginx -s stop #強制停止Nginx服務
nginx -s quit #優雅地停止Nginx服務(即處理完所有請求後再停止服務)
nginx -t #檢測配置文件是否有語法錯誤,然後退出
nginx -?,-h #打開幫助信息
nginx -v #顯示版本信息並退出
nginx -V #顯示版本和配置選項信息,然後退出
nginx -t #檢測配置文件是否有語法錯誤,然後退出
nginx -T #檢測配置文件是否有語法錯誤,轉儲並退出
nginx -q #在檢測配置文件期間屏蔽非錯誤信息
nginx -p prefix #設置前綴路徑(默認是:/usr/share/nginx/)
nginx -c filename #設置配置文件(默認是:/etc/nginx/nginx.conf)
nginx -g directives #設置配置文件外的全局指令
killall nginx #殺死所有nginx進程
9、配置反向代理
假設我們要代理到百度,只需在location片段裏邊加proxy_pass http://www.baidu.com
修改完配置文件別忘了讓配置文件生效,執行
nginx -s reload
10、添加nginx到系統服務並設置開機啓動
①添加nginx文件 vim /etc/init.d/nginx
②編寫nginx執行腳本
#!/bin/bash
#Startup script for the nginx Web Server
#chkconfig: 2345 85 15
nginx=/usr/local/nginx/sbin/nginx
conf=/usr/local/nginx/conf/nginx.conf
case $1 in
start)
echo -n "Starting Nginx"
$nginx -c $conf
echo " done."
;;
stop)
echo -n "Stopping Nginx"
killall -9 nginx
echo " done."
;;
test)
$nginx -t -c $conf
echo "Success."
;;
reload)
echo -n "Reloading Nginx"
ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP
echo " done."
;;
restart)
$nginx -s reload
echo "reload done."
;;
*)
echo "Usage: $0 {start|restart|reload|stop|test|show}"
;;
esac
③給nginx執行權限:chmod a+x /etc/init.d/nginx
④將nginx服務加入chkconfig管理列表:chkconfig --add /etc/init.d/nginx
既可以使用如下命令進行相關操作
service nginx start
service nginx stop
service nginx restart
⑤設置nginx爲開機自啓動
chkconfig nginx on