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
