How To Use Apache as a Reverse Proxy with mod_proxy on Ubuntu 22.04
a month ago
Here is a step-by-step guide to install Laravel with a LEMP stack on Ubuntu 22.04:
sudo apt update
sudo apt upgrade
sudo apt install nginx
sudo systemctl status nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo apt-get install mariadb-server
Secure MariaDB installation
sudo mysql_secure_installation
Log in to the MariaDB console
sudo mysql
Create a database for Laravel:
CREATE DATABASE laravel;
Create a database user and grant privileges:
CREATE USER 'laraveluser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON laravel.* TO 'laraveluser'@'localhost';
FLUSH PRIVILEGES;
Exit the MariaDB console:
exit;
Add the Ondrej sury PPA repository:
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
Install PHP and required extensions
sudo apt-get install php8.2 php8.2-fpm php8.2-cli php8.2-curl php8.2-zip php8.2-mysql php8.2-mbstring php8.2-xml php8.2-bcmath
Download the Composer installer:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Run the installer:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Remove the installer:
php -r "unlink('composer-setup.php');"
Verify the Composer installation:
composer --version
cd /var/www
composer create-project --prefer-dist laravel/laravel myproject
Set proper permissions for the Laravel project:
sudo chown -R www-data:www-data /var/www/myproject
sudo chmod -R 755 /var/www/myproject/storage
Create a new Nginx server block configuration file for the Laravel project
sudo nano /etc/nginx/sites-available/laravel
Add the following content to the file:
server {
listen 80;
server_name laravel.example.com;
root /var/www/myproject/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Save and close the file.
Create a symbolic link from the new server block configuration file to the sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
Test the Nginx configuration:
sudo nginx -t
If the test is successful, restart the Nginx service:
sudo systemctl restart nginx
Open the Laravel .env file and Update the database connection information:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laraveluser
DB_PASSWORD=password
Migrate the database tables:
php artisan migrate