Using any cloud hosting provider—Linode, Vultr, DigitalOcean, etc.—this cheatsheet offers detailed instructions on how to deploy and configure a Laravel application on an Ubuntu OS running an LEMP stack.
- Any cloud hosting account that you can create Compute VPS Instance
- Amazon Webservice account with a created EC2 VPS instance (Ubuntu 20 or later is much recommended).
- Linode account with a created virtual server (Ubuntu 20 or later is much recommended).
- DigitalOcean account with a created VPS instance (Ubuntu 20 or later is much recommended).
- Knowledge on Basic server administration.
- Git installed on your local machine.
- A Laravel application hosted on GitHub (e.g., "laravel_yourapp").
- Example we have an application called "laravel_yourapp". Change the "laravel_yourapp" for your website/domain name or Laravel app name.
-
Create a VPS instance:
- Choose Ubuntu 20.04 as the operating system.
- Follow the wizard to create the VPS instance.
-
Access Your VPS instance:
- Open your terminal and use SSH to access your VPS instance:
ssh root@your_VPS instance_ip
- Open your terminal and use SSH to access your VPS instance:
-
Update Server Packages:
sudo apt update sudo apt upgrade -y # optional
-
Install NGINX Web Server
sudo apt install nginx # Start NGINX and Enable it. sudo systemctl start nginx sudo systemctl enable nginx # Check if NGINX status is running sudo systemctl status nginx
-
Install MySQL:
sudo apt install mysql-server sudo mysql_secure_installation # modify some insecure defaults
-
Change root mysql password:
sudo mysql ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password'; mysql -u root -p # Access mysql
-
Install PHP and Required Extensions:
- To Download Other Versions of PHP
sudo add-apt-repository ppa:ondrej/php
- Latest Version
sudo apt install php php-fpm php-mysql php-common php-bcmath php-ctype php-json php-mbstring php-openssl php-pdo php-tokenizer php-xml php-zip php-gd
- If you want specific version of PHP like 7.4
sudo apt install php7.4 php7.4-fpm php7.4-mysql php7.4-common php7.4-bcmath php7.4-ctype php7.4-json php7.4-mbstring php7.4-openssl php7.4-pdo php7.4-tokenizer php7.4-xml php7.4-zip php7.4-gd
- To Download Other Versions of PHP
-
Configure Nginx for Laravel:
- Create an Nginx server block configuration for your Laravel app (e.g., /etc/nginx/sites-available/laravel_yourapp).
- Configure the Nginx server block to use PHP-FPM for processing PHP files.
-
Create server block config
sudo nano /etc/nginx/sites-available/laravel_yourapp
# NGINX CONFIG FOR LARAVEL [https://laravel.com/docs/7.x/deployment] server { listen 80; server_name your_VPS instance_ip; root /var/www/laravel_yourapp/public; add_header X-Frame-Options "SAMEORIGIN"; add_header X-XSS-Protection "1; mode=block"; 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/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.(?!well-known).* { deny all; } }
# From Digital Ocean Snippets [https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-ubuntu-18-04] server { listen 80; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; server_name your_domain; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; } location ~ /\.ht { deny all; } }
-
Enable the Nginx Configuration:
- Create a symbolic link to enable the site:
sudo ln -s /etc/nginx/sites-available/laravel_yourapp /etc/nginx/sites-enabled/
- Unlink the default conf
sudo unlink /etc/nginx/sites-enabled/default
-
Test Nginx Configuration:
nginx -t
-
Clone Your Laravel App from GitHub:
git clone https://github.com/yourusername/laravel_yourapp.git /var/www/laravel_yourapp
-
Install Composer
-
Install Composer Dependencies
cd /var/www/laravel_yourapp composer install --no-interaction --prefer-dist
-
Generate Laravel Application Key:
php artisan key:generate
-
Set Permissions:
- Ensure proper file permissions for Laravel Application
sudo chown -R www-data:www-data /var/www/laravel_yourapp sudo chmod -R 755 /var/www/laravel_yourapp/storage
-
Access MySQL:
mysql -u root -p
-
Create a database and user:
CREATE DATABASE laravel_yourapp_db; CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON laravel_yourapp_db.* TO 'laravel_user'@'localhost'; FLUSH PRIVILEGES;
-
EXIT MYSQL
exit
-
Configure .env
- Update the Database Configuration
cd /var/www/laravel_yourapp nano .env
-
Migrate and Seed Database (Laravel):
php artisan migrate --seed
- Reload NGINX
systemctl reload nginx
- Restart PHP-FPM
systemctl restart php7.4-fpm