Skip to content

Commit

Permalink
Merge pull request #96 from genesisweb/nginx-fixes
Browse files Browse the repository at this point in the history
Nginx fixes & Mailhog replacement
  • Loading branch information
uttamrabadiya authored Apr 23, 2023
2 parents 06f1457 + 5f457e7 commit 5379118
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 60 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
ij_php_align_key_value_pairs = true
ij_php_align_phpdoc_param_names = true
ij_php_align_phpdoc_comments = true
ij_php_concat_spaces = false

[*.md]
trim_trailing_whitespace = false
Expand Down
5 changes: 5 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
risky: false
preset: recommended
monolithic: true
tab-width: 4
use-tabs: false
Binary file removed bin/mailhog
Binary file not shown.
47 changes: 29 additions & 18 deletions cli/Valet/Mailhog.php → cli/Valet/Mailpit.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
use Valet\Contracts\PackageManager;
use Valet\Contracts\ServiceManager;

class Mailhog
class Mailpit
{
public $pm;
public $sm;
public $cli;
public $files;

const SERVICE_NAME = 'mailpit';

/**
* Create a new MailHog instance.
*
Expand Down Expand Up @@ -42,7 +44,18 @@ public function install()

$this->createService();

$this->sm->start('mailhog');
$this->sm->start(self::SERVICE_NAME);

if (!$this->sm->disabled('mailhog')) {
$this->sm->disable('mailhog');
if ($this->files->exists('/opt/valet-linux/mailhog')) {
$this->files->remove('/opt/valet-linux/mailhog');
}
$domain = \Configuration::get('domain');
if ($this->files->exists(VALET_HOME_PATH."/Nginx/mailhog.$domain")) {
\Site::proxyDelete("mailhog.$domain");
}
}
}

/**
Expand All @@ -53,9 +66,7 @@ public function install()
public function ensureInstalled()
{
if (!$this->isAvailable()) {
$optDir = '/opt/valet-linux';
$this->files->ensureDirExists($optDir);
$this->cli->run('ln -s '.VALET_BIN_PATH.'/mailhog /opt/valet-linux/mailhog');
$this->cli->run('sudo bash < <(curl -sL https://raw.githubusercontent.com/axllent/mailpit/develop/install.sh)');
}
}

Expand All @@ -64,15 +75,15 @@ public function ensureInstalled()
*/
public function createService()
{
info('Installing MailHog service...');
info('Installing Mailpit service...');

$servicePath = '/etc/init.d/mailhog';
$serviceFile = VALET_ROOT_PATH.'/cli/stubs/init/mailhog.sh';
$servicePath = '/etc/init.d/mailpit';
$serviceFile = VALET_ROOT_PATH.'/cli/stubs/init/mailpit.sh';
$hasSystemd = $this->sm->_hasSystemd();

if ($hasSystemd) {
$servicePath = '/etc/systemd/system/mailhog.service';
$serviceFile = VALET_ROOT_PATH.'/cli/stubs/init/mailhog';
$servicePath = '/etc/systemd/system/mailpit.service';
$serviceFile = VALET_ROOT_PATH.'/cli/stubs/init/mailpit';
}

$this->files->put(
Expand All @@ -84,7 +95,7 @@ public function createService()
$this->cli->run("chmod +x $servicePath");
}

$this->sm->enable('mailhog');
$this->sm->enable(self::SERVICE_NAME);

$this->updateDomain();
}
Expand All @@ -96,9 +107,9 @@ public function createService()
*/
public function updateDomain()
{
$domain = \Configuration::read()['domain'];
$domain = \Configuration::get('domain');

\Site::proxyCreate("mailhog.{$domain}", 'http://localhost:8025', true);
\Site::proxyCreate("mails.$domain", 'http://localhost:8025', true);
}

/**
Expand All @@ -108,7 +119,7 @@ public function isAvailable()
{
try {
$output = $this->cli->run(
'which mailhog',
'which mailpit',
function () {
throw new DomainException('Service not available');
}
Expand All @@ -127,7 +138,7 @@ function () {
*/
public function start()
{
$this->sm->start('mailhog');
$this->sm->start(self::SERVICE_NAME);
}

/**
Expand All @@ -137,7 +148,7 @@ public function start()
*/
public function restart()
{
$this->sm->restart('mailhog');
$this->sm->restart(self::SERVICE_NAME);
}

/**
Expand All @@ -147,7 +158,7 @@ public function restart()
*/
public function stop()
{
$this->sm->stop('mailhog');
$this->sm->stop(self::SERVICE_NAME);
}

/**
Expand All @@ -157,7 +168,7 @@ public function stop()
*/
public function status()
{
$this->sm->printStatus('mailhog');
$this->sm->printStatus(self::SERVICE_NAME);
}

/**
Expand Down
30 changes: 15 additions & 15 deletions cli/Valet/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public function replaceOldLoopbackWithNew($siteConf, $old, $new)
}

/**
* @param $site
* @param $site
* @param null $suffix
*
* @return string|null
Expand Down Expand Up @@ -624,15 +624,16 @@ public function buildSecureNginxServer($url, $stub = null)

return str_array_replace(
[
'VALET_HOME_PATH' => $this->valetHomePath(),
'VALET_SERVER_PATH' => VALET_SERVER_PATH,
'VALET_STATIC_PREFIX' => VALET_STATIC_PREFIX,
'VALET_SITE' => $url,
'VALET_CERT' => $path.'/'.$url.'.crt',
'VALET_KEY' => $path.'/'.$url.'.key',
'VALET_HTTP_PORT' => $this->config->get('port', 80),
'VALET_HTTPS_PORT' => $this->config->get('https_port', 443),
'VALET_REDIRECT_PORT' => $this->httpsSuffix(),
'VALET_HOME_PATH' => $this->valetHomePath(),
'VALET_SERVER_PATH' => VALET_SERVER_PATH,
'VALET_STATIC_PREFIX' => VALET_STATIC_PREFIX,
'VALET_SITE' => $url,
'VALET_CERT' => $path.'/'.$url.'.crt',
'VALET_KEY' => $path.'/'.$url.'.key',
'VALET_HTTP_PORT' => $this->config->get('port', 80),
'VALET_HTTPS_PORT' => $this->config->get('https_port', 443),
'VALET_REDIRECT_PORT' => $this->httpsSuffix(),
'VALET_FPM_SOCKET_FILE' => \PhpFpm::fpmSocketFile(\PhpFpm::getCurrentVersion()),
],
$stub
);
Expand Down Expand Up @@ -752,7 +753,7 @@ public function getSites($path, $certs)
*/
public function getPhpVersion($url)
{
$defaultPhpVersion = PHP_VERSION;
$defaultPhpVersion = \PhpFpm::getCurrentVersion();
$phpVersion = \PhpFpm::normalizePhpVersion($this->customPhpVersion($url));
if (empty($phpVersion)) {
$phpVersion = \PhpFpm::normalizePhpVersion($defaultPhpVersion);
Expand Down Expand Up @@ -825,7 +826,7 @@ public function isolate($url, $phpVersion, $secure = false)

// Isolate specific variables
$siteConf = str_array_replace([
'VALET_PHP_FPM_SOCKET' => \PhpFpm::socketFileName($phpVersion),
'VALET_FPM_SOCKET_FILE' => \PhpFpm::fpmSocketFile($phpVersion),
'VALET_ISOLATED_PHP_VERSION' => $phpVersion,
], $this->files->get($stub));

Expand All @@ -848,8 +849,7 @@ public function removeIsolation($valetSite)
// If a site has an SSL certificate, we need to keep its custom config file, but we can
// just re-generate it without defining a custom `valet.sock` file
if ($this->files->exists($this->certificatesPath().'/'.$valetSite.'.crt')) {
$siteConf = $this->buildSecureNginxServer($valetSite);
$this->files->putAsUser($this->nginxPath($valetSite), $siteConf);
$this->createSecureNginxServer($valetSite);
} else {
// When site doesn't have SSL, we can remove the custom nginx config file to remove isolation
$this->files->unlink($this->nginxPath($valetSite));
Expand Down Expand Up @@ -995,7 +995,7 @@ private function prepareConf(string $url, bool $requireSecure = false)
$stub = $this->files->get($stub);
// Isolate specific variables
return str_array_replace([
'VALET_PHP_FPM_SOCKET' => \PhpFpm::socketFileName($phpVersion),
'VALET_FPM_SOCKET_FILE' => \PhpFpm::fpmSocketFile($phpVersion),
'VALET_ISOLATED_PHP_VERSION' => $phpVersion,
], $stub);
}
Expand Down
7 changes: 6 additions & 1 deletion cli/Valet/Traits/PhpFpmHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ private function installExtensions($version)
$this->pm->ensureInstalled(implode(' ', $extArray));
}

public function fpmSocketFile($version)
{
return VALET_HOME_PATH.'/'.$this->socketFileName($version);
}

/**
* Update the PHP FPM configuration to use the current user.
*
Expand All @@ -93,7 +98,7 @@ private function installConfiguration($version)
str_array_replace([
'VALET_USER' => user(),
'VALET_GROUP' => group(),
'VALET_FPM_SOCKET_FILE' => VALET_HOME_PATH.'/'.$this->socketFileName($version),
'VALET_FPM_SOCKET_FILE' => $this->fpmSocketFile($version),
], $contents)
);
}
Expand Down
3 changes: 2 additions & 1 deletion cli/includes/facades.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class Ngrok extends Facade
* @method static void stop($version = null)
* @method static void status($version = null)
* @method static string getCurrentVersion()
* @method static string fpmSocketFile($version)
* @method static string socketFileName($version = null)
* @method static string normalizePhpVersion($version)
* @method static Collection isolatedDirectories()
Expand Down Expand Up @@ -246,7 +247,7 @@ class Requirements extends Facade
* @method static void status()
* @method static void uninstall()
*/
class Mailhog extends Facade
class Mailpit extends Facade
{
}

Expand Down
11 changes: 0 additions & 11 deletions cli/stubs/init/mailhog

This file was deleted.

11 changes: 11 additions & 0 deletions cli/stubs/init/mailpit
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Mailpit
After=network.target

[Service]
User=root
Group=root
ExecStart=/usr/local/bin/mailpit

[Install]
WantedBy=multi-user.target
16 changes: 8 additions & 8 deletions cli/stubs/init/mailhog.sh → cli/stubs/init/mailpit.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#!/bin/sh
### BEGIN INIT INFO
# Provides: mailhog
# Provides: mailpit
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts mailhog service
# Description: starts mailhog Resolution using start-stop-daemon
# Short-Description: starts mailpit service
# Description: starts mailpit Resolution using start-stop-daemon
### END INIT INFO

# Original Author: Uttam Rabadiya
# Maintainer: Uttam Rabadiya

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PIDFILE=/opt/valet-linux/mailhog.pid
LOGFILE=/opt/valet-linux/mailhog.log
DAEMON=/opt/valet-linux/mailhog
NAME=mailhog
DESC="Mailhog Service"
PIDFILE=/opt/valet-linux/mailpit.pid
LOGFILE=/opt/valet-linux/mailpit.log
DAEMON=/usr/local/bin/mailpit
NAME=mailpit
DESC="Mailpit Service"

STOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/5/TERM/5/KILL/5}"

Expand Down
3 changes: 2 additions & 1 deletion cli/stubs/isolated.valet.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# ISOLATED_PHP_VERSION=VALET_ISOLATED_PHP_VERSION
server {
listen VALET_HTTP_PORT;
listen 88;
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
root /;
charset utf-8;
Expand All @@ -28,7 +29,7 @@ server {

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass "unix:VALET_HOME_PATH/VALET_PHP_FPM_SOCKET";
fastcgi_pass "unix:VALET_FPM_SOCKET_FILE";
fastcgi_index VALET_SERVER_PATH;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME VALET_SERVER_PATH;
Expand Down
1 change: 1 addition & 0 deletions cli/stubs/proxy.valet.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

server {
listen VALET_HTTP_PORT;
listen 88;
#listen VALET_LOOPBACK:80; # valet loopback
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
root /;
Expand Down
2 changes: 1 addition & 1 deletion cli/stubs/secure.isolated.valet.conf
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ server {

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass "unix:VALET_HOME_PATH/VALET_PHP_FPM_SOCKET";
fastcgi_pass "unix:VALET_FPM_SOCKET_FILE";
fastcgi_index VALET_SERVER_PATH;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME VALET_SERVER_PATH;
Expand Down
4 changes: 2 additions & 2 deletions cli/stubs/secure.valet.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ server {

server {
listen VALET_HTTPS_PORT ssl http2;
listen 88;
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
root /;
charset utf-8;
Expand Down Expand Up @@ -33,7 +34,7 @@ server {

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:VALET_HOME_PATH/valet.sock;
fastcgi_pass unix:VALET_FPM_SOCKET_FILE;
fastcgi_index VALET_SERVER_PATH;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME VALET_SERVER_PATH;
Expand All @@ -43,4 +44,3 @@ server {
deny all;
}
}

1 change: 1 addition & 0 deletions cli/stubs/valet.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
server {
listen VALET_PORT default_server;
listen 88;
root /;
charset utf-8;
client_max_body_size 128M;
Expand Down
4 changes: 2 additions & 2 deletions cli/valet.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Create the application.
*/
Container::setInstance(new Container());
const VALET_VERSION = 'v1.6.4';
const VALET_VERSION = 'v1.6.5';

$app = new Application('ValetLinux+', VALET_VERSION);

Expand All @@ -41,7 +41,7 @@
PhpFpm::install();
DnsMasq::install(Configuration::read()['domain']);
Valet::symlinkToUsersBin();
Mailhog::install();
Mailpit::install();
ValetRedis::install();
Nginx::restart();
Mysql::install($mariaDB);
Expand Down

0 comments on commit 5379118

Please sign in to comment.