Skip to content

Instantly share code, notes, and snippets.

@FaridAghili
Last active February 11, 2024 23:06
Show Gist options
  • Save FaridAghili/70e29a88dc40c274c856f2cd8d8b3fba to your computer and use it in GitHub Desktop.
Save FaridAghili/70e29a88dc40c274c856f2cd8d8b3fba to your computer and use it in GitHub Desktop.

First run:

apt update && apt upgrade -y && apt autoremove -y && reboot

Then create a file named server.sh, paste the following content and run it using bash server.sh:

#!/bin/bash

echo 'Enter a username to own the projects:'
read -r account_username

add-apt-repository ppa:ondrej/php -y
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - 

account_password=$(openssl rand -base64 32)
database_password=$(openssl rand -base64 32)

apt install memcached mysql-server nginx nodejs redis-server snapd supervisor unzip -y
apt install php8.2 php8.2-{curl,dom,fpm,gd,intl,mbstring,memcached,mysql,redis,xml,zip} -y

snap install --classic certbot
ln -s /snap/bin/certbot /usr/local/bin/certbot

ufw allow 'Nginx Full'
ufw allow 'OpenSSH'
ufw --force enable

mysql <<_EOF_
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$database_password';
    DELETE FROM mysql.user WHERE Host != 'localhost';
    DELETE FROM mysql.user WHERE User = '';
    DROP DATABASE IF EXISTS test;
    FLUSH PRIVILEGES;
_EOF_

curl -fsSL https://getcomposer.org/installer --output composer-setup.php
php composer-setup.php
unlink composer-setup.php
mv composer.phar /usr/local/bin/composer

apt update && apt upgrade -y && apt autoremove -y

adduser --gecos "" --disabled-password $account_username
chpasswd <<<"$account_username:$account_password"
usermod -aG sudo $account_username

chown -R $account_username:$account_username /var/www
chmod -R 755 /var/www

echo "Account username: $account_username
Account password: $account_password
MySQL root password: $database_password"

rm server.sh

To deploy a new project, first clone the git repositry in /var/www/ folder, cd into it, create a file named deploy.sh, paste the following content and run it using bash deploy.sh:

#!/bin/bash

echo 'Enter database root password:'
read -rs database_root_password

echo 'Enter a name for Database:'
read -r database_name

database_password=$(openssl rand -base64 32)

mysql -u root "-p$database_root_password" <<_EOF_
    CREATE DATABASE IF NOT EXISTS $database_name;
    CREATE USER IF NOT EXISTS '$database_name'@'localhost' IDENTIFIED WITH mysql_native_password BY '$database_password';
    GRANT ALL ON $database_name.* TO '$database_name'@'localhost';
_EOF_

cp .env.example .env

sed -i "s/^APP_ENV=.*/APP_ENV=production/" .env
sed -i "s/^APP_KEY=.*/APP_KEY=/" .env
sed -i "s/^APP_DEBUG=.*/APP_DEBUG=false/" .env
sed -i "s/^DB_DATABASE=.*/DB_DATABASE=$database_name/" .env
sed -i "s/^DB_USERNAME=.*/DB_USERNAME=$database_name/" .env
sed -i "s/^DB_PASSWORD=.*/DB_PASSWORD=$database_password/" .env

composer install --optimize-autoloader --no-dev

php artisan key:generate
php artisan storage:link
php artisan config:cache
php artisan event:cache
php artisan route:cache
php artisan view:cache
php artisan migrate --force

npm install
npm run build

sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache

rm deploy.sh

Then a create a new nginx config file in /etc/nginx/sites-available/ and paste the following content:

server {
    listen 80;
    server_name demo.faridaghili.ir;
    root /var/www/faridaghili/public;
 
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
 
    index index.php;
 
    charset utf-8;
 
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
 
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
 
    error_page 404 /index.php;
 
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
 
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Restart nginx using sudo systemctl reload nginx.

Run certbot --nginx to setup HTTPS and your done.

Make sure to edit .env file inside your Laravel project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment