Laravel

Laravel

Laravel is an open-source PHP framework for building web applications using the Model-View-Controller (MVC) architecture. Laravel is known for its elegant syntax, ease of understanding, and comprehensive built-in features such as routing, authentication, sessions, and database migrations. The framework provides tools like Eloquent ORM for managing databases, the Blade Template Engine for simplifying view creation, and the Artisan Command Line Tool for automating various tasks. Laravel is highly suitable for building modern web applications due to its support for RESTful APIs, middleware, and integration with various third-party services.

1. System Requirements

System requirements for Laravel:

  • PHP 8.2+
  • PHP Extensions: Ctype, cURL, DOM, Fileinfo, Filter, Hash, Mbstring, OpenSSL, PCRE, PDO, Session, Tokenizer, XML
  • Web server: Nginx, FrankenPHP, or Apache

Before starting deployment, prepare:

  • VPS or server with Ubuntu 24.04 OS
  • SSH access to the server
  • A domain already pointed to the server

2. Server Preparation

Update the system

sudo apt update
sudo apt upgrade -y

Install MariaDB

sudo apt install mariadb-server -y

Add the PPA ondrej/php repository

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

Install PHP 8.2 and required extensions

sudo apt install php8.2 php8.2-cli php8.2-common \
  php8.2-apcu php8.2-mbstring php8.2-gd php8.2-intl \
  php8.2-xml php8.2-soap php8.2-bcmath php8.2-mysql php8.2-zip \
  php8.2-curl php8.2-tidy php8.2-imagick php8.2-sqlite3 -y

Install Composer

sudo wget https://getcomposer.org/download/latest-stable/composer.phar -O /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

3. Create Database

Log in to MariaDB

sudo mysql

Create database and user

CREATE DATABASE laravel;
CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'rahasia';
GRANT ALL PRIVILEGES ON laravel.* TO 'laravel'@'localhost';
FLUSH PRIVILEGES;
exit

4. Web Server Configuration

Apache

Install Apache

sudo apt install apache2 libapache2-mod-php8.2 -y

Create a virtual host configuration for contoh.com

sudo nano /etc/apache2/sites-available/contoh.com.conf	

Enter the configuration

<VirtualHost *:80>
    ServerName contoh.com
    ServerAlias www.contoh.com
    DocumentRoot /var/www/contoh.com/public
    <Directory /var/www/contoh.com/public>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog /var/log/apache2/contoh.com_error.log
    CustomLog /var/log/apache2/contoh.com_access.log combined
</VirtualHost>

Enable the rewrite module, virtual host, and restart apache2

sudo a2enmod rewrite
sudo a2ensite contoh.com
sudo systemctl restart apache2	

Install Certbot for Apache

sudo apt install certbot python3-certbot-apache -y	

Request SSL certificate

sudo certbot --non-interactive \
    -m [email protected] \
    --agree-tos \
    --no-eff-email \
    --apache -d contoh.com -d www.contoh.com \
    --redirect	

Nginx

Install Nginx

sudo apt install nginx php8.2-fpm -y

Create a virtual host

sudo nano /etc/nginx/sites-available/contoh.com.conf

Enter the configuration

server {
    listen 80;
    server_name www.contoh.com contoh.com;
    root /var/www/contoh.com/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 ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_hide_header X-Powered-By;
    }
 
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Activate the virtual host

sudo ln -s /etc/nginx/sites-available/contoh.com.conf /etc/nginx/sites-enabled/
sudo systemctl restart nginx

Install Certbot for Nginx

sudo apt install certbot python3-certbot-nginx -y

Request SSL certificate

sudo certbot --non-interactive \
    -m [email protected] \
    --agree-tos \
    --no-eff-email \
    --nginx -d contoh.com -d www.contoh.com \
    --redirect	

5. Install Laravel

Install Laravel as a sample application to deploy.

If you already have a Laravel application ready for deployment, skip to configuring the .env file.

sudo apt install unzip -y
composer create-project laravel/laravel contoh.com

Enter the directory and open the .env file

cd contoh.com
nano .env

Set the URL and database configuration

APP_NAME="My Laravel App"
APP_ENV=production
APP_DEBUG=false
APP_TIMEZONE=Asia/Jakarta
APP_URL=https://www.contoh.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=rahasia

Run migrations and seed data

php artisan migrate --force
php artisan db:seed

Or import from a .sql dump file

mysql -u laravel -p laravel < backup-db.sql

Move the Laravel application directory to /var/www

sudo mv contoh.com /var/www

Clear the cache

cd /var/www/contoh.com
php artisan optimize:clear

Change the directory’s user-group

sudo chown -R www-data:www-data /var/www/contoh.com

Laravel deployment is complete. Test access and application functionality.