Deploy Laravel With Nginx
2025-03-04 | Technology
Clone Project
git clone https://github.com/Naffsisky/submission-laravel.git
Pindahkan Folder
Pindahkan folder menggunakan sudo
Pindahkan ke: /var/www/html
Installation
Install Composer
-
Check php version
php -v
Contoh response:
PHP 8.1.2-1ubuntu2.20 (cli) (built: Dec 3 2024 20:14:35) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.2, Copyright (c) Zend Technologies with Zend OPcache v8.1.2-1ubuntu2.20, Copyright (c), by Zend Technologies
-
Jika dibutuhkan bisa install library yang dibutuhkan
sudo apt install php-mbstring php-xml php-bcmath php-curl
-
Memberikan Izin atau Permission pada folder
sudo chown -R www-data:www-data /var/www/html/submission-laravel/ sudo chmod -R 777 /var/www/html/submission-laravel/
-
Lalu install composer
sudo apt install composer
-
Install php-fps Sesuaikan dengan versi php
sudo apt install php8.1-fpm
Check apakah sudah berjalan
sudo systemctl status php8.1-fpm
Configuration App
Sekarang membuat konfigurasi aplikasi terlebih dahulu
-
Membuat
.env
bisa juga di copy dari.env.example
sudo cp .env.example .env
Merubah
.env
untuk production dan sesuaikan databaseAPP_NAME="Blog Management" APP_ENV=production APP_KEY= APP_DEBUG=false APP_URL=http://submission.prinafsika.world
Sesuaikan dengan kebutuhan, dan siapkan juga untuk subdomainnya
-
Install composer
composer install
-
Membuat Key Application
php artisan key:generate
-
Build Aplikasi
Jika menggunakan library javascript bisa melakukan
npm install && npm run build
-
Migrasi Database
Install php mysql Versi Mysql di sesuaikan
sudo apt-get install php8.1-mysql
atauphp8.1-pgsql
Lalu migrate
php artisan migrate
Jika ada seed, silahkan di seed
php artisan db:seed
-
Link Storage Application
php artisan storage:link
Configuration Nginx
-
Siapkan domain atau sub domain terlebih dahulu di platform domain masing-masing.
-
Install nginx
sudo apt install nginx
-
Konfigurasi Sites-Available
sudo vim /etc/nginx/sites-available/submission
Disini saya menggunakan subdomain submisson dan domain prinafsika.world. Sesuaikan juga versi dari
fpm.sock
bisa cek di
sudo vim /etc/php/8.1/fpm/pool.d/www.conf
lalu temukan barislisten = /run/php/php8.1-fpm.sock
jika tidak ada bisa install terlebih dahulu
sudo apt install php8.1-fpm
Contoh konfigurasi nginx
/etc/nginx/sites-available/submission
server { listen 80; listen [::]:80; server_name submission.prinafsika.world; root /var/www/html/submission-laravel/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:/run/php/php8.1-fpm.sock; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.(?!well-known).* { deny all; } }
bisa ditambahkan untuk file upload pada nginx
client_max_body_size 5M;
server { server_name api.office.prinafsika.world; root /var/www/html/rent-office/public; client_max_body_size 5M; 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:/run/php/php8.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } location ^~ /livewire { try_files $uri $uri/ /index.php?$query_string; } location ~ /\.(?!well-known).* { deny all; } listen [::]:443 ssl; # managed by Certbot listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/api.office.prinafsika.world/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/api.office.prinafsika.world/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot } server { if ($host = api.office.prinafsika.world) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; listen [::]:80; server_name api.office.prinafsika.world; return 404; # managed by Certbot }
-
Symbolic Link ke Sites-Enabled
sudo ln -s /etc/nginx/sites-available/submission /etc/nginx/sites-enabled/
-
Check Konfigurasi Nginx
Memastikan konfigurasi sudah berjalan dengan benar
sudo nginx -t
Contoh response tanpa masalah
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Lalu restart nginx
sudo systemctl restart nginx
-
Memasang SSL untuk HTTPS
Menggunakan certbot
sudo apt install certbot python3-certbot-nginx
Pasang SSL
sudo certbot --nginx -d submission.prinafsika.world
Contoh response jika SSL berhasil terpasang
Saving debug log to /var/log/letsencrypt/letsencrypt.log Requesting a certificate for submission.prinafsika.world Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/submission.prinafsika.world/fullchain.pem Key is saved at: /etc/letsencrypt/live/submission.prinafsika.world/privkey.pem This certificate expires on 2025-06-02. These files will be updated when the certificate renews. Certbot has set up a scheduled task to automatically renew this certificate in the background. Deploying certificate Successfully deployed certificate for submission.prinafsika.world to /etc/nginx/sites-enabled/submission Congratulations! You have successfully enabled HTTPS on https://submission.prinafsika.world - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - If you like Certbot, please consider supporting our work by: * Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate * Donating to EFF: https://eff.org/donate-le - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Testing
Kunjungi situs yang telah berhasil di deploy, dan lakukan pengujian untuk semua fitur apakah sudah berjalan dengan baik.