DevOps: Deploy Laravel App di VPS Server

iwanna
2 min readApr 1, 2023

--

First of all

Login ke server menggunakan ssh dari terminal.

ssh username@server-ip

atau jika menggunakan ssh public key

ssh -i /path to ssh keys.pem username@server-ip

Let’s get started

Step 1 — Clone Repository

cd /var/www/html

...
git clone your-app-link

Step 2 — Change Permission

sudo chown -R your-user:www-data your-app-dir

...
cd your-app-dir

...
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

Step 3 — Composer Install

composer install

...
php artisan clear-compiled
php artisan optimize

Step 4 — Create Database

sudo mysql -u root -p
...
CREATE DATABASE your-db-name;

...
exit

Step 5 — Update .env File

sudo cp .env.example .env && sudo nano .env
...
DB_CONNECTION=mysql
DB_HOST=your-server-ip
DB_PORT=3306
DB_DATABASE=your-db-name
DB_USERNAME=your-db-username
DB_PASSWORD=your-db-password

Step 6 — Generate Key

php artisan key:generate

...
php artisan config:cache

Step 7 — Migration Database

php artisan migrate

Step 8 — Configuring Nginx

sudo nano /etc/nginx/sites-available/your-app
server {
listen 80; // or other ip 8001
listen [::]:80; // use other ip 8001
server_name your-domain // or ip XXX.XX.XXX.XXX;
root /var/www/html/your-project/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; // or php7.4
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.(?!well-known).* {
deny all;
}
}

Step 9 — Enable Configuration

sudo ln -s /etc/nginx/sites-available/your-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Step 10— Allow Port

sudo ufw status
sudo ufw allow 8001 // your-port
sudo ufw reload

--

--

iwanna
iwanna

Written by iwanna

Software Engineer | Sharing insights on web development & coding practices.

No responses yet