Introduction
This document outlines the procedure to install and configure a LEMP stack on Ubuntu 24 for hosting a Laravel application. The process involves installing Nginx, MySQL, PHP, and other necessary packages.
Prerequisites
- A server running Ubuntu 24.
- Sudo privileges for executing administrative commands.
- A domain name pointed to your server's public IP address (for SSL configuration).
Step 1: Update and Install Packages
First, update your package manager and install the necessary packages including Nginx, MySQL, PHP, and additional PHP extensions needed for Laravel.
sudo apt update
sudo apt install -y nginx mysql-server php-fpm php-mysql acl zip
sudo apt install -y php-fpm php-cli php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-bcmath openssl php-tokenizer
Step 2: Configure MySQL
Secure your MySQL installation and set up a new database user.
- Log into MySQL as the root user:
sudo mysql
- Create a new user and grant privileges:
CREATE USER 'web'@'localhost' IDENTIFIED BY 'password1';
GRANT ALL PRIVILEGES ON *.* TO 'web'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
- Log into MySQL as the newly created user:
mysql -u web -p
Step 3: Additional Server Configuration
Create a user for the Laravel application and set up SSH access.
sudo adduser web
sudo su - web
mkdir .ssh
cat >> .ssh/authorized_keys ## Paste your public SSH key here.
chmod 700 .ssh
chmod 600 .ssh/authorized_keys
chmod 755 /home/web
Create server side user ssh keys to be use for deployment connecting to github.
ssh-keygen -t ed25519 -C "web@yourdomain.com"
Step 4: Configure Nginx
Set up Nginx to serve your Laravel application.
- Remove the default Nginx configuration:
sudo unlink /etc/nginx/sites-enabled/default
- Copy the default configuration to use as a basis for your Laravel app, replacing
SITENAME
with your app's name:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/SITENAME
sudo link /etc/nginx/sites-available/SITENAME /etc/nginx/sites-enabled/SITENAME
- Edit the Nginx site configuration. Replace
SITENAME
with your site's configuration details:
sudo nano /etc/nginx/sites-available/SITENAME
Insert the following server block, adjusting paths and directives as necessary:
server {
server_name SUDOMAIN.vientodigital.dev;
root /home/web/www/current/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.html 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 ~ /\.(?!well-known).* {
deny all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Note: Update the fastcgi_pass
directive to match the PHP socket file name corresponding to the installed PHP version (e.g., /run/php/php8.3-fpm.sock
).
Step 5: Configure SSL with Let's Encrypt
Install Certbot and obtain SSL certificates to enable HTTPS for your domain.
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot --nginx -d yourdomain.com
Conclusion
After following these steps, your server will be configured with a LEMP stack ready to deploy a Laravel application. Ensure that all paths and version numbers match your specific setup requirements.