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:
sudo apt update
sudo apt upgrade -y
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:
sudo apt install apache2 -y
- To install the MySQL database, run:
sudo apt install mysql-server -y
- To install PHP and some additional PHP modules, run:
sudo apt install php libapache2-mod-php php-mysql php-xml php-mbstring php-mcrypt php-curl php-zip -y
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:
sudo mysql_secure_installation
Follow the on-screen instructions to secure your MySQL installation. Then, log into MySQL to create a database for your Laravel application:
sudo mysql -u root -p
Inside the MySQL shell, run these commands to create a database and a user:
CREATE DATABASE laravel_app;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 4: Set Up Apache Server
We need to configure Apache to serve our Laravel application. First, enable the mod_rewrite module:
sudo a2enmod rewrite
Then, create a new Apache configuration file for your Laravel site:
sudo nano /etc/apache2/sites-available/laravel.conf
Add the following content to the file:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/laravel/public
<Directory /var/www/laravel>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file. Then, enable the new site and restart Apache:
sudo a2ensite laravel.conf
sudo systemctl restart apache2
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:
cd /var/www/laravel
composer install
Next, set the correct permissions for the storage and cache directories:
sudo chown -R www-data:www-data storage
sudo chown -R www-data:www-data bootstrap/cache
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:
DB_DATABASE=laravel_app
DB_USERNAME=laravel_user
DB_PASSWORD=your_password
Step 7: Generate Application Key
Finally, generate an application key for your Laravel application:
php artisan key:generate
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:
sudo apt install certbot python3-certbot-apache -y
Obtain and install the SSL certificate:
sudo certbot --apache
Follow the on-screen instructions. Certbot will automatically configure SSL for your site and renew the certificate when needed.
Test the certificate renewal process:
sudo certbot renew --dry-run
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!