Skip to content

Commit fb95b9b

Browse files
committed
Added nginx-virtual-host command.
1 parent 4e75fc7 commit fb95b9b

File tree

5 files changed

+264
-1
lines changed

5 files changed

+264
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace DrupalCodeGenerator\Commands\Other;
4+
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
use DrupalCodeGenerator\Commands\BaseGenerator;
8+
9+
/**
10+
* Implements other:nginx-virtual-host command.
11+
*/
12+
class NginxVirtualHost extends BaseGenerator {
13+
14+
protected $name = 'other:nginx-virtual-host';
15+
protected $description = 'Generates an Nginx site configuration file';
16+
protected $alias = 'nginx-virtual-host';
17+
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
protected function interact(InputInterface $input, OutputInterface $output) {
22+
$questions = [
23+
'server_name' => ['Server name', 'example.com'],
24+
'docroot' => ['Document root', [$this, 'defaultDocumentRoot']],
25+
];
26+
27+
$vars = $this->collectVars($input, $output, $questions);
28+
29+
$this->files[$vars['server_name']] = $this->render('other/nginx-virtual-host.twig', $vars);
30+
}
31+
32+
/**
33+
* Returns default answer for docroot question.
34+
*/
35+
protected function defaultDocumentRoot($vars) {
36+
return '/var/www/' . $vars['server_name'] . '/docroot';
37+
}
38+
39+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#
2+
# @DCG: {
3+
# The configuration is based on official Nginx recipe.
4+
# See https://www.nginx.com/resources/wiki/start/topics/recipes/drupal/
5+
#
6+
# Check out Perusio's config for more delicate configuration.
7+
# See https://github.com/perusio/drupal-with-nginx
8+
# @DCG: }
9+
#
10+
server {
11+
server_name {{ server_name }};
12+
root {{ docroot }};
13+
14+
location = /favicon.ico {
15+
log_not_found off;
16+
access_log off;
17+
}
18+
19+
location = /robots.txt {
20+
allow all;
21+
log_not_found off;
22+
access_log off;
23+
}
24+
25+
# Very rarely should these ever be accessed.
26+
location ~* \.(make|txt|log|engine|inc|info|install|module|profile|po|pot|sh|sql|test|theme)$ {
27+
return 404;
28+
}
29+
30+
location ~ \..*/.*\.php$ {
31+
return 404;
32+
}
33+
34+
location ~ ^/sites/.*/private/ {
35+
return 403;
36+
}
37+
38+
# Allow "Well-Known URIs" as per RFC 5785
39+
location ~* ^/.well-known/ {
40+
allow all;
41+
}
42+
43+
# Block access to "hidden" files and directories whose names begin with a
44+
# period. This includes directories used by version control systems such
45+
# as Subversion or Git to store control files.
46+
location ~ (^|/)\. {
47+
return 404;
48+
}
49+
50+
location / {
51+
try_files $uri /index.php?$query_string;
52+
}
53+
54+
# Don't allow direct access to PHP files in the vendor directory.
55+
location ~ /vendor/.*\.php$ {
56+
deny all;
57+
return 404;
58+
}
59+
60+
# In Drupal 8, we must also match new paths where the '.php' appears in
61+
# the middle, such as update.php/selection. The rule we use is strict,
62+
# and only allows this pattern with the update.php front controller.
63+
# This allows legacy path aliases in the form of
64+
# blog/index.php/legacy-path to continue to route to Drupal nodes. If
65+
# you do not have any paths like that, then you might prefer to use a
66+
# laxer rule, such as:
67+
# location ~ \.php(/|$) {
68+
# The laxer rule will continue to work if Drupal uses this new URL
69+
# pattern with front controllers other than update.php in a future
70+
# release.
71+
location ~ '\.php$|^/update.php' {
72+
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
73+
# Security note: If you're running a version of PHP older than the
74+
# latest 5.3, you should have "cgi.fix_pathinfo = 0;" in php.ini.
75+
# See http://serverfault.com/q/627903/94922 for details.
76+
include fastcgi_params;
77+
# Block httpoxy attacks. See https://httpoxy.org/.
78+
fastcgi_param HTTP_PROXY "";
79+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
80+
fastcgi_param PATH_INFO $fastcgi_path_info;
81+
fastcgi_intercept_errors on;
82+
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
83+
}
84+
85+
# Fighting with Styles? This little gem is amazing.
86+
location ~ ^/sites/.*/files/styles/ {
87+
try_files $uri @rewrite;
88+
}
89+
90+
# Handle private files through Drupal.
91+
location ~ ^/system/files/ {
92+
try_files $uri /index.php?$query_string;
93+
}
94+
95+
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
96+
expires max;
97+
log_not_found off;
98+
}
99+
}

src/Tests/GeneratorDiscoveryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
class GeneratorsDiscoveryTest extends \PHPUnit_Framework_TestCase {
1212

13-
const TOTAL_GENERATORS = 53;
13+
const TOTAL_GENERATORS = 54;
1414

1515
/**
1616
* Test callback.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace DrupalCodeGenerator\Tests\Other;
4+
5+
use DrupalCodeGenerator\Tests\GeneratorTestCase;
6+
7+
/**
8+
* Test for other:nginx-virtual-host command.
9+
*/
10+
class NginxVirtualHostTest extends GeneratorTestCase {
11+
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public function setUp() {
16+
$this->class = 'Other\NginxVirtualHost';
17+
$this->answers = [
18+
'example.local',
19+
'/var/www/example.local/docroot',
20+
];
21+
$this->target = 'example.local';
22+
$this->fixture = __DIR__ . '/_nginx_virtual_host';
23+
parent::setUp();
24+
}
25+
26+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#
2+
# @DCG: {
3+
# The configuration is based on official Nginx recipe.
4+
# See https://www.nginx.com/resources/wiki/start/topics/recipes/drupal/
5+
#
6+
# Check out Perusio's config for more delicate configuration.
7+
# See https://github.com/perusio/drupal-with-nginx
8+
# @DCG: }
9+
#
10+
server {
11+
server_name example.local;
12+
root /var/www/example.local/docroot;
13+
14+
location = /favicon.ico {
15+
log_not_found off;
16+
access_log off;
17+
}
18+
19+
location = /robots.txt {
20+
allow all;
21+
log_not_found off;
22+
access_log off;
23+
}
24+
25+
# Very rarely should these ever be accessed.
26+
location ~* \.(make|txt|log|engine|inc|info|install|module|profile|po|pot|sh|sql|test|theme)$ {
27+
return 404;
28+
}
29+
30+
location ~ \..*/.*\.php$ {
31+
return 404;
32+
}
33+
34+
location ~ ^/sites/.*/private/ {
35+
return 403;
36+
}
37+
38+
# Allow "Well-Known URIs" as per RFC 5785
39+
location ~* ^/.well-known/ {
40+
allow all;
41+
}
42+
43+
# Block access to "hidden" files and directories whose names begin with a
44+
# period. This includes directories used by version control systems such
45+
# as Subversion or Git to store control files.
46+
location ~ (^|/)\. {
47+
return 404;
48+
}
49+
50+
location / {
51+
try_files $uri /index.php?$query_string;
52+
}
53+
54+
# Don't allow direct access to PHP files in the vendor directory.
55+
location ~ /vendor/.*\.php$ {
56+
deny all;
57+
return 404;
58+
}
59+
60+
# In Drupal 8, we must also match new paths where the '.php' appears in
61+
# the middle, such as update.php/selection. The rule we use is strict,
62+
# and only allows this pattern with the update.php front controller.
63+
# This allows legacy path aliases in the form of
64+
# blog/index.php/legacy-path to continue to route to Drupal nodes. If
65+
# you do not have any paths like that, then you might prefer to use a
66+
# laxer rule, such as:
67+
# location ~ \.php(/|$) {
68+
# The laxer rule will continue to work if Drupal uses this new URL
69+
# pattern with front controllers other than update.php in a future
70+
# release.
71+
location ~ '\.php$|^/update.php' {
72+
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
73+
# Security note: If you're running a version of PHP older than the
74+
# latest 5.3, you should have "cgi.fix_pathinfo = 0;" in php.ini.
75+
# See http://serverfault.com/q/627903/94922 for details.
76+
include fastcgi_params;
77+
# Block httpoxy attacks. See https://httpoxy.org/.
78+
fastcgi_param HTTP_PROXY "";
79+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
80+
fastcgi_param PATH_INFO $fastcgi_path_info;
81+
fastcgi_intercept_errors on;
82+
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
83+
}
84+
85+
# Fighting with Styles? This little gem is amazing.
86+
location ~ ^/sites/.*/files/styles/ {
87+
try_files $uri @rewrite;
88+
}
89+
90+
# Handle private files through Drupal.
91+
location ~ ^/system/files/ {
92+
try_files $uri /index.php?$query_string;
93+
}
94+
95+
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
96+
expires max;
97+
log_not_found off;
98+
}
99+
}

0 commit comments

Comments
 (0)