一.nginx配置自启:
1. 在系统服务目录里创建nginx.service文件
> vi /usr/lib/systemd/system/nginx.service
2. 写入下面内容
```bash
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
```
3.设置开机启动
> systemctl enable nginx.service
4、查看nginx状态
> systemctl status nginx.service
5.如果有出现Active: inactive (dead),可以执行下面命令
```bash
pkill -9 nginx
ps aux | grep nginx
systemctl start nginx
```
再次查看状态,变成了active了.
6.reboot重启后再次查看状态就ok了。
二.php-fpm配置自启
1. 在系统服务目录里创建php-fpm.service文件
> vim /etc/systemd/system/php-fpm.service
2. 写入下面内容
```bash
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/run/php-fpm.pid
ExecStart=/usr/sbin/php-fpm --nodaemonize --fpm-config /etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target
```
注:以上ExecStart后面的路径,可以通过whereis php-fpm来查看
3.启动php-fpm-
> systemctl start php-fpm.service
4.添加到开机启动
> systemctl enable php-fpm.service
注:如果出现Failed to execute operation: File exists报错可以用:systemctl disable php-fpm.service 命令清除掉
5.reboot重启后,查看状态.
</br>
`
附:systemctl命令
`
```bash
systemctl enable *.service #开机运行服务
systemctl disable *.service #取消开机运行
systemctl start *.service #启动服务
systemctl stop *.service #停止服务
systemctl restart *.service #重启服务
systemctl reload *.service #重新加载服务配置文件
systemctl status *.service #查询服务运行状态
systemctl --failed #显示启动失败的服务
修改 php.ini 文件 设置 expose_php = Off
vim /usr/local/php7/etc/php.ini
找到 expose_php = On
改为 expose_php = Off
```
nginx和php-fpm配置自启