程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP_環境配置_python腳本_2017,_python_2017

PHP_環境配置_python腳本_2017,_python_2017

編輯:關於PHP編程

PHP_環境配置_python腳本_2017,_python_2017


Apache配置

需要安裝:VC2015

httpd-2.4.16-win32-VC14.zip VC14就是2015的環境。

又比如:php-5.6.12-Win32-VC11-x86 VC11就是2012的環境。

如果沒有安裝對應的環境,在配置php + apache時,提示內容會很奇怪。

可能會提示"提示NET HELPMSG 3547"在

語句填寫正確的情況下,VC環境也是造成其原因之一,切記。

將Apache解壓到C:\www\Apache24\,進入C:\www\Apache24\bin

httpd.exe -k install -n "Apache24"
rem httpd.exe -k uninstall -n "Apache24"

遇到錯誤:

查看apache錯誤日志:目錄下的apache/logs/error.log

查看window系統日志:我的電腦——》右鍵管理——》系統工具——》事件查看器——》應用程序日志

在命令行中使用netstat -ano就可以很清楚的看到本地80口占用程序的PID    記下PID
然後打開任務管理器   在   進程    裡查找到PID所對應的進程

修改C:\www\Apache24\httpd.conf裡面的目錄路徑,將C: \Apache24更改為C:\www\Apache24

PHP配置

將php-5.6.12-Win32-VC11-x86.zip解壓到目錄,然後在httpd.conf中進行配置:

# php5 support
LoadModule php5_module "C: /php/php5apache2_4.dll"
AddHandler application/x-httpd-php .php
AddType application/x-httpd-php .html .htm
# configure thepath to php.ini
PHPIniDir " C:/php"

將上述行為寫成一個腳本可以方便下次配置,並且省去總是改目錄的問題。

MYSQL配置

[mysqld]
# These are commonly set, remove the # and set as required.
#mysql的根目錄
basedir = "E:/BaiduBack/BaiduBack/PHPENV/mysql"
#mysql的根目錄
datadir = "E:/data"
# 默認端口
port = 3306
# server_id = .....
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# 服務端的編碼方式
character-set-server = utf8
[client]
#客戶端編碼方式,最好和服務端保存一致
loose-default-character-set = utf8

注意:MySQL的管理員用戶名為root,密碼默認為空。

MySQL修改root密碼的多種方法

方法1: 用SET PASSWORD命令

mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');

方法2:用mysqladmin

mysqladmin -u root password "newpass"
如果root已經設置過密碼,采用如下方法
mysqladmin -u root password oldpass "newpass"

方法3: 用UPDATE直接編輯user表

mysql -u root
mysql> use mysql;
mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';
mysql> FLUSH PRIVILEGES;
在丟失root密碼的時候,可以這樣
mysqld_safe --skip-grant-tables&
mysql -u root mysql
mysql> UPDATE user SET password=PASSWORD("new password") WHERE user='root';
mysql> FLUSH PRIVILEGES;

如何在本地配置兩個站點:

讓Apache在啟動時能加載虛擬主機模塊。

打開Apache安裝目錄下conf/httpd.conf文件,找到下面兩行文字,把最前面的 # 號去掉,然後保存。

#LoadModule vhost_alias_module modules/mod_vhost_alias.so
#Include conf/extra/httpd-vhosts.conf   

接著找到同一文件中的DocumentRoot和Directory,改為站點目錄的上一級目錄

例如站點放在 D:/Appserv/www/1 和 D:/Appserv/www/2,則改為以下形式

DocumentRoot"D:/Appserv/www"
<Directory"D:/Appserv/www">

配置完成後即可打開Apache安裝目錄下/conf/extra/httpd-vhosts.conf文件,在最後添加如下:

DocumentRoot是文件放置路徑,ServerName是網站域名:

<VirtualHost*:80>
DocumentRoot"D:/Appserv/www/1"
ServerName  www.xxx.com
</VirtualHost>
<VirtualHost*:80>
DocumentRoot"D:/Appserv/www/2"
ServerName   www.xxx2.com
</VirtualHost>

PHP_環境配置_python腳本

import os

class Moments:
    def __init__(self):
        print("Hello Moments");
        pass
    def configApache24(self, pathname="Apache24"):
        if os.path.exists(pathname):
            print("Find path %s" % pathname)
        else:
            return false;

        f5 = open(os.path.abspath(".").replace("\\","/") + "/php/php.ini-production", "r").readlines()
        f6 = open(os.path.abspath(".").replace("\\","/") + "/php/php.ini", "w")
        for line in f5:
            if '''extension_dir = "ext"''' in line:
                line = """extension_dir = "ext"\n"""
            elif """;extension=php_mysql.dll""" in line:
                line = """extension=php_mysql.dll\n"""
            elif """;extension=php_mysqli.dll""" in line:
                line = """extension=php_mysqli.dll\n"""
            elif """;extension=php_pdo_mysql.dll""" in line:
                line = """extension=php_pdo_mysql.dll\n"""                   
            else:
                line = line
            f6.write(line)
        f6.close()
        

        f3 = open(os.path.abspath(".").replace("\\","/") + "/mysql/my-default.ini", "r").readlines()
        f4 = open(os.path.abspath(".").replace("\\","/") + "/mysql/my.ini", "w")

        for line in f3:
            if "basedir" in line:
                line = "basedir = %s\n" % (os.path.abspath(".").replace("\\","/") + "/mysql")
            elif "datadir" in line:
                line = "datadir = %s\n" % (os.path.abspath(".").replace("\\","/") + "/mysql/data")
            elif "port" in line:
                line = "port = 3306\n"
            else:
                line = line
            f4.write(line)
        f4.write("""# 服務端的編碼方式
character-set-server = utf8
[client]
#客戶端編碼方式,最好和服務端保存一致
loose-default-character-set = utf8""")
        f4.close()
        s = os.path.abspath(".").replace("\\","/") + "/mysql/bin/mysqld.exe --remove mysql"
        os.system(s)        
        s = os.path.abspath(".").replace("\\","/") + "/mysql/bin/mysqld.exe --install mysql"
        os.system(s)
        os.system("net stop mysql & net start mysql & pause")
        

        if os.path.exists(pathname + "/conf/httpd.conf.bak"):
            os.remove(pathname + "/conf/httpd.conf")
            #os.rename(pathname + "/conf/httpd.conf.bak", pathname + "/conf/httpd.conf")
        else:
            os.rename(pathname + "/conf/httpd.conf", pathname + "/conf/httpd.conf.bak")
        
        f = open(pathname + "/conf/httpd.conf.bak", "r").readlines()
        f2 = open(pathname + "/conf/httpd_new.conf", "w")

        
        #改變appache路徑
        for line in f:
            if 'c:/Apache24' in line:
                #line = "#"+line+"\n"+"ServerRoot "+os.path.abspath(".").replace("\\","/")+"/"+pathname
                line = line.replace("c:/Apache24", os.path.abspath(".").replace("\\","/")+"/"+pathname)
                #多個站點設置上級目錄(這裡直接寫成根目錄)
                line = line.replace("Apache24/htdocs", "")
            elif '<IfModule unixd_module>' in line:
                s_temp = """# php5 support

LoadModule php5_module "%s/php/php5apache2_4.dll"

AddHandler application/x-httpd-php .php
AddType application/x-httpd-php .html .htm

# configure thepath to php.ini

PHPIniDir "%s/php"

""" % (os.path.abspath(".").replace("\\","/"), os.path.abspath(".").replace("\\","/"))
                if os.path.exists(os.path.abspath(".").replace("\\","/")+"/php/php5apache2_4.dll"):
                    line = s_temp + line;
                else:
                    print("###ERROR:Can not find "+os.path.abspath(".").replace("\\","/")+"/php/php5apache2_4.dll")
            #這裡是多個站點設置
            elif 'mod_vhost_alias.so' in line:
                line = line.replace("#","")
            #這裡是多個站點設置
            elif 'httpd-vhosts.conf' in line:
                line = line.replace("#","")
            else:
                line = line
            f2.write(line);
        f2.close()
        #將apache24添加到服務
        s = os.path.abspath(".").replace("\\","/")+"/"+pathname+'/bin/httpd.exe -k uninstall -n "Apache24"'
        os.system(s)
        s = os.path.abspath(".").replace("\\","/")+"/"+pathname+'/bin/httpd.exe -k install -n "Apache24"'
        os.system(s)
        print (s)
        
        os.rename(pathname + "/conf/httpd_new.conf", pathname + "/conf/httpd.conf")
        

        f = open(pathname + "/conf/extra/httpd-vhosts.conf", "a")
        f.write("""
<VirtualHost *:80>
DocumentRoot "%swww_1_com"
ServerName   www.1.com
</VirtualHost>

<VirtualHost *:80>
DocumentRoot "%swww_2_com"
ServerName   www.2.com
</VirtualHost>
""" % (os.path.abspath(".").replace("\\","/"), os.path.abspath(".").replace("\\","/")))
        
        os.mkdir("%swww_1_com" % os.path.abspath("."))
        os.mkdir("%swww_2_com" % os.path.abspath("."))
        os.system("net stop apache24 & net start apache24 & pause")
            
if __name__ == "__main__":
    mo = Moments();
    mo.configApache24("Apache24");
    print "請修改hosts 127.0.0.1 www.1.com"
    print "請修改hosts 127.0.0.1 www.2.com"
    os.system("net stop apache24 & net start apache24 & pause")

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved