As of today, Laravel is the top choice for PHP developers to build APIs and web apps. When your app is ready to launch, the next step is to pick the right operating system, settings, and security options. Deploying a Laravel app to a live server can be a bit tricky, especially if you’re new to it. In this guide, we’ll show you each step, using Ubuntu as our server operating system. By the end of this tutorial, you’ll have your Laravel app running on a live server. We’ll cover everything from setting up your server to configuring Laravel for production.
How to Deploy Laravel on Ubuntu
Before we begin, make sure you have the following:
- A server running Ubuntu (24.04 or 22.04 is recommended).
- SSH access to your server with a sudo privileges account.
- Basic knowledge of using the command line.
- A Laravel project ready to deploy.
Step 1: Update Your Server
The first step is to make sure your current system packages are up to date. To update packages, connect to your server using SSH and run the following commands:
Step 2: Install Required Software
Laravel is written in the PHP programming language. To serve it on the web, you need a web server like Apache. For storing data permanently, you need a database like MySQL. Overall, you need to install some software that is required to run a Laravel application. This includes Apache, MySQL, PHP, and some other tools.
- Run the following command to install Apache:
- To install the MySQL database, run:
- To install PHP and some additional PHP modules, run:
Step 3: Configuring MySQL
After the first-time installation of the MySQL server, you need to run the following command to start the MySQL setup wizard:
Follow the on-screen instructions to secure your MySQL installation. Then, log into MySQL to create a database for your Laravel application:
Inside the MySQL shell, run these commands to create a database and a user:
Step 4: Set Up Apache Server
We need to configure Apache to serve our Laravel application. First, enable the mod_rewrite module:
Then, create a new Apache configuration file for your Laravel site:
Add the following content to the file:
Save and close the file. Then, enable the new site and restart Apache:
Step 5: Deploy Your Laravel Application
Now, let’s deploy our Laravel application. First, upload your Laravel project to the /var/www/laravel directory on your server. You can use SCP, SFTP, or any other method you prefer.
Once the files are on the server, navigate to the project directory and install the required dependencies:
Next, set the correct permissions for the storage and cache directories:
Step 6: Configure Environment Variables
Laravel uses a .env file to store configuration settings. Make sure to update the database settings in the .env file to match the database and user we created earlier:
Step 7: Generate Application Key
Finally, generate an application key for your Laravel application:
Step 8: Set Up SSL Certificate
To secure your Laravel application, you can use Let’s Encrypt to set up a free SSL certificate. Follow these steps:
Install Certbot:
Obtain and install the SSL certificate:
Follow the on-screen instructions. Certbot will automatically configure SSL for your site and renew the certificate when needed.
Test the certificate renewal process:
Conclusion
That’s it! Your Laravel application should now be up and running on your Ubuntu server with SSL configured using Let’s Encrypt. Visit your server’s IP address in a web browser to see your application live and secured. This guide covered everything from updating your server and installing necessary software to deploying your Laravel app and setting up SSL. Now, your Laravel app is ready for the world!