開機自啓動nginx,php-fpm

 

開機自啓動nginx,php-fpm(其餘服務相似)

centos 7以上是用Systemd進行系統初始化的,Systemd 是 Linux 系統中最新的初始化系統(init),它主要的設計目標是克服 sysvinit 固有的缺點,提升系統的啓動速度。 Systemd服務文件以.service結尾,好比如今要創建nginx爲開機啓動,若是用yum install命令安裝的,yum命令會自動建立nginx.service文件,直接用命令systemcel enable nginx.service設置開機啓動便可。php

systemcel enable nginx.service

源碼安裝的手動創建nginx.service服務文件nginx

  1. 在系統服務目錄裏建立nginx.service文件
    vi /lib/systemd/system/nginx.service
    寫入如下內容(路徑改爲本身的)
    [Unit]
    Description=nginx
    After=network.target
    [Service]
    Type=forking
    ExecStart=/www/lnmp/nginx/sbin/nginx -c /www/lnmp/nginx/conf/nginx.conf
    ExecReload=/www/lnmp/nginx/sbin/nginx -s reload
    ExecStop=/www/lnmp/nginx/sbin/nginx -s quit
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target

     

    在系統服務目錄裏建立php-fpm.service文件
    vi /lib/systemd/system/php-fpm.service
    寫入如下內容(路徑改爲本身的)
    [Unit]
    Description=php-fpm
    After=network.target
    [Service]
    Type=forking
    ExecStart=/www/lnmp/php/sbin/php-fpm
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target

     

    [Unit]:服務的說明
    Description:描述服務
    After:描述服務類別
    [Service]服務運行參數的設置
    Type=forking是後臺運行的形式
    ExecStart爲服務的具體運行命令
    ExecReload爲重啓命令
    ExecStop爲中止命令
    PrivateTmp=True表示給服務分配獨立的臨時空間
    注意:[Service]的啓動、重啓、中止命令所有要求使用絕對路徑
    [Install]運行級別下服務安裝的相關設置,可設置爲多用戶,即系統運行級別爲3centos

  2. 測試並加入開機自啓
    先關閉nginx,php-fpm
    使用如下命令開啓
    systemctl start nginx.service             #若是服務是開啓狀態,使用此命令會啓動失敗。
    systemctl start php-fpm.service

     

    開啓成功,將服務加入開機自啓
    systemctl enable nginx.service                #注意後面不能跟空格
    systemctl enable php-fpm.service

     

    重啓服務器,查看是否啓動
    shutdown -r now        #重啓
    systemctl list-units --type=service           #查看運行的服務

     



 
其餘命令
systemctl start nginx.service              #啓動nginx服務
systemctl enable nginx.service             #設置開機自啓動
systemctl disable nginx.service            #中止開機自啓動
systemctl status nginx.service             #查看服務當前狀態
systemctl restart nginx.service           #從新啓動服務
systemctl list-units --type=service        #查看全部已啓動的服務

 

轉載:https://blog.csdn.net/ijijni/article/details/78513521
相關文章
相關標籤/搜索