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