1. 程式人生 > >線上編輯php.ini並且修改後無需任何操作配置會生效

線上編輯php.ini並且修改後無需任何操作配置會生效

**1.**下載安裝inotify (監控檔案)

伺服器上安裝inotify,執行如下命令
wget https://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar xzvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
cd ..
安裝完畢 建立軟連線
ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0

**2.**設定指令碼,檔案修改時php-fpm自動重啟

在程式碼釋出伺服器上以root身份建立inotify_rsync.sh指令碼
mkdir /root/script
vi /root/script/inotify_rsync.sh 輸入如下程式碼:
#!/bin/sh
SRC=/usr/local/php/etc/php.ini   #程式碼釋出伺服器目錄
/usr/local/bin/inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w%f %e' --exclude "(.swp|.swx|.svn)" \
-e create,move,delete,close_write,attrib $SRC | while read files
do
	service php-fpm restart
done

然後賦予指令碼可執行許可權
chmod +x /root/script/inotify_rsync.sh
設定開機自啟動 echo "/root/inotify_rsync.sh &" >> /etc/rc.local
執行指令碼/root/script/inotify_rsync.sh &

**3.**給php.ini可執行許可權。還要再/usr/local/nginx/conf/fastcgi.conf中加入php.ini的路由或直接加上usr。

**4.**建一個php檔案,讀取php.ini並展示在文字框中,程式碼如下:

<?php
$handle = fopen("/usr/local/php/etc/php.ini", "rb");
//$handle = fopen("./hh.php","rb");
$contents = '';
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
}
fclose($handle);
?>
<html>
        <form action="./put.php" method="post"
                <textarea style="width:800px;height:550px;" name="file" ><?php echo $contents; ?> </textarea>
<button type='submit'>
    儲存
</button>
        </form>
</html>

**5.**建另一個php檔案接受文字框資料,例如put.php如下:


<?php
$file=$_POST['file'];
$res=file_put_contents('/usr/local/php/etc/php.ini',$file);
if($res){
        echo '修改成功';


}else{
        echo '修改失敗';
}