Panduan Lengkap Deploy Laravel di Ubuntu dengan Nginx, PHP, MySQL & HTTPS - Web Server Ubuntu

1. Install Nginx, PHP & MySQL

2. Konfigurasi MySQL

3. Setup Project Laravel

4. Konfigurasi Nginx

5. Pastikan Domain Mengarah ke Server

6. Install SSL dengan Certbot (Let's Encrypt)

7. Update .env untuk HTTPS

9. Verifikasi


Berikut panduan lengkap untuk deploy project Laravel Anda:

1. Install Nginx, PHP & MySQL

If it's Apache:

bash

sudo systemctl stop apache2
sudo systemctl disable apache2  # Prevent it from starting on boot

bash

# Update sistem
sudo apt update && sudo apt upgrade -y

# Install Nginx
sudo apt install nginx -y

# Install PHP 8.2 dan ekstensi yang dibutuhkan Laravel
sudo apt install php
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https -y
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.3
sudo apt install php8.2-fpm php8.2-mysql php8.2-mbstring php8.2-xml php8.2-bcmath php8.2-curl php8.2-zip php8.2-gd php8.2-intl php8.2-redis -y

# Install MySQL Server
sudo apt install mysql-server -y

# Install Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

2. Konfigurasi MySQL

bash

# Amankan instalasi MySQL
sudo mysql_secure_installation

# Masuk ke MySQL
sudo mysql

# Jalankan perintah SQL berikut:

sql

-- Buat database
CREATE DATABASE db_blog_dairyland CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Update konfigurasi mysql
cd /etc/mysql/mysql.conf.d/
nano mysqld.cnf
ubah port : 3307 hapus pagar
bind-address = 0.0.0.0
bind-address = 0.0.0.0
-- lalu tambahkan dibawah bind-address
sql_mode=""

-- Buat user dan berikan akses
CREATE USER 'user'@'%' IDENTIFIED WITH mysql_native_password BY 'Password';
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

-- Keluar
EXIT;
```

**Catatan Penting:** .env Anda menggunakan `DB_PORT=3307` dan `DB_HOST=172.106.0.21`. MySQL default port adalah 3306. Ubah di .env menjadi:
```
DB_HOST=127.0.0.1
DB_PORT=3306

3. Setup Project Laravel

bash

# Masuk ke direktori project
cd /var/www/yourproject

# Install dependencies
composer install --optimize-autoloader --no-dev

# Set permissions yang benar
sudo chown -R www-data:www-data /var/www/dairyland
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 755 /var/www/dairyland
sudo chmod -R 775 /var/www/dairyland/storage
sudo chmod -R 775 /var/www/dairyland/bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache

# Update .env (sesuaikan DB_HOST dan DB_PORT seperti di atas)
nano /var/www/yourproject/.env

# Jalankan migrasi database
php artisan migrate --force

# Cache konfigurasi untuk performa
php artisan config:cache
php artisan route:cache
php artisan view:cache

# Link storage jika ada
php artisan storage:link

Jika belum ada Node.js/npm, install dulu:

bash

# Install Node.js dan npm
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs

# Verifikasi
node -v
npm -v

# Install dependencies Node.js jika belum
npm install

# Build untuk production
npm run build

4. Konfigurasi Nginx

bash

# Buat file konfigurasi Nginx
sudo nano /etc/nginx/sites-available/dairyland

Tambahkan konfigurasi berikut:

nginx

server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.com;
    root /var/www/yourproject/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;
    }
}

sesuaikan path project kamu pada bagian root /var/www/yourproject/public;
ganti yourdomain.com dengan domain kamu.

bash

# Aktifkan konfigurasi
sudo ln -s /etc/nginx/sites-available/yourproject /etc/nginx/sites-enabled/

# Hapus default config jika ada
sudo rm /etc/nginx/sites-enabled/default

# Test konfigurasi Nginx
sudo nginx -t

# Restart Nginx
sudo systemctl restart nginx

# Enable Nginx untuk auto-start
sudo systemctl enable nginx

5. Pastikan Domain Mengarah ke Server

Pastikan DNS domain yourdomain.com sudah mengarah ke IP ip server dengan A Record.

Cek dengan:

bash

ping blog.dairyland.id

6. Install SSL dengan Certbot (Let's Encrypt)

bash

# Install Certbot untuk Nginx
sudo apt install certbot python3-certbot-nginx -y

# Dapatkan sertifikat SSL (akan otomatis mengupdate konfigurasi Nginx)
sudo certbot --nginx -d yourdomain.com

# Ikuti petunjuk interaktif:
# - Masukkan email Anda
# - Setujui Terms of Service
# - Pilih redirect HTTP ke HTTPS (pilihan 2)

# Test auto-renewal
sudo certbot renew --dry-run

Certbot akan otomatis:

  • Mengupdate konfigurasi Nginx Anda

  • Menambahkan redirect dari HTTP ke HTTPS

  • Setup auto-renewal sertifikat

7. Update .env untuk HTTPS

bash

nano /var/www/yourproject/.env

Ubah:

env

APP_URL=https://yourdomain.com
APP_DEBUG=false

PENTING: Set APP_DEBUG=false untuk production!

bash

# Clear cache setelah update .env
php artisan config:clear
php artisan config:cache

8. Restart Semua Service

bash

sudo systemctl restart php8.2-fpm
sudo systemctl restart nginx
sudo systemctl restart mysql

9. Verifikasi

Akses website Anda:

Cek SSL:

bash

sudo certbot certificates

0 Response to "Panduan Lengkap Deploy Laravel di Ubuntu dengan Nginx, PHP, MySQL & HTTPS - Web Server Ubuntu"

Post a Comment