1. 程式人生 > >Centos 7 安裝Nginx、設定為自啟動服務、部署vue靜態打包檔案筆記

Centos 7 安裝Nginx、設定為自啟動服務、部署vue靜態打包檔案筆記

A.vue專案打包
npm run build
B.Centos 7.0 安裝nginx
1.依賴安裝
yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
2.Nginx安裝
cd /usr/local
wget http://nginx.org/download/nginx-1.13.6.tar.gz
tar -zvxf nginx-1.13.6.tar.gz
cd nginx-1.13.6
./configure --with-http_ssl_module --with-http_gzip_static_module
make && make install

 配置資訊檢視

程式位置:/usr/local/nginx/sbin/nginx
配置檔案位置:/usr/local/nginx/conf/nginx.conf
檢視執行程序狀態:
ps aux | grep nginx

停止nginx:
./nginx -s stop

重啟nginx(配置檔案變動後需要重啟才能生效):
./nginx -s reload

檢查配置檔案是否正確:
./nginx -t

檢視nginx的pid:
cat /usr/local/nginx/logs/nginx.pid

檢視nginx版本

./nginx -v

回頭看編譯配置
./nginx -V


C.設定nginx為系統服務

1.新增系統配置檔案
vi /etc/init.d/nginx
新增如下內容
#! /bin/bash
# chkconfig: 2345 10 90
#StartupscriptforthenginxWebServer
#description:nginxisaWorldWideWebserver.Itisusedtoserve
#HTMLfilesandCGI.
#processname:nginx
#pidfile:/usr/local/nginx/logs/nginx.pid
#config:/usr/local/nginx/conf/nginx.conf
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0

2.設定執行許可權
chmod a+x /etc/init.d/nginx
3.註冊成服務
chkconfig --add nginx
4.設定開機啟動
chkconfig nginx on

對nginx服務執行停止/啟動/重新讀取配置檔案操作
#啟動nginx服務
systemctl start nginx.service
#停止nginx服務
systemctl stop nginx.service
#重啟nginx服務
systemctl restart nginx.service
#重新讀取nginx配置(這個最常用, 不用停止nginx服務就能使修改的配置生效)
systemctl reload nginx.service

D 釋出靜態資源html
1.將打包好的vue專案放到 /usr/local/webSpace/目錄下

2.修改nginx配置檔案
vi /usr/local/nginx/conf/nginx.conf
將 server-location-root- 修改為
/usr/local/webSpace/showTime/
3.重新載入nginx
systemctl reload nginx.service
大功告成