Skip to content

Instantly share code, notes, and snippets.

@khavari
Last active November 29, 2025 23:02
Show Gist options
  • Select an option

  • Save khavari/a0d7c149e7f940762fa05078786db6a6 to your computer and use it in GitHub Desktop.

Select an option

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:

sudo 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:

sudo apt install nano htop git zip unzip curl python3 gettext screen cron supervisor redis-server software-properties-common openssh-server tesseract-ocr -y

2- Install and Configure Nginx Web Server

install the nginx web server:

sudo apt update
sudo apt install nginx -y

3- Install PHP

Install PHP and modules:

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

Latest Version:

sudo 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

8.4

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

Switch to another version:

sudo update-alternatives --set php /usr/bin/php8.4
sudo service php8.4-fpm restart

4- Install and Configure MySQL

Install MySQL:

sudo apt install mysql-server -y
sudo systemctl status mysql

Run security script as validate password plugin:

sudo mysql_secure_installation
sudo 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:

sudo apt update
sudo apt install phpmyadmin -y
sudo nano /etc/nginx/sites-available/phpmyadmin.conf
server {
        listen 80;
        listen [::]:80;
	root /usr/share/phpmyadmin;
        index index.php index.html;
        server_name db.domain.com;
        
        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
                fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                include fastcgi_params;
        }
}
sudo ln -s /etc/nginx/sites-available/phpmyadmin.conf /etc/nginx/sites-enabled
sudo service nginx restart

If access denied issue:

nano /etc/phpmyadmin/config-db.php

6- Install Composer

cd /tmp
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'c8b085408188070d5f52bcfe4ecfbee5f727afa458b2573b8eaaf77b3419b0bf2768dc67c86944da1544f06fa544fd47') { echo 'Installer verified'.PHP_EOL; } else { echo 'Installer corrupt'.PHP_EOL; unlink('composer-setup.php'); exit(1); }"
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 and add to (settings, repository, deploy keys)

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

git config --global user.email "khavari.mvc@gmail.com"
git config --global user.name "Mohammad Khavari"
git config --global core.fileMode false

git clone git@gitlab.com: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 root@104.248.1.27:/var/www/html/
scp -rp *.png ssh root@104.248.1.27:/var/www/html/img

Upload directory:

scp -rp upload/ ssh root@104.248.1.27:/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": "git@gitlab.com: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 Node.js and npm

cd /tmp
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo bash -
sudo apt install nodejs -y
node --version
npm --version

Install and config supervisor

sudo apt install supervisor
sudo nano /etc/supervisor/conf.d/horizon.conf
[program:horizon]
command=php /var/www/trader/artisan horizon
autostart=true
autorestart=true

stdout_logfile=/var/log/supervisor/horizon.log
stdout_logfile_maxbytes=2MB
stdout_logfile_backups=5
sudo systemctl reload supervisor
sudo systemctl restart supervisor
@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