- import os
- class Moments:
- def __init__(self):
- print("Hello Moments");
- pass
- defconfigApache24(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")
- forlinein 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")
- forlinein 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")
- ifos.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路径
- forlinein f:
- if 'c:/Apache24' in line:
- #line = "#"+line+"\n"+"ServerRoot "+os.path.abspath(".").replace("\\","/")+"/"+pathnameline = 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("\\","/"))
- ifos.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")
来源: