php管理nginx虛擬主機(jī)shell腳本實(shí)例
本文實(shí)例講述了php管理nginx虛擬主機(jī)shell腳本,分享給大家供大家參考。具體分析如下:
使用php作為shell腳本是一件很方便的事情。理所當(dāng)然,我們可以使用php腳本來管理 nginx虛擬主機(jī),下面是筆者的 腳本 文件供各位參考:
<?php
start: fwrite(STDOUT,"===========Vhost Script===========\n");
fwrite(STDOUT,"= Choose an operation \n");
fwrite(STDOUT,"= 1.Create 2.Delete 3.Exit\n");
fwrite(STDOUT,"==================================\n");
$operate = trim(fgets(STDIN));
if ( $operate == 1 ){
fwrite(STDOUT,"Please Enter a Domain Name:");
$domain = trim(fgets(STDIN));
$path = "/home/sites/{$domain}";
$nginx_conf = "/etc/nginx/sites/{$domain}";
$nginx_template = "/etc/nginx/template/site_conf";
$apache_conf = "/etc/httpd/conf/httpd.conf";
$conf_str = "";
//變量初始化
if( file_exists($path ) ) exit ("Domain Existed!\n");
else mkdir($path,0700);
if(file_exists($nginx_conf)) exit ("Nginx Config file existed!\n");
else {
$conf_str = file_get_contents( $nginx_template );
}
//目錄檢測(cè)及配置文件拷貝
eval ( "\$conf_str = \"$conf_str\";" );
$succes = file_put_contents($nginx_conf,$conf_str);
if( !$succes ) exit ("Write Config File Fauile!");
else echo "Create Vhost success!\n";
goto start;
//寫入配置文件
}
else if ($operate == 2){
$confs_dir = dir("/etc/nginx/sites");
$confs_list = array();
$count = 0;
while ( false !== ( $conf_file = $confs_dir->read() ) ){
if( $conf_file == "." ) continue;
if( $conf_file == ".." ) continue;
if ( is_file( $confs_dir->path ."/". $conf_file) ) {
$confs_list[$count++] = $conf_file;
}
}
echo "Select a site by number which to delete:\n";
if( count( $confs_list ) >0 )
foreach ( $confs_list as $k=>$v ){
echo "{$k}. $v\n";
}
$index = trim(fgets(STDIN));
if( in_array ( $index,array_keys( $confs_list ) ) ){
copy( $confs_dir->path ."/". $confs_list[$index],"/etc/nginx/backup/{$confs_list[$index]}" );
unlink ( $confs_dir->path ."/". $confs_list[$index] );
exec("tar -zcf /home/sites/{$confs_list[$index]}.tar.gz /home/sites/".$confs_list[$index] );
exec("rm -Rf /home/sites/".$confs_list[$index]);
}
//刪除指定配置,并保存?zhèn)浞?br /> }
else if( $operate == 3 ) {
exit;
}
else {
exit ("No Operation Selected!");
}
?>
下面是nginx的配置模版
listen 80;
server_name {$domain};
access_log /var/log/nginx/{$domain}_access_log;
error_log /var/log/nginx/{$domain}_error_log;
root {$path};
#不記錄對(duì)站點(diǎn)圖標(biāo)訪問
location = /favicon.ico {
log_not_found off;
access_log off;
}
#不記錄對(duì)robots.txt的訪問
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location = / {
try_files @proxy;
}
location / {
index index.htm index.html index.php;
try_files \$uri @proxy;
}
#匹配html
location ~* \.(html|htm)$ {
expires 30s;
gzip off;
add_header Content-Encoding gzip;
try_files \$uri \$uri/ /wp-content/cache/supercache/\$http_host/\$request_uri/index.html.gz @proxy;
}
#匹配圖片、腳本文件等
location ~* \.(jpe?g|gif|png|ico|css|js|flv|swf|avi|zip|rar|svg|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mp3)$ {
expires 30d;
}
#傳遞給apache
location @proxy {
index index.htm index.html index.php;
proxy_pass http://127.0.0.1:81;
include /etc/nginx/proxy.conf;
}
}
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- 阿里云主機(jī)一鍵安裝lamp、lnmp環(huán)境的shell腳本分享
- Shell腳本實(shí)現(xiàn)自動(dòng)修改IP、主機(jī)名等功能分享
- NTFS權(quán)限設(shè)置以避免通過webshell遍歷主機(jī)目錄
- nginx虛擬主機(jī)防webshell完美版
- shell腳本實(shí)現(xiàn)批量測(cè)試局域網(wǎng)主機(jī)是否在線
- 虛擬主機(jī)封殺webshell提權(quán)!!!!!!!!!!
- 使用PowerShell獲取當(dāng)前主機(jī)內(nèi)存使用量和總量的方法
- PowerShell腳本實(shí)現(xiàn)檢測(cè)網(wǎng)絡(luò)內(nèi)主機(jī)類型
- Shell交互批量更改主機(jī)名的方法
相關(guān)文章
php中顯示數(shù)組與對(duì)象的實(shí)現(xiàn)代碼
數(shù)組是PHP中變量的一種,在PHP開發(fā)中經(jīng)常使用,因此非常重要,使用PHP語句打印數(shù)組內(nèi)容。2011-04-04
簡(jiǎn)單談?wù)凱HP中strlen 函數(shù)
在PHP中,函數(shù)strlen()返回字符串的長(zhǎng)度,strlen()函數(shù)返回字符串所占的字節(jié)長(zhǎng)度,一個(gè)英文字母、數(shù)字、各種符號(hào)均占一個(gè)字節(jié),它們的長(zhǎng)度均為12016-02-02
PHP實(shí)現(xiàn)本地圖片轉(zhuǎn)base64格式并上傳
這篇文章主要介紹了PHP實(shí)現(xiàn)本地圖片轉(zhuǎn)base64格式并上傳,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
深入for,while,foreach遍歷時(shí)間比較的詳解
本篇文章是對(duì)for,while,foreach遍歷時(shí)間比較進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP實(shí)現(xiàn)隨機(jī)發(fā)放撲克牌
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)隨機(jī)發(fā)放撲克牌,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
Windows下IIS6/Apache2.2.4+MySQL5.2+PHP5.2.1安裝配置方法
Windows下IIS6/Apache2.2.4+MySQL5.2+PHP5.2.1安裝配置方法...2007-05-05

