界面展示一下:
源码: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>
/// <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();
}
开启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 < 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/";
}
}
开启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版本,放到集成环境的目录下即可(改一下配置,重启)