Laravel is a PHP framework similar to Angular and Symfony used to quickly and efficiently build robust websites. In this article, we'll learn how to deploy Laravel applications on Ubuntu with Apache and MySQL.

Step 1: Update the System

Let's first update the package list and upgrade all outdated packages to the most recent version.

$ sudo apt update
$ sudo apt upgrade -y

Step 2: Install Required Packages

To get Laravel working, you need to install essential packages like Apache, MySQL, PHP, and some required PHP modules.

$ sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql php-xml php-mbstring php-mcrypt php-curl php-zip

Step 3: Configure MySQL

Now that we have installed the packages, let's begin by configuring MySQL for Laravel. We'll proceed by initiating a MySQL script that adjusts a few security-related settings for us.

$ sudo mysql_secure_installation

After you execute the above command, it will prompt a few questions that you should answer with the provided response below.

  • Would you like to setup VALIDATE PASSWORD component? N
  • Remove anonymous users? Y
  • Disallow root login remotely? Y
  • Remove test database and access to it? Y
  • Reload privilege tables now? Y

It's time to create a database and a new user for Laravel, so let's start by logging into the MySQL console.

$ sudo mysql -u root

Afterward, you can create a demo database named laravel_app and a demo username named laravel_user for Laravel. Feel free to adjust the database and username as needed, but be sure to use a more secure password.

mysql> CREATE DATABASE laravel_app;
mysql> CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'changeme';
mysql> GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_user'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;

Step 4: Install Composer

It's time to install Composer, which will allow us to create a Laravel application. Let's proceed with the installation script.

$ curl -sS https://getcomposer.org/installer | php

After that, we need to move the "composer.phar" file to the "/bin" directory and assign executable permissions to the composer binary file.

$ sudo mv composer.phar /usr/local/bin/composer
$ sudo chmod +x /usr/local/bin/composer

Step 5: Install Laravel

Finally, let's create a Laravel project named "laravel_proj" in the current directory.

$ composer create-project --prefer-dist laravel/laravel laravel_proj

Next, we navigate to the directory and edit the ".env" file.

$ cd laravel_proj
$ vim .env

Within the file, look for the following directives, uncomment them by removing the # prefix, and ensure you update their value to the specified one, unless you have named the Laravel database, username, and password differently.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=laravel_user
DB_PASSWORD=changeme

Save and close the file, then run the following command that use the migrations in the "database/migrations" directory to quickly create the table structure for the Laravel application, including providing version control for the database.

$ php artisan migrate

Step 6: Use Laravel for Local Development

We are now ready to access the Laravel application on our local system. To do this, simply run the following command to serve the application on localhost at port 80:

$ sudo php artisan serve --host=localhost --port=80

You can now open your browser and go to "http://localhost/" to access the app.

Step 7: Deploy the Laravel Application

To effectively deploy the app for production on the server, you need to move the Laravel directory (which we named "laravel_proj") to the "/var/www/html" directory. While it's not a strict requirement, we're using this approach for now because it's the most traditional method.

$ cd ..
$ sudo mv laravel_proj/ /var/www/html/

Following that, we recursively assign "www-data" as the directory's owner and grant the necessary permissions to the "storage" directory within the Laravel app.

$ sudo chgrp -R www-data /var/www/html/laravel_proj/
$ sudo chmod -R 775 /var/www/html/laravel_proj/storage/

Next, we'll create an Apache configuration file for the Laravel app. To do this, use the command to create a "laravel.conf" file.

$ sudo nano /etc/apache2/sites-available/laravel.conf

Copy-paste the following snippet inside the file:

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/laravel_proj/public

<Directory /var/www/html/laravel_proj>
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 be sure to disable the default Apache configuration file.

$ sudo a2dissite 000-default.conf

Afterward, activate the configuration we created for the Laravel app.

$ sudo a2ensite laravel.conf

Lastly, enable the Apache rewrite module and restart the Apache service to apply the changes.

$ sudo a2enmod rewrite
$ sudo systemctl restart apache2

Step 8: Allow Firewall

UFW (Uncomplicated Firewall) comes pre-installed on Ubuntu, so if you're using it to manage firewall rules on your system, make sure to enable the necessary ports required for Apache.

$ sudo ufw allow “Apache Full”

Conclusion

That's it! You can access the Laravel application using the IP address or hostname. If you have any questions about the topic, please let me know in the comments section.