Skip to content

Instantly share code, notes, and snippets.

@khavari
Last active June 15, 2024 09:54
Show Gist options
  • Save khavari/a0d7c149e7f940762fa05078786db6a6 to your computer and use it in GitHub Desktop.
Save khavari/a0d7c149e7f940762fa05078786db6a6 to your computer and use it in GitHub Desktop.
Laravel Production Deployment

Laravel Production Ubuntu 20.04

1- Server Initial Setup

Set server timezone to Tehran:

dpkg-reconfigure tzdata

Upgrade server:

sudo apt update
sudo apt upgrade
sudo apt full-upgrade
sudo apt autoremove
sudo apt install ubuntu-release-upgrader-core

Install the required tools:

apt install htop git zip unzip curl python gettext screen cron supervisor redis-server software-properties-common openssh-server -y

2- Install and Configure Nginx Web Server

install the nginx web server:

apt update
apt install nginx -y

3- Install PHP

Install PHP 7.4 and its modules:

add-apt-repository -y ppa:ondrej/php

Latest Version:

apt install php-fpm php-mysql php-common php-cli php-gd php-mbstring php-xml php-zip php-bcmath php-curl php-soap php-redis php-memcached -y

7.4

apt install php7.4-fpm php7.4-mysql php7.4-common php7.4-cli php7.4-gd php7.4-mbstring php7.4-xml php7.4-zip php7.4-bcmath php7.4-gettext php7.4-curl php7.4-soap php7.4-redis php7.4-memcached -y

4- Install and Configure MySQL

Install MySQL:

apt install mysql-server -y
systemctl status mysql

Run security script as validate password plugin:

mysql_secure_installation
mysql
SELECT user,authentication_string,plugin,host FROM mysql.user;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '*********';

Create databases, users, and grant all corresponding privileges:

CREATE DATABASE DB_NAME_HERE CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER USERNAME_HERE@localhost IDENTIFIED BY 'DB_PASSWORD_HERE';
GRANT ALL PRIVILEGES ON DB_NAME_HERE.* TO 'USERNAME_HERE'@'localhost' IDENTIFIED BY 'DB_PASSWORD_HERE';

Apply the new settings:

FLUSH PRIVILEGES;
exit

5- Install and Upgrade phpMyAdmin

Inistall phpMyAdmin:

apt update
apt install phpmyadmin -y
nano /etc/nginx/sites-available/default
ln -s /usr/share/phpmyadmin/ /var/www/html

Upgrade phpMyAdmin:

mv /usr/share/phpmyadmin/ /usr/share/phpmyadmin.bak
cd /usr/share/
wget https://files.phpmyadmin.net/phpMyAdmin/5.2.1/phpMyAdmin-5.2.1-all-languages.zip
unzip phpMyAdmin-5.2.1-all-languages.zip
mv phpMyAdmin-5.2.1-all-languages/ phpmyadmin
rm phpMyAdmin-5.2.1-all-languages.zip
cd /usr/share/phpmyadmin

Edit vendor_config.php:

mkdir /usr/share/phpmyadmin/tmp
sudo chmod 777 /usr/share/phpmyadmin/tmp/

nano /usr/share/phpmyadmin/libraries/vendor_config.php

If access denied issue:

nano /etc/phpmyadmin/config-db.php

Restart Nginx:

service nginx restart

6- Install Composer

cd /tmp
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer

Mongo

curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

sudo apt update

sudo apt install mongodb-org -y

sudo systemctl start mongod.service

sudo systemctl enable mongod

mongo --eval 'db.runCommand({ connectionStatus: 1 })'

sudo apt install php-mongodb



ln -s /usr/share/phpmyadmin/ /var/www/trader/public/

6- Add ssh-keys and clone repositories

Create a key for root:

ssh-keygen -t rsa
cat /root/.ssh/id_rsa.pub

git config --global user.email "[email protected]"
git config --global user.name "Mohammad Khavari"
git clone [email protected]:asrenet/deployer.git /var/www/deployer

Ignore file mode:

git config core.fileMode false

7- Upload file or directory to server

Upload file:

scp -rp index.php ssh [email protected]:/var/www/html/
scp -rp *.png ssh [email protected]:/var/www/html/img

Upload directory:

scp -rp upload/ ssh [email protected]:/var/www/html/
unzip /var/www/html/file.zip -d /var/www/
unzip file.zip

8- Deploy on Push Service

Install git-auto-deploy:

add-apt-repository ppa:olipo186/git-auto-deploy
apt install git-auto-deploy

Start the service:

/etc/init.d/git-auto-deploy start

This service listens on 8001 port number by default. Use telnet to check if it’s started correctly and is listening on this port.

telnet localhost 8001

Config git-auto-deploy repositories:

nano /etc/git-auto-deploy.conf.json

"https-enabled": false,

 {
      "url": "[email protected]:asrenet/deployer.git",
      "branch": "master",
      "remote": "origin",
      "path": "/var/www/deployer",
      "prepull": "",
      "postpull": "",
      "deploy": ""
    },

Restart the service:

service git-auto-deploy restart

Copy generated ssh keys to git-auto-deploy home directory:

cp /root/.ssh/id_rsa /etc/git-auto-deploy/.ssh/
cp /root/.ssh/id_rsa.pub /etc/git-auto-deploy/.ssh/
chown -R git-auto-deploy:git-auto-deploy /etc/git-auto-deploy

Add ssh keys for authentication without password to localhost with git-auto-deploy:

cat /etc/git-auto-deploy/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
chmod og-wx /root/.ssh/authorized_keys

Add git-auto-deploy to the www-data group:

usermod -a -G www-data git-auto-deploy

Fix owners:

chown -R git-auto-deploy:www-data /var/

watch the service log:

tail -f /var/log/git-auto-deploy.log

9- Install and Configure Webpack

Install nodejs v10.x and npm v6.x:

cd /tmp
sudo curl -sL https://deb.nodesource.com/setup_20.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh

sudo apt install nodejs

node --version
npm --version

Install webpack 4.x and webpack-cli 3.x:

npm install  --global webpack webpack-cli

webpack -v
webpack-cli -v

@khavari
Copy link
Author

khavari commented Mar 12, 2019

How to Manually Upgrade phpMyAdmin

  1. Back up phpMyAdmin
    mv /usr/share/phpmyadmin/ /usr/share/phpmyadmin.bak
    wget https://files.phpmyadmin.net/phpMyAdmin/4.8.5/phpMyAdmin-4.8.5-all-languages.zip
    unzip phpMyAdmin-4.8.5-all-languages.zip
    rm phpMyAdmin-4.8.5-all-languages.zip
    mv phpMyAdmin-4.8.5-all-languages/ phpmyadmin
    cd /usr/share/phpmyadmin

@khavari
Copy link
Author

khavari commented Mar 13, 2019

Protect a Directory in Nginx:

apt install apache2-utils
htpasswd -c /etc/nginx/.htpasswd username
cat /etc/nginx/.htpasswd
 nano /etc/nginx/sites-available/default
location / {
        # Other Codes

        auth_basic "Restricted Access";
        auth_basic_user_file /etc/nginx/.htpasswd;
}
or
location /phpmyadmin {
        auth_basic "Restricted Access";
        auth_basic_user_file /etc/nginx/.htpasswd;
}
nginx -t
service nginx restart

@khavari
Copy link
Author

khavari commented Mar 18, 2019

Database backup:

mysqldump -u root -p database_name > /srv/backup/database/site_name/backup_name_`date +%Y_%m_%d__%H_%M`.sql

@khavari
Copy link
Author

khavari commented May 11, 2021

sudo mv composer.phar /usr/local/bin/composer

@khavari
Copy link
Author

khavari commented Nov 11, 2021

Set default php version

sudo update-alternatives --set php /usr/bin/php8.0

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