虛擬機每次啓動之後都要重新啓動一下 nginx 和fastdfs服務,比較麻煩,所以增加開機自啓動。
1.編輯 /etc/rc.d/rc.local 文件,增加啓動項
vim /etc/rc.d/rc.local
2.改爲如下:
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
# fastdfs start
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
# nginx start
/usr/local/nginx/sbin/nginx
3.在centos7中, /etc/rc.d/rc.local 文件的權限被降低了,需要給rc.local 文件增加可執行的權限
chmod +x /etc/rc.d/rc.local
4.但是重啓虛擬機後,再次重啓nginx會報錯:
open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)
5.解決方案
進入 cd /usr/local/nginx/conf/ 目錄,編輯配置文件nginx.conf ,在配置文件中有個註釋的地方: #pid logs/nginx.pid,它默認是被註釋上的。我們把這個註釋取消,並修改爲:pid /usr/local/nginx/logs/nginx.pid
接着保存完配置文件後,在 /usr/local/nginx 目錄下創建 logs 目錄:mkdir /usr/local/nginx/logs
使用命令測試nginx服務:
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx/sbin/nginx -s quit
如果沒有問題。重啓虛擬機進行測試
成功。