Skip to content

Instantly share code, notes, and snippets.

@rajeshisnepali
Last active December 28, 2019 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajeshisnepali/c062813643dfb9ec44f89d3f489563d6 to your computer and use it in GitHub Desktop.
Save rajeshisnepali/c062813643dfb9ec44f89d3f489563d6 to your computer and use it in GitHub Desktop.
setup a project (laravel or any php) making serverblock (nginx) after cloning from git.
#!/bin/bash
# version 1.2
### changelog: first ask whether it's a laravel project or not.
# to be able to work this function globally, copy or create link in /usr/local/bin with execute permission (chmod +x)
## Process
# 1. cp .env.example .env
# 2. composer install
# 3. php artisan key:generate
# 4. ask for db credentials and save to .env
# [Optional]
# 5. make server block
makeServerBlock() {
try_files='try_files $uri $uri/ /index.php?$query_string;'
block="/etc/nginx/sites-available/$domain"
#--------------------------------------------------------------------
# add to /etc/hosts file
sudo sh -c 'echo "127.0.0.1 '$domain'" >> /etc/hosts'
# Create the Nginx server block file:
sudo tee $block > /dev/null <<EOF
server {
listen 80;
listen [::]:80;
root $path;
index index.php index.html index.htm;
server_name $domain;
location / {
#autoindex on;
$try_files
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php$php-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
EOF
# Link to make it available
sudo ln -s $block /etc/nginx/sites-enabled/
# Test configuration and reload if successful
sudo nginx -t && sudo service nginx reload
# Give write Permission to /bootstrap/cache & /storage
if [ -d $(pwd)/storage ] ; then
sudo chgrp -R www-data $(pwd)/storage $(pwd)/bootstrap/cache
sudo chmod -R ug+rwx $(pwd)/storage $(pwd)/bootstrap/cache
fi
}
# get accessories ready foor server block
prepareForServerBlock() {
echo Enter your local domain name:
read domain
echo Select PHP version for the project? [7.3]:
read php
if [ -z "$php" ]
then
php='7.3'
else
php=$php
fi
makeServerBlock $domain $path $php
}
processForLaravelProject() {
# 1. cp .env.example .env
cp .env.example .env
echo '.env created';
#
## 2. composer install
composer install
#
## 3. php artisan key:generate
php artisan key:generate
# 4. ask for db credentials and save to .env
echo Enter your database name, user and password [with space]:
read db user pass
# replace DB_DATABASE, DB_USERNAME & DB_PASSWORD in .env
sed -i 's/DB_DATABASE=homestead/DB_DATABASE='$db'/g' .env
sed -i 's/DB_USERNAME=homestead/DB_USERNAME='$user'/g' .env
sed -i 's/DB_PASSWORD=secret/DB_PASSWORD='$pass'/g' .env
echo Your database name is $db, user is $user and password is $pass
}
echo Is this a laravel project? [y/n]:
read isLaravel
case $isLaravel in
[Yy]* )
path=$(pwd)/public
processForLaravelProject
prepareForServerBlock $path
;;
[Nn]* )
path=$(pwd)
prepareForServerBlock $path
;;
* )
path=$(pwd)/public
processForLaravelProject
prepareForServerBlock $path
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment