Skip to content

Instantly share code, notes, and snippets.

@moh-slimani
Created November 3, 2019 11:14
Show Gist options
  • Save moh-slimani/ec3c0ddf484d3d0e3b794099fded50d6 to your computer and use it in GitHub Desktop.
Save moh-slimani/ec3c0ddf484d3d0e3b794099fded50d6 to your computer and use it in GitHub Desktop.
Generate Laravel Configuration file for nginx server
#!/bin/bash
if [[ $EUID > 0 ]]; then # we can compare directly with this syntax.
echo "Please run as root/sudo"
exit 1
else
if test -z "$1"; then
PROJECT=${PWD##*/}
DIR="$PWD"
else
PROJECT=$1
DIR="$PWD/$1"
fi
echo "Creating nginx server configuration for $DIR"
echo "new website domain will be $PROJECT.oo"
echo "creating a sim link to $PROJECT"
echo `ln -s $DIR /var/www/$PROJECT`
echo "creating the configuration file in /etc/nginx/site-available/$PROJECT"
printf "server {
listen 80;
listen [::]:80;
root /var/www/$PROJECT/public;
index index.php;
access_log /var/www/$PROJECT/storage/logs/access.log;
error_log /var/www/$PROJECT/storage/logs/error.log;
server_name $PROJECT.oo www.$PROJECT.oo;
location ~* \\.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
log_not_found off;
}
if (!-d \$request_filename)
{
rewrite ^/(.+)/$ /\$1 permanent;
}
if (\$host ~* ^www\\.(.*))
{
set \$host_without_www \$1;
rewrite ^/(.*)$ \$scheme://\$host_without_www/\$1 permanent;
}
if (!-e \$request_filename)
{
rewrite ^/(.*)$ /index.php?/\$1 last;
break;
}
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \\.php$ {
try_files \$uri /index.php =404;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\\.ht {
deny all;
}
}
\n" > /etc/nginx/sites-available/$PROJECT
echo "enabling website"
echo `ln -s /etc/nginx/sites-available/$PROJECT /etc/nginx/sites-enabled/$PROJECT`
echo "runing service nginx restart"
echo `service nginx restart`
echo "adding to localdomain hosts file"
echo "127.0.0.1 $PROJECT.oo" >> /etc/hosts
echo "final permissions changes"
echo `chmod a+rw -R $DIR/storage/`
echo "All Don"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment