Skip to content

Instantly share code, notes, and snippets.

@khavari
Last active March 28, 2025 11:16
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:

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.3

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

Switch to another version:

sudo update-alternatives --set php /usr/bin/php8.3
sudo service php8.3-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.3-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') === '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 and add to (settings, repository, deploy keys)

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

git config --global user.email "[email protected]"
git config --global user.name "Mohammad Khavari"
git config --global core.fileMode false

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 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
@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