自做Windows上界面美观的PHP集成环境软件
来源:SegmentFault
时间:2023-01-13 21:30:02 342浏览 收藏
小伙伴们对数据库编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《自做Windows上界面美观的PHP集成环境软件》,就很适合你,本篇文章讲解的知识点主要包括MySQL、nginx、PHP、c#、wpf。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!
界面展示一下:
源码:SalamanderWnmp
集成包下载 ,关于这个软件的讲座,自己做个Nginx+PHP+MySQL的集成环境
原因
平常工作中用Nginx比较多,网上虽然也有wnmp集成环境,但是感觉界面不好看,用起来不舒服,所有决定自己做一个吧。
特点
免安装,界面简洁
原料
软件用的是C#,GUI框架是WPF(这个做出来更好看一点),先去官网下载PHP,用的是NTS版本的(因为这里PHP是以CGi的形式跑的),再去下载Windows版的Nginx和Mysql
代码
基类(BaseProgram.cs)
public abstract class BaseProgram: INotifyPropertyChanged { ////// exe 执行文件位置 /// public string exeFile { get; set; } ////// 进程名称 /// public string procName { get; set; } ////// 进程别名,用来在日志窗口显示 /// public string programName { get; set; } ////// 进程工作目录(Nginx需要这个参数) /// public string workingDir { get; set; } ////// 进程日志前缀 /// public Log.LogSection progLogSection { get; set; } ////// 进程开启的参数 /// public string startArgs { get; set; } ////// 关闭进程参数 /// public string stopArgs { get; set; } ////// /// public bool killStop { get; set; } ////// 进程配置目录 /// public string confDir { get; set; } ////// 进程日志目录 /// public string logDir { get; set; } ////// 进程异常退出的记录信息 /// 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")); } } } ////// 设置状态 /// public void SetStatus() { if (IsRunning()) { this.Running = true; } else { this.Running = false; } } ////// 启动进程 /// /// /// /// 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); } ////// 杀死进程 /// /// 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); } ////// 判断程序是否运行 /// ///public virtual bool IsRunning() { var processes = Process.GetProcessesByName(procName); return (processes.Length != 0); } /// /// 设置初始参数 /// public abstract void Setup(); }
开启mysql代码(Mysql.cs)
class MysqlProgram : BaseProgram { private readonly ServiceController mysqlController = new ServiceController(); public const string ServiceName = "mysql-salamander"; public MysqlProgram() { mysqlController.MachineName = Environment.MachineName; mysqlController.ServiceName = ServiceName; } ////// 移除服务 /// public void RemoveService() { StartProcess("cmd.exe", stopArgs); } ////// 安装服务 /// public void InstallService() { StartProcess(exeFile, startArgs); } ////// 获取my.ini中mysql的端口 /// ///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; } /// /// 服务是否存在 /// ///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); } } /// /// 通过ServiceController判断服务是否在运行 /// ///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/"; } /// /// 打开MySQL Client命令行 /// 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(); } }
开启php代码(PHP.cs)
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 { DecreaseFCGINum(); }); IncreaseFCGINum(); } WatchPHPFCGINum(); Log.wnmp_log_notice("Started " + programName, progLogSection); } } ////// 异步查看php-cgi数量 /// /// ///private void WatchPHPFCGINum() { watchPHPFCGI = true; watchThread = new Thread(() => { while (watchPHPFCGI) { uint delta = Common.Settings.PHPProcesses.Value - FCGI_NUM; for (int i = 0; i { 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/"; } }
开启nginx(Nginx.cs)
这里要注意WorkingDirectory属性设置成nginx目录
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/"; } ////// 打开命令行 /// 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扩展
编程语言面板
注意
php 版本为7.1.12 64位版本,需要MSVC14 (Visual C++ 2015)运行库支持,下载:https://download.microsoft.co...
其实用户完全可以选择自己想要的php版本,放到集成环境的目录下即可(改一下配置,重启)
理论要掌握,实操不能落!以上关于《自做Windows上界面美观的PHP集成环境软件》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
499 收藏
-
244 收藏
-
235 收藏
-
157 收藏
-
101 收藏
最新阅读
更多>
-
209 收藏
-
497 收藏
-
335 收藏
-
467 收藏
-
303 收藏
-
176 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习