Skip to content

Instantly share code, notes, and snippets.

@andrewdaluz
Created February 19, 2021 22:41
Show Gist options
  • Save andrewdaluz/825181fa0706f562bca204563a15db89 to your computer and use it in GitHub Desktop.
Save andrewdaluz/825181fa0706f562bca204563a15db89 to your computer and use it in GitHub Desktop.
<?php
/*
* Template of virtual host
* {$argv[1]} : nome do projeto
* {$argv[2]} : diretorio do projeto
* @example sudo php create-project-m2.php nome_projeto /var/diretorio/
* created by André Felipe (www.trezo.com.br)
*/
$template = "## Example configuration:\n";
$template .= "#upstream fastcgi_backend {\n";
$template .= "# use tcp connection\n";
$template .= "# server 127.0.0.1:9000;\n";
$template .= "# # or socket\n";
$template .= "# server unix:/var/run/php5-fpm.sock;\n";
$template .= "# server unix:/var/run/php/php7.0-fpm.sock;\n";
$template .= "# fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;\n";
$template .= "#}\n";
$template .= "server {\n";
$template .= " listen 80;\n";
$template .= " listen 443 ssl;\n\n";
$template .= " ssl_certificate /etc/nginx/SERVER.crt;\n";
$template .= " ssl_certificate_key /etc/nginx/SERVER.key;\n";
$template .= " server_name {$argv[1]};\n\n";
$template .= " set \$MAGE_ROOT {$argv[2]};\n";
$template .= " #include /vagrant/magento2/nginx.conf.sample;\n";
$template .= " access_log /var/log/nginx/{$argv[1]}.access.log;\n";
$template .= " error_log /var/log/nginx/{$argv[1]}.error.log notice;\n\n";
$template .= " #\n";
$template .= " ## Optional override of deployment mode. We recommend you use the\n";
$template .= " ## command 'bin/magento deploy:mode:set' to switch modes instead.\n";
$template .= " ##\n";
$template .= " set \$MAGE_MODE developer; # or production or developer\n";
$template .= " ##\n";
$template .= " ## If you set MAGE_MODE in server config, you must pass the variable into the\n";
$template .= " ## PHP entry point blocks, which are indicated below. You can pass\n";
$template .= " ## it in using:\n";
$template .= " ##\n\n";
$template .= " fastcgi_param MAGE_MODE \$MAGE_MODE;\n\n";
$template .= " ##\n";
$template .= " ## In production mode, you should uncomment the 'expires' directive in the /static/ location block\n\n";
$template .= " root \$MAGE_ROOT/pub;\n\n";
$template .= " index index.php;\n";
$template .= " autoindex off;\n";
$template .= " charset UTF-8;\n";
$template .= " error_page 404 403 = /errors/404.php;\n";
$template .= " #add_header \"X-UA-Compatible\" \"IE=Edge\";\n\n";
$template .= " # PHP entry point for setup application\n";
$template .= " location ~* ^/setup($|/) {\n";
$template .= " root \$MAGE_ROOT;\n";
$template .= " location ~ ^/setup/index.php {\n";
$template .= " fastcgi_pass fastcgi_backend;\n\n";
$template .= " fastcgi_param PHP_FLAG \"session.auto_start=off \n suhosin.session.cryptua=off\";\n";
$template .= " fastcgi_param PHP_VALUE \"memory_limit=756M \n max_execution_time=600\";\n";
$template .= " fastcgi_read_timeout 600s;\n";
$template .= " fastcgi_connect_timeout 600s;\n\n";
$template .= " fastcgi_index index.php;\n";
$template .= " fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n";
$template .= " include fastcgi_params;\n";
$template .= " }\n\n";
$template .= " location ~ ^/setup/(?!pub/). {\n";
$template .= " deny all;\n";
$template .= " }\n\n";
$template .= " location ~ ^/setup/pub/ {\n";
$template .= " add_header X-Frame-Options \"SAMEORIGIN\";\n";
$template .= " }\n";
$template .= " }\n\n";
$template .= " # PHP entry point for update application\n";
$template .= " location ~* ^/update($|/) {\n";
$template .= " root \$MAGE_ROOT;\n\n";
$template .= " location ~ ^/update/index.php {\n";
$template .= " fastcgi_split_path_info ^(/update/index.php)(/.+)$;\n";
$template .= " fastcgi_pass fastcgi_backend;\n";
$template .= " fastcgi_index index.php;\n";
$template .= " fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n";
$template .= " fastcgi_param PATH_INFO \$fastcgi_path_info;\n";
$template .= " include fastcgi_params;\n";
$template .= " }\n\n";
$template .= " # Deny everything but index.php\n";
$template .= " location ~ ^/update/(?!pub/). {\n";
$template .= " deny all;\n";
$template .= " }\n\n";
$template .= " location ~ ^/update/pub/ {\n";
$template .= " add_header X-Frame-Options i\"SAMEORIGIN\";\n";
$template .= " }\n";
$template .= " }\n\n";
$template .= " location / {\n";
$template .= " try_files \$uri \$uri/ /index.php\$is_args\$args;\n";
$template .= " }\n\n";
$template .= " location /pub/ {\n";
$template .= " location ~ ^/pub/media/(downloadable|customer|import|theme_customization/.*\.xml) {\n";
$template .= " deny all;\n";
$template .= " }\n";
$template .= " alias \$MAGE_ROOT/pub/;\n";
$template .= " add_header X-Frame-Options \"SAMEORIGIN\";\n";
$template .= " }\n\n";
$template .= " location /static/ {\n";
$template .= " # Uncomment the following line in production mode\n";
$template .= " # expires max;\n\n";
$template .= " # Remove signature of the static files that is used to overcome the browser cache\n";
$template .= " location ~ ^/static/version {\n";
$template .= " rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last;\n";
$template .= " }\n\n";
$template .= " location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {\n";
$template .= " add_header Cache-Control \"public\";\n";
$template .= " add_header X-Frame-Options \"SAMEORIGIN\";\n";
$template .= " expires +1y;\n\n";
$template .= " if (!-f \$request_filename) {\n";
$template .= " rewrite ^/static/?(.*)$ /static.php?resource=$1 last;\n";
$template .= " }\n";
$template .= " }\n";
$template .= " location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {\n";
$template .= " add_header Cache-Control \"no-store\";\n";
$template .= " add_header X-Frame-Options \"SAMEORIGIN\";\n";
$template .= " expires off;\n\n";
$template .= " if (!-f \$request_filename) {\n";
$template .= " rewrite ^/static/?(.*)$ /static.php?resource=$1 last;\n";
$template .= " }\n";
$template .= " }\n";
$template .= " if (!-f \$request_filename) {\n";
$template .= " rewrite ^/static/?(.*)$ /static.php?resource=$1 last;\n";
$template .= " }\n";
$template .= " add_header X-Frame-Options \"SAMEORIGIN\";\n";
$template .= " }\n\n";
$template .= " location /media/ {\n";
$template .= " try_files \$uri \$uri/ /get.php\$is_args\$args;\n\n";
$template .= " location ~ ^/media/theme_customization/.*\.xml {\n";
$template .= " deny all;\n";
$template .= " }\n\n";
$template .= " location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {\n";
$template .= " add_header Cache-Control \"public\";\n";
$template .= " add_header X-Frame-Options \"SAMEORIGIN\";\n";
$template .= " expires +1y;\n";
$template .= " try_files \$uri \$uri/ /get.php\$is_args\$args;\n";
$template .= " }\n";
$template .= " location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {\n";
$template .= " add_header Cache-Control \"no-store\";\n";
$template .= " add_header X-Frame-Options \"SAMEORIGIN\";\n";
$template .= " expires off;\n";
$template .= " try_files \$uri \$uri/ /get.php\$is_args\$args;\n";
$template .= " }\n";
$template .= " add_header X-Frame-Options \"SAMEORIGIN\";\n";
$template .= " }\n\n";
$template .= " location /media/customer/ {\n";
$template .= " deny all;\n";
$template .= " }\n\n";
$template .= " location /media/downloadable/ {\n";
$template .= " deny all;\n";
$template .= " }\n\n";
$template .= " location /media/import/ {\n";
$template .= " deny all;\n";
$template .= " }\n\n";
$template .= " # PHP entry point for main application\n";
$template .= " location ~ (index|get|static|report|404|503|health_check)\.php$ {\n";
$template .= " try_files \$uri =404;\n";
$template .= " fastcgi_pass fastcgi_backend;\n";
$template .= " fastcgi_buffers 1024 4k;\n\n";
$template .= " fastcgi_param PHP_FLAG \"session.auto_start=off \n suhosin.session.cryptua=off\";\n";
$template .= " fastcgi_param PHP_VALUE \"memory_limit=756M \n max_execution_time=18000\";\n";
$template .= " fastcgi_read_timeout 600s;\n";
$template .= " fastcgi_connect_timeout 600s;\n\n";
$template .= " fastcgi_index index.php;\n";
$template .= " fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n";
$template .= " include fastcgi_params;\n";
$template .= " }\n\n";
$template .= " gzip on;\n";
$template .= " gzip_disable \"msie6\";\n\n";
$template .= " gzip_comp_level 6;\n";
$template .= " gzip_min_length 1100;\n";
$template .= " gzip_buffers 16 8k;\n";
$template .= " gzip_proxied any;\n";
$template .= " gzip_types\n";
$template .= " text/plain\n";
$template .= " text/css\n";
$template .= " text/js\n";
$template .= " text/xml\n";
$template .= " text/javascript\n";
$template .= " application/javascript\n";
$template .= " application/x-javascript\n";
$template .= " application/json\n";
$template .= " application/xml\n";
$template .= " application/xml+rss\n";
$template .= " image/svg+xml;\n";
$template .= " gzip_vary on;\n\n";
$template .= " # Banned locations (only reached if the earlier PHP entry point regexes don't match)\n";
$template .= " location ~* (\.php$|\.htaccess$|\.git) {\n";
$template .= " deny all;\n";
$template .= " }\n";
$template .= "}\n";
echo $template;
$file = fopen("/etc/nginx/sites-available/{$argv[1]}.conf", 'x');
fwrite($file, $template);
fclose($file);
exec("ln -s /etc/nginx/sites-available/{$argv[1]}.conf /etc/nginx/sites-enabled/{$argv[1]}.conf");
$file = fopen("/etc/hosts", 'a+');
fwrite($file, "127.0.0.1 {$argv[1]}\n");
fclose($file);
exec('/etc/init.d/nginx restart');
echo 'Created with success the config for: ', $argv[1] . "\n" ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment