Skip to content

Commit

Permalink
#105: Use named arguments in vhost templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Rombauts committed Jun 26, 2019
1 parent 3af5126 commit e71a8cf
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 63 deletions.
16 changes: 8 additions & 8 deletions bin/.files/vhosts/apache.conf
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<VirtualHost *:%3$s>
ServerAdmin webmaster@%1$s.test
DocumentRoot %2$s
ServerName %1$s.test
ServerAlias www.%1$s.test %1$s.dev www.%1$s.dev
<VirtualHost *:%http_port%>
ServerAdmin webmaster@%site%.test
DocumentRoot %root%
ServerName %site%.test
ServerAlias www.%site%.test %site%.dev www.%site%.dev

<Directory %2$s>
<Directory %root%>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

ErrorLog /var/log/apache2/%1$s.test-error_log
CustomLog /var/log/apache2/%1$s.test-access_log common
ErrorLog /var/log/apache2/%site%.test-error_log
CustomLog /var/log/apache2/%site%.test-access_log common
</VirtualHost>
20 changes: 10 additions & 10 deletions bin/.files/vhosts/apache.ssl.conf
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<IfModule mod_ssl.c>
<VirtualHost *:%3$s>
ServerAdmin webmaster@%1$s.test
DocumentRoot %2$s
ServerName %1$s.test
ServerAlias www.%1$s.test %1$s.dev www.%1$s.dev
<VirtualHost *:%ssl_port%>
ServerAdmin webmaster@%site%.test
DocumentRoot %root%
ServerName %site%.test
ServerAlias www.%site%.test %site%.dev www.%site%.dev

<Directory %2$s>
<Directory %root%>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

SSLEngine on
SSLCertificateFile %4$s
SSLCertificateKeyFile %5$s
SSLCertificateFile %certificate%
SSLCertificateKeyFile %key%

ErrorLog /var/log/apache2/%1$s.test-error_log
CustomLog /var/log/apache2/%1$s.test-access_log common
ErrorLog /var/log/apache2/%site%.test-error_log
CustomLog /var/log/apache2/%site%.test-access_log common
</VirtualHost>
</IfModule>
12 changes: 6 additions & 6 deletions bin/.files/vhosts/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
server {
listen *:%3$s;
server_name %1$s.test www.%1$s.test %1$s.dev www.%1$s.dev;
root %2$s;
listen *:%http_port%;
server_name %site%.test www.%site%.test %site%.dev www.%site%.dev;
root %root%;
index index.html index.htm index.php;

access_log /var/log/nginx/%1$s.access.log;
error_log /var/log/nginx/%1$s.error.log;
access_log /var/log/nginx/%site%.access.log;
error_log /var/log/nginx/%site%.error.log;

location / {
try_files $uri $uri/ /index.php?$args;
Expand All @@ -15,7 +15,7 @@ server {
include /etc/nginx/fastcgi_params;
try_files $uri = 400;

fastcgi_pass %4$s;
fastcgi_pass %php_fpm%;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_buffer_size 32k;
Expand Down
16 changes: 8 additions & 8 deletions bin/.files/vhosts/nginx.ssl.conf
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
server {
listen *:%3$s ssl;
server_name %1$s.test www.%1$s.test %1$s.dev www.%1$s.dev;
root %2$s;
listen *:%ssl_port% ssl;
server_name %site%.test www.%site%.test %site%.dev www.%site%.dev;
root %root%;
index index.html index.htm index.php;

access_log /var/log/nginx/%1$s.access.log;
error_log /var/log/nginx/%1$s.error.log;
access_log /var/log/nginx/%site%.access.log;
error_log /var/log/nginx/%site%.error.log;

ssl on;
ssl_certificate %5$s;
ssl_certificate_key %6$s;
ssl_certificate %certificate%;
ssl_certificate_key %key%;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
Expand All @@ -24,7 +24,7 @@ server {
include /etc/nginx/fastcgi_params;
try_files $uri = 400;

fastcgi_pass %4$s;
fastcgi_pass %php_fpm%;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_buffer_size 32k;
Expand Down
74 changes: 43 additions & 31 deletions src/Joomlatools/Console/Command/Vhost/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
parent::execute($input, $output);

$site = $input->getArgument('site');
$port = $input->getOption('http-port');
$path = realpath(__DIR__.'/../../../../../bin/.files/');
$tmp = '/tmp/vhost.tmp';

$variables = $this->_getVariables($input);

if (is_dir('/etc/apache2/sites-available'))
{
$template = file_get_contents($path.'/vhosts/apache.conf');
$documentroot = Util::isPlatform($this->target_dir) ? $this->target_dir . '/web/' : $this->target_dir;

file_put_contents($tmp, sprintf($template, $site, $documentroot, $port));
$template = file_get_contents($path.'/vhosts/apache.conf');

if (!$input->getOption('disable-ssl'))
{
$ssl_crt = $input->getOption('ssl-crt');
$ssl_key = $input->getOption('ssl-key');
$ssl_port = $input->getOption('ssl-port');

if (file_exists($ssl_crt) && file_exists($ssl_key))
{
$template = "\n\n" . file_get_contents($path.'/vhosts/apache.ssl.conf');
file_put_contents($tmp, sprintf($template, $site, $documentroot, $ssl_port, $ssl_crt, $ssl_key), FILE_APPEND);
if (file_exists($input->getOption('ssl-crt')) && file_exists($input->getOption('ssl-key'))) {
$template .= "\n\n" . file_get_contents($path.'/vhosts/apache.ssl.conf');
}
else $output->writeln('<comment>SSL was not enabled for the site. One or more certificate files are missing.</comment>');
}

$vhost = str_replace(array_keys($variables), array_values($variables), $template);

file_put_contents($tmp, $vhost);

`sudo tee /etc/apache2/sites-available/1-$site.conf < $tmp`;
`sudo a2ensite 1-$site.conf`;
`sudo /etc/init.d/apache2 restart > /dev/null 2>&1`;
Expand All @@ -106,42 +102,58 @@ protected function execute(InputInterface $input, OutputInterface $output)

if (is_dir('/etc/nginx/sites-available'))
{
$socket = $input->getOption('php-fpm-address');

if (Util::isJoomlatoolsBox() && $port == 80) {
$port = 81;
if (Util::isJoomlatoolsBox() && $variables['%http_port%'] == 80) {
$variables['%http_port%'] = 81;
}

$file = Util::isKodekitPlatform($this->target_dir) ? 'nginx.kodekit.conf' : 'nginx.conf';

$template = file_get_contents($path.'/vhosts/'.$file);
$documentroot = Util::isPlatform($this->target_dir) ? $this->target_dir . '/web/' : $this->target_dir;

file_put_contents($tmp, sprintf($template, $site, $documentroot, $port, $socket));
$template = file_get_contents($path.'/vhosts/'.$file);

if (!$input->getOption('disable-ssl'))
{
$ssl_crt = $input->getOption('ssl-crt');
$ssl_key = $input->getOption('ssl-key');
$ssl_port = $input->getOption('ssl-port');

if (Util::isJoomlatoolsBox() && $ssl_port == 443) {
$ssl_port = 444;
if (Util::isJoomlatoolsBox() && $variables['%ssl_port%'] == 443) {
$variables['%ssl_port%'] = 444;
}

if (file_exists($ssl_crt) && file_exists($ssl_key))
{
$template = "\n\n" . file_get_contents($path.'/vhosts/nginx.kodekit.ssl.conf');
file_put_contents($tmp, sprintf($template, $site, $documentroot, $ssl_port, $socket, $ssl_crt, $ssl_key), FILE_APPEND);
if (file_exists($input->getOption('ssl-crt')) && file_exists($input->getOption('ssl-key'))) {
$template .= "\n\n" . file_get_contents($path.'/vhosts/nginx.ssl.conf');
}
else $output->writeln('<comment>SSL was not enabled for the site. One or more certificate files are missing.</comment>');
}

$vhost = str_replace(array_keys($variables), array_values($variables), $template);

file_put_contents($tmp, $vhost);

`sudo tee /etc/nginx/sites-available/1-$site.conf < $tmp`;
`sudo ln -fs /etc/nginx/sites-available/1-$site.conf /etc/nginx/sites-enabled/1-$site.conf`;
`sudo /etc/init.d/nginx restart > /dev/null 2>&1`;

@unlink($tmp);
}
}

protected function _getVariables(InputInterface $input)
{
$documentroot = Util::isPlatform($this->target_dir) ? $this->target_dir . '/web/' : $this->target_dir;

$variables = array(
'%site%' => $input->getArgument('site'),
'%root%' => $documentroot,
'%http_port%' => $input->getOption('http-port'),
'%php_fpm%' => $input->getOption('php-fpm-address')
);

if (!$input->getOption('disable-ssl'))
{
$variables = array_merge($variables, array(
'%ssl_port%' => $input->getOption('ssl-port'),
'%certificate%' => $input->getOption('ssl-crt'),
'%key%' => $input->getOption('ssl-key')
));
}

return $variables;
}
}

0 comments on commit e71a8cf

Please sign in to comment.