|
1、为了给同一个应用项目动态配置多个域名访问,把apache服务器换成了nginx,在/etc/nginx/conf.d/下配置域名命名的配置文件
#所有访问80端口的请求都重写到443 server {
listen 80;
server_name xxx.com www.xxx.com;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 443 ssl;
server_name xxx.com www.xxx.com;
#ssl on;
ssl_certificate xxx.crt; #crt文件
ssl_certificate_key xxx.key; #key文件
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
#charset koi8-r;
root /var/www/html;
location / {
index index.html index.htm index.php; #tp5的路由重新
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
2、php动态上传证书文件完毕的时候,生成域名命名的配置文件,需注意配置文件带$符号的字符串保持原样
$cmd = "echo '$str' > /etc/nginx/conf.d/$file_name";
exec($cmd);
常用执行shell脚本的php命令:
- exec — 执行一个外部程序
- shell_exec — 通过 shell 环境执行命令,并且将完整的输出以字符串的方式返回。
- system — 执行外部程序,并且显示输出
执行shell脚本的php命令详解:https://www.php.net/exec
3、重启nginx服务器
exec('service nginx restart');
发现不生效,这是linux用户权限的问题。系统服务默认只有root用户有权限,所以需要以root用户的身份去执行nginx的重启,此时百度的关键词为linux sudo
4、php的执行用户配置在/etc/php-fpm.d/www.conf,一般是apache或nginx用户和用户组,此处是apache,编辑/etc/sudoers文件,添加以下红色一行:
## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
apache ALL=(root) NOPASSWD: /usr/sbin/service nginx restart
5、此时php调用重启nginx的命令变成:
exec('sudo service nginx restart');
6、发现生效了,但是生效的同时由于nginx重启了,这个请求哦豁了,所以想到定时计划,linux的atd就可以只执行一次定时任务就停止了。此时该百度的词就是linux at了
at 命令参数
at [参数] [时间]
-m:当指定的任务被完成之后,将给用户发送邮件,即使没有标准输出
-I:atq的别名
-d:atrm的别名
-v:显示任务将被执行的时间
-c:打印任务的内容到标准输出
-V:显示版本信息
-q:使用指定队列
-f:从指定文件读入任务,而不是从标准输入读入
-t:一时间参数的形式提交要运行的任务
是不是就两种方式读入任务啊?
//exec('sudo service nginx restart');
exec(at -f "xxx.txt" now + 3 min); //3分钟后执行一次xxx.txt文件里面的命令,xxx.txt里面就可以放service nginx restart了
7、最后发现不执行,问了一下别人才知道,atd服务需要可以登录的用户才能执行,所以,又可以学习一下linux用户管理啦?
#打开 /etc/passwd,把apache修改为如下:
apache:x:48:48:Apache:/usr/share/httpd:/bin/bash
来源:https://www.cnblogs.com/zhylioooo/p/13892650.html |