当前位置:首页 > 文章列表 > 数据库 > MySQL > 自做Windows上界面美观的PHP集成环境软件

自做Windows上界面美观的PHP集成环境软件

来源:SegmentFault 2023-01-13 21:30:02 0浏览 收藏

小伙伴们对数据库编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《自做Windows上界面美观的PHP集成环境软件》,就很适合你,本篇文章讲解的知识点主要包括MySQL、nginx、PHP、c#、wpf。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

界面展示一下:

UI界面

源码:SalamanderWnmp
集成包下载 ,关于这个软件的讲座,自己做个Nginx+PHP+MySQL的集成环境

原因

平常工作中用Nginx比较多,网上虽然也有wnmp集成环境,但是感觉界面不好看,用起来不舒服,所有决定自己做一个吧。

特点

免安装,界面简洁

原料

软件用的是C#,GUI框架是WPF(这个做出来更好看一点),先去官网下载PHP,用的是NTS版本的(因为这里PHP是以CGi的形式跑的),再去下载Windows版的NginxMysql

代码

基类(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扩展

clipboard.png

clipboard.png
编程语言面板

编程语言面板

注意

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源并安装MySQLCentOS 7使用yum配置MySQL源并安装MySQL
上一篇
CentOS 7使用yum配置MySQL源并安装MySQL
MySQL在大数据、高并发场景下的SQL语句优化和"最佳实践"。
下一篇
MySQL在大数据、高并发场景下的SQL语句优化和"最佳实践"。
查看更多
最新文章
查看更多
课程推荐
  • 前端进阶之JavaScript设计模式
    前端进阶之JavaScript设计模式
    设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
    542次学习
  • GO语言核心编程课程
    GO语言核心编程课程
    本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
    508次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    497次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    484次学习
查看更多
AI推荐
  • 笔灵AI生成答辩PPT:高效制作学术与职场PPT的利器
    笔灵AI生成答辩PPT
    探索笔灵AI生成答辩PPT的强大功能,快速制作高质量答辩PPT。精准内容提取、多样模板匹配、数据可视化、配套自述稿生成,让您的学术和职场展示更加专业与高效。
    16次使用
  • 知网AIGC检测服务系统:精准识别学术文本中的AI生成内容
    知网AIGC检测服务系统
    知网AIGC检测服务系统,专注于检测学术文本中的疑似AI生成内容。依托知网海量高质量文献资源,结合先进的“知识增强AIGC检测技术”,系统能够从语言模式和语义逻辑两方面精准识别AI生成内容,适用于学术研究、教育和企业领域,确保文本的真实性和原创性。
    24次使用
  • AIGC检测服务:AIbiye助力确保论文原创性
    AIGC检测-Aibiye
    AIbiye官网推出的AIGC检测服务,专注于检测ChatGPT、Gemini、Claude等AIGC工具生成的文本,帮助用户确保论文的原创性和学术规范。支持txt和doc(x)格式,检测范围为论文正文,提供高准确性和便捷的用户体验。
    30次使用
  • 易笔AI论文平台:快速生成高质量学术论文的利器
    易笔AI论文
    易笔AI论文平台提供自动写作、格式校对、查重检测等功能,支持多种学术领域的论文生成。价格优惠,界面友好,操作简便,适用于学术研究者、学生及论文辅导机构。
    42次使用
  • 笔启AI论文写作平台:多类型论文生成与多语言支持
    笔启AI论文写作平台
    笔启AI论文写作平台提供多类型论文生成服务,支持多语言写作,满足学术研究者、学生和职场人士的需求。平台采用AI 4.0版本,确保论文质量和原创性,并提供查重保障和隐私保护。
    35次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码