自做Windows上界面美观的PHP集成环境软件
来源:SegmentFault
2023-01-13 21:30:02
0浏览
收藏
小伙伴们对数据库编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《自做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 { /// <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 name="args"> /// <param name="wait"> 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"> 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(); }
开启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; } /// <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(); } }
开启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); } } /// <summary> /// 异步查看php-cgi数量 /// </summary> /// <param name="ct"> /// <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 { 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/"; } /// <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扩展


编程语言面板

注意
php 版本为7.1.12 64位版本,需要MSVC14 (Visual C++ 2015)运行库支持,下载:https://download.microsoft.co...
其实用户完全可以选择自己想要的php版本,放到集成环境的目录下即可(改一下配置,重启)
理论要掌握,实操不能落!以上关于《自做Windows上界面美观的PHP集成环境软件》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
版本声明
本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除

- 上一篇
- CentOS 7使用yum配置MySQL源并安装MySQL

- 下一篇
- MySQL在大数据、高并发场景下的SQL语句优化和"最佳实践"。
查看更多
最新文章
-
- 数据库 · MySQL | 36秒前 |
- MySQL安装到D盘教程及路径设置详解
- 279浏览 收藏
-
- 数据库 · MySQL | 1小时前 |
- MySQL缓存设置及查询作用解析
- 470浏览 收藏
-
- 数据库 · MySQL | 5小时前 |
- MySQLcount优化技巧及性能提升方法
- 371浏览 收藏
-
- 数据库 · MySQL | 6小时前 |
- MySQLUPDATE替换字段值方法详解
- 292浏览 收藏
-
- 数据库 · MySQL | 8小时前 |
- MySQL基础:增删改查全教程
- 356浏览 收藏
-
- 数据库 · MySQL | 8小时前 |
- MySQL建表语法详解与实例教程
- 498浏览 收藏
-
- 数据库 · MySQL | 10小时前 |
- MySQL中文界面设置方法详解
- 356浏览 收藏
-
- 数据库 · MySQL | 19小时前 |
- MySQL安装后如何启动和连接
- 233浏览 收藏
-
- 数据库 · MySQL | 21小时前 |
- MySQL中WHERE与HAVING的区别详解
- 259浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- MySQL数据备份方法与策略详解
- 112浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- MySQL中HAVING和WHERE的区别
- 363浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- MySQL数据归档方法与工具推荐
- 372浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 511次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 498次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
查看更多
AI推荐
-
- 千音漫语
- 千音漫语,北京熠声科技倾力打造的智能声音创作助手,提供AI配音、音视频翻译、语音识别、声音克隆等强大功能,助力有声书制作、视频创作、教育培训等领域,官网:https://qianyin123.com
- 88次使用
-
- MiniWork
- MiniWork是一款智能高效的AI工具平台,专为提升工作与学习效率而设计。整合文本处理、图像生成、营销策划及运营管理等多元AI工具,提供精准智能解决方案,让复杂工作简单高效。
- 83次使用
-
- NoCode
- NoCode (nocode.cn)是领先的无代码开发平台,通过拖放、AI对话等简单操作,助您快速创建各类应用、网站与管理系统。无需编程知识,轻松实现个人生活、商业经营、企业管理多场景需求,大幅降低开发门槛,高效低成本。
- 96次使用
-
- 达医智影
- 达医智影,阿里巴巴达摩院医疗AI创新力作。全球率先利用平扫CT实现“一扫多筛”,仅一次CT扫描即可高效识别多种癌症、急症及慢病,为疾病早期发现提供智能、精准的AI影像早筛解决方案。
- 90次使用
-
- 智慧芽Eureka
- 智慧芽Eureka,专为技术创新打造的AI Agent平台。深度理解专利、研发、生物医药、材料、科创等复杂场景,通过专家级AI Agent精准执行任务,智能化工作流解放70%生产力,让您专注核心创新。
- 87次使用
查看更多
相关文章
-
- golang MySQL实现对数据库表存储获取操作示例
- 2022-12-22 499浏览
-
- 搞一个自娱自乐的博客(二) 架构搭建
- 2023-02-16 244浏览
-
- B-Tree、B+Tree以及B-link Tree
- 2023-01-19 235浏览
-
- mysql面试题
- 2023-01-17 157浏览
-
- MySQL数据表简单查询
- 2023-01-10 101浏览