界面展現一下:
php
源碼:SalamanderWnmp
集成包下載 ,關於這個軟件的講座,本身作個Nginx+PHP+MySQL的集成環境mysql
日常工做中用Nginx比較多,網上雖然也有wnmp集成環境,可是感受界面很差看,用起來不舒服,全部決定本身作一個吧。nginx
免安裝,界面簡潔git
軟件用的是C#,GUI框架是WPF(這個作出來更好看一點),先去官網下載PHP,用的是NTS版本的(由於這裏PHP是以CGi的形式跑的),再去下載Windows版的Nginx和Mysqlgithub
public abstract class BaseProgram: INotifyPropertyChanged { /// <summary> /// exe 執行文件位置 /// </summary> public string exeFile { get; set; } /// <summary> /// 進程名稱 /// </summary> public string procName { get; set; } /// <summary> /// 進程別名,用來在日誌窗口顯示 /// </summary> public string programName { get; set; } /// <summary> /// 進程工做目錄(Nginx須要這個參數) /// </summary> public string workingDir { get; set; } /// <summary> /// 進程日誌前綴 /// </summary> public Log.LogSection progLogSection { get; set; } /// <summary> /// 進程開啓的參數 /// </summary> public string startArgs { get; set; } /// <summary> /// 關閉進程參數 /// </summary> public string stopArgs { get; set; } /// <summary> /// /// </summary> public bool killStop { get; set; } /// <summary> /// 進程配置目錄 /// </summary> public string confDir { get; set; } /// <summary> /// 進程日誌目錄 /// </summary> public string logDir { get; set; } /// <summary> /// 進程異常退出的記錄信息 /// </summary> protected string errOutput = ""; public Process ps = new Process(); public event PropertyChangedEventHandler PropertyChanged; // 是否在運行 private bool running = false; public bool Running { get { return this.running; } set { this.running = value; if(PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Running")); } } } /// <summary> /// 設置狀態 /// </summary> public void SetStatus() { if (IsRunning()) { this.Running = true; } else { this.Running = false; } } /// <summary> /// 啓動進程 /// </summary> /// <param name="exe"></param> /// <param name="args"></param> /// <param name="wait"></param> public void StartProcess(string exe, string args, EventHandler exitedHandler = null) { ps = new Process(); ps.StartInfo.FileName = exe; ps.StartInfo.Arguments = args; ps.StartInfo.UseShellExecute = false; ps.StartInfo.RedirectStandardOutput = true; ps.StartInfo.RedirectStandardError = true; ps.StartInfo.WorkingDirectory = workingDir; ps.StartInfo.CreateNoWindow = true; ps.Start(); // ErrorDataReceived event signals each time the process writes a line // to the redirected StandardError stream ps.ErrorDataReceived += (sender, e) => { errOutput += e.Data; }; ps.Exited += exitedHandler != null ? exitedHandler : (sender, e) => { if (!String.IsNullOrEmpty(errOutput)) { Log.wnmp_log_error("Failed: " + errOutput, progLogSection); errOutput = ""; } }; ps.EnableRaisingEvents = true; ps.BeginOutputReadLine(); ps.BeginErrorReadLine(); } public virtual void Start() { if(IsRunning()) { return; } try { StartProcess(exeFile, startArgs); Log.wnmp_log_notice("Started " + programName, progLogSection); } catch (Exception ex) { Log.wnmp_log_error("Start: " + ex.Message, progLogSection); } } public virtual void Stop() { if(!IsRunning()) { return; } if (killStop == false) StartProcess(exeFile, stopArgs); var processes = Process.GetProcessesByName(procName); foreach (var process in processes) { process.Kill(); } Log.wnmp_log_notice("Stopped " + programName, progLogSection); } /// <summary> /// 殺死進程 /// </summary> /// <param name="procName"></param> protected void KillProcess(string procName) { var processes = Process.GetProcessesByName(procName); foreach (var process in processes) { process.Kill(); } } public void Restart() { this.Stop(); this.Start(); Log.wnmp_log_notice("Restarted " + programName, progLogSection); } /// <summary> /// 判斷程序是否運行 /// </summary> /// <returns></returns> public virtual bool IsRunning() { var processes = Process.GetProcessesByName(procName); return (processes.Length != 0); } /// <summary> /// 設置初始參數 /// </summary> public abstract void Setup(); }
class MysqlProgram : BaseProgram { private readonly ServiceController mysqlController = new ServiceController(); public const string ServiceName = "mysql-salamander"; public MysqlProgram() { mysqlController.MachineName = Environment.MachineName; mysqlController.ServiceName = ServiceName; } /// <summary> /// 移除服務 /// </summary> public void RemoveService() { StartProcess("cmd.exe", stopArgs); } /// <summary> /// 安裝服務 /// </summary> public void InstallService() { StartProcess(exeFile, startArgs); } /// <summary> /// 獲取my.ini中mysql的端口 /// </summary> /// <returns></returns> private static int GetIniMysqlListenPort() { string path = Common.APP_STARTUP_PATH + Common.Settings.MysqlDirName.Value + "/my.ini"; Regex regPort = new Regex(@"^\s*port\s*=\s*(\d+)"); Regex regMysqldSec = new Regex(@"^\s*\[mysqld\]"); using (var sr = new StreamReader(path)) { bool isStart = false;// 是否找到了"[mysqld]" string str = null; while ((str = sr.ReadLine()) != null) { if (isStart && regPort.IsMatch(str)) { MatchCollection matches = regPort.Matches(str); foreach (Match match in matches) { GroupCollection groups = match.Groups; if (groups.Count > 1) { try { return Int32.Parse(groups[1].Value); } catch { return -1; } } } } // [mysqld]段開始 if (regMysqldSec.IsMatch(str)) { isStart = true; } } } return -1; } /// <summary> /// 服務是否存在 /// </summary> /// <returns></returns> public bool ServiceExists() { ServiceController[] services = ServiceController.GetServices(); foreach (var service in services) { if (service.ServiceName == ServiceName) return true; } return false; } public override void Start() { if (IsRunning()) return; try { if (!File.Exists(Common.APP_STARTUP_PATH + Common.Settings.MysqlDirName.Value + "/my.ini")) { Log.wnmp_log_error("my.ini file not exist", progLogSection); return; } int port = GetIniMysqlListenPort();// -1表示提取出錯 if (port != -1 && PortScanHelper.IsPortInUseByTCP(port)) { Log.wnmp_log_error("Port " + port + " is used", progLogSection); return; } mysqlController.Start(); Log.wnmp_log_notice("Started " + programName, progLogSection); } catch (Exception ex) { Log.wnmp_log_error("Start(): " + ex.Message, progLogSection); } } public override void Stop() { if(!IsRunning()) { return; } try { mysqlController.Stop(); mysqlController.WaitForStatus(ServiceControllerStatus.Stopped); Log.wnmp_log_notice("Stopped " + programName, progLogSection); } catch (Exception ex) { Log.wnmp_log_error("Stop(): " + ex.Message, progLogSection); } } /// <summary> /// 經過ServiceController判斷服務是否在運行 /// </summary> /// <returns></returns> public override bool IsRunning() { mysqlController.Refresh(); try { return mysqlController.Status == ServiceControllerStatus.Running; } catch { return false; } } public override void Setup() { this.exeFile = Common.APP_STARTUP_PATH + String.Format("{0}/bin/mysqld.exe", Common.Settings.MysqlDirName.Value); this.procName = "mysqld"; this.programName = "MySQL"; this.workingDir = Common.APP_STARTUP_PATH + Common.Settings.MysqlDirName.Value; this.progLogSection = Log.LogSection.WNMP_MARIADB; this.startArgs = "--install-manual " + MysqlProgram.ServiceName + " --defaults-file=\"" + Common.APP_STARTUP_PATH + String.Format("\\{0}\\my.ini\"", Common.Settings.MysqlDirName.Value); this.stopArgs = "/c sc delete " + MysqlProgram.ServiceName; this.killStop = true; this.confDir = "/mysql/"; this.logDir = "/mysql/data/"; } /// <summary> /// 打開MySQL Client命令行 /// </summary> public static void OpenMySQLClientCmd() { Process ps = new Process(); ps.StartInfo.FileName = Common.APP_STARTUP_PATH + String.Format("{0}/bin/mysql.exe", Common.Settings.MysqlDirName.Value); ps.StartInfo.Arguments = String.Format("-u{0} -p{1}", Common.Settings.MysqlClientUser.Value, Common.Settings.MysqlClientUserPass.Value); ps.StartInfo.UseShellExecute = false; ps.StartInfo.CreateNoWindow = false; ps.Start(); } }
class PHPProgram : BaseProgram { private const string PHP_CGI_NAME = "php-cgi"; private const string PHP_MAX_REQUEST = "PHP_FCGI_MAX_REQUESTS"; private Object locker = new Object(); private uint FCGI_NUM = 0; private bool watchPHPFCGI = true; private Thread watchThread; private void DecreaseFCGINum() { lock (locker) { FCGI_NUM--; } } private void IncreaseFCGINum() { lock (locker) { FCGI_NUM++; } } public PHPProgram() { if (Environment.GetEnvironmentVariable(PHP_MAX_REQUEST) == null) Environment.SetEnvironmentVariable(PHP_MAX_REQUEST, "300"); } public override void Start() { if(!IsRunning() && PortScanHelper.IsPortInUseByTCP(Common.Settings.PHP_Port.Value)) { Log.wnmp_log_error("Port " + Common.Settings.PHP_Port.Value + " is used", progLogSection); } else if(!IsRunning()) { for (int i = 0; i < Common.Settings.PHPProcesses.Value; i++) { StartProcess(this.exeFile, this.startArgs, (s, args) => { DecreaseFCGINum(); }); IncreaseFCGINum(); } WatchPHPFCGINum(); Log.wnmp_log_notice("Started " + programName, progLogSection); } } /// <summary> /// 異步查看php-cgi數量 /// </summary> /// <param name="ct"></param> /// <returns></returns> private void WatchPHPFCGINum() { watchPHPFCGI = true; watchThread = new Thread(() => { while (watchPHPFCGI) { uint delta = Common.Settings.PHPProcesses.Value - FCGI_NUM; for (int i = 0; i < delta; i++) { StartProcess(this.exeFile, this.startArgs, (s, args) => { DecreaseFCGINum(); }); IncreaseFCGINum(); Console.WriteLine("restart a php-cgi"); } } }); watchThread.Start(); } public void StopWatchPHPFCGINum() { watchPHPFCGI = false; } public override void Stop() { if (!IsRunning()) { return; } StopWatchPHPFCGINum(); KillProcess(PHP_CGI_NAME); Log.wnmp_log_notice("Stopped " + programName, progLogSection); } public override void Setup() { string phpDirPath = Common.APP_STARTUP_PATH + Common.Settings.PHPDirName.Value; this.exeFile = string.Format("{0}/php-cgi.exe", phpDirPath); this.procName = PHP_CGI_NAME; this.programName = "PHP"; this.workingDir = phpDirPath; this.progLogSection = Log.LogSection.WNMP_PHP; this.startArgs = String.Format("-b 127.0.0.1:{0} -c {1}/php.ini", Common.Settings.PHP_Port.Value, phpDirPath); this.killStop = true; this.confDir = "/php/"; this.logDir = "/php/logs/"; } }
這裏要注意WorkingDirectory屬性設置成nginx目錄redis
class NginxProgram : BaseProgram { public override void Setup() { this.exeFile = Common.APP_STARTUP_PATH + String.Format("{0}/nginx.exe", Common.Settings.NginxDirName.Value); this.procName = "nginx"; this.programName = "Nginx"; this.workingDir = Common.APP_STARTUP_PATH + Common.Settings.NginxDirName.Value; this.progLogSection = Log.LogSection.WNMP_NGINX; this.startArgs = ""; this.stopArgs = "-s stop"; this.killStop = false; this.confDir = "/conf/"; this.logDir = "/logs/"; } /// <summary> /// 打開命令行 /// </summary> public static void OpenNginxtCmd() { Process ps = new Process(); ps.StartInfo.FileName = "cmd.exe"; ps.StartInfo.Arguments = ""; ps.StartInfo.UseShellExecute = false; ps.StartInfo.CreateNoWindow = false; ps.StartInfo.WorkingDirectory = Common.APP_STARTUP_PATH + Common.Settings.NginxDirName.Value; ps.Start(); } }
配置nginx,php,mysql目錄名,管理php擴展sql
php 版本爲7.1.12 64位版本,須要MSVC14 (Visual C++ 2015)運行庫支持,下載:https://download.microsoft.co...
其實用戶徹底能夠選擇本身想要的php版本,放到集成環境的目錄下便可(改一下配置,重啓)編程