
Laravel is a popular PHP framework for building modern web applications with support for dynamic syntax and robust features like routing, authentication, and database management. Laravel works with multiple PHP versions and supports integration with other backend applications like database servers. Deploying Laravel with Nginx lets you deliver high-performance and scalable applications in production environments, allowing you to build modern web applications.
This article explains how to deploy Laravel with Nginx on Ubuntu 24.04. You will install Laravel, and create a new project to deploy with Nginx using a custom domain such as app.example.com.
Before you begin, you need to:
app.example.com.Laravel uses a relational database to store the application data. Follow the steps below to set up a new MySQL database, add sample application data for testing, and create a dedicated database user for Laravel.
Log in to the MySQL console as the root database user.
Run the following command to update the APT package index, install MySQL if it's not installed, and log in to the database console.
Create a new vultr_demo database to use with Laravel.
Create a new vultr_user MySQL user with a strong password. Replace secure_password with your desired password.
Grant the vultr_user user full privileges to the vultr_demo database.
Flush the MySQL privileges table to apply the database user changes.
Exit the MySQL console.
Log in to MySQL as vultr_user and enter the user password when prompted.
List all databases and verify that the vultr_demo database is available.
Output:
Use the vultr_demo database.
Create a server_stats with active columns to track server metrics.
Insert the demo data into the server_stats table.
Exit the MySQL console.
Laravel requires Composer and specific PHP extensions to run on a server. Follow the steps below to install PHP, Composer, and all required PHP extensions for Laravel.
Update the APT package index.
Install Composer and the required PHP extensions for Laravel.
The above command installs Composer, PHP, and the necessary extensions for Laravel. The installed packages include:
composer: A dependency manager for PHP.php-fpm: The PHP FastCGI Process Manager that enables PHP to run as a service and accept connections from web servers like Nginx.php-mysql: Connects PHP to MySQL databases.php-mbstring: Enables multibyte string support required by Laravel.php-curl: Enables PHP to make HTTP requests to external URLs.php-bcmath: Enables arbitrary precision math functions in PHP applications.php-json: Encodes and decodes JSON data necessary for APIs, Javascript and structured data.php-xml: Enables passing and generation of XML documents in applications.php-tokenizer: Includes tools for tokenizing PHP code.php-zip: Enables Laravel to read and write compressed .zip archives.Verify the installed Composer version.
Output:
Verify the installed PHP version.
Output:
Restart PHP-FPM depending on the installed version such as php8.3-fpm.
Follow the steps below to create a Laravel application and configure it to use the MySQL database you created earlier.
Switch to your user's home directory.
Create a new Laravel project such as vultr-demo.
The above command downloads Laravel with all dependencies and creates a vultr-demo project directory.
Navigate to the vultr-demo project directory.
Generate a unique application key required for secure encryption.
Output:
Open the .env file using a text editor such as nano to set up the Laravel database connection information and session driver.
Update the first section to include your project name like vultr-demo, change the environment from local to development, and change the application URL from http://localhost to your domain URL. Replace app.example.com with your actual domain.
Update the database connection section to include your database information. Change the default sqlite connection value to mysql, uncomment the host, port, database, and user field, replacing the default with your actual MySQL database details to use with Laravel.
Save and close the file.
Move your vultr-demo project directory to a standard web root path like /var/www.
Grant the www-data Nginx user and group ownership privileges to the /var/www/vultr-demo Laravel project directory.
Change the /var/www/vultr-demo permissions mode to 755 to grant the www-data user full privileges to the directory.
Change the /var/www/vultr-demo/bootstrap/cache permissions mode to 755 to grant the www-data user full privileges to create cache files.
Navigate to the /var/www/vultr-demo project directory.
Migrate the Laravel data to your database to test the connection.
Your output should be similar to the one below when successful.
Nginx uses server block configurations to serve Laravel files from your project directory. Follow the steps below to configure Laravel with Nginx using a new virtual host configuration with the required server block and PHP connection details.
View the Nginx service status and verify that it's running.
Output:
Run the following command to install Nginx if it's not installed, stop Apache if it's active, and start the Nginx service.
Create a new vultr-demo.conf Nginx configuration file in the /etc/nginx/sites-available directory.
Add the following configurations to the vultr-demo.conf file. Replace app.example.com with your actual domain.
Save and close the file.
The above configuration enables Nginx to serve the Laravel application files on the HTTP port 80 using your app.example.com domain. The location ~ \.php block forwards all PHP requests to the PHP-FPM socket, enabling Nginx to serve all Laravel application files.
Link the vultr-demo.conf file to the /etc/nginx/sites-enabled directory to enable the configuration.
Test the Nginx configuration for errors.
Output:
Restart Nginx to apply the configuration changes.
Allow the HTTP port 80 through the firewall.
Reload UFW to apply the firewall changes.
Access your app.example.com domain using a web browser such as Chrome and verify the default Laravel page displays.
Nginx serves all Laravel application files using HTTP which is insecure by default. Generating SSL certificates enables HTTPS connections and encrypts the connection to your web server. Follow the steps below to generate trusted Let's Encrypt SSL certificates and secure Laravel.
Install the Certbot Let's Encrypt client and its plugin for Nginx.
Generate a new SSL certificate using the Nginx Plugin and your virtual host domain. Replace app.example.com with your actual domain and admin@app.example.com with your active email address.
Your output should be similar to the one below when the SSL certificate request is successful.
Verify that Certbot auto-renews the SSL certificate before expiry.
Restart Nginx to apply the SSL configuration changes.
Allow the HTTPS port 443 through the firewall.
Reload UFW to apply the firewall changes.
Laravel includes a demo application with a Let's get started page you can replace with your custom code. Follow the steps below to replace the default project files, and build a demo Laravel application with a practical dashboard to display server metrics from your MySQL database.
Open and modify the Laravel's route file.
Back up the default web.php file in your Laravel project directory/
Create the web.php file again.
Add the following default route configuration to the web.php file to query the server metrics data in your MySQL database.
Save and close the file.
The above application code uses Laravel's query builder to fetch all records from the server_stats table in the MySQL database. Then, it orders servers by status, by CPU usage, and passes the results to a dashboard view.
Create a new dashboard.blade.php Blade template to use with the dashboard view.
Add the following HTML application code to the dashboard.blade.php file to display the server metrics.
Save and close the file.
The above application code displays the server metrics received from the web.php file in a single web page.
Restart Nginx to apply the changes.
Access your domain using HTTPS in a new web browser window and verify that the Laravel application displays correctly with data from the MySQL database.
You have deployed Laravel with Nginx on Ubuntu 24.04 and created a sample application that queries the MySQL database to display live data. You can modify your Laravel project with new models, views, and controllers to deliver your applications with Nginx. For more information and configuration options, visit the official Laravel documentation.
0 Comments
Be the first to comment and share your perspective with the community.