How to Deploy a Laravel App Using Apache and MySQL.

Adeyomola Kazeem
6 min readOct 18, 2022

--

Earlier, I talked about How to Install Apache, PHP, Composer, and MySQL on Debian 11 (Deploying Laravel App). So, in this article, I continue the process by discussing how to deploy a sample Laravel app using Apache and MySQL.

In this article, we will be using this https://github.com/f1amy/laravel-realworld-example-app repo.

Table of Contents

  1. Clone the Repo into your user’s home directory
  2. Run the composer create-project command
  3. Move the project directory to Apache’s web root directory
  4. Create a configuration file the Laravel App’s virtual host
  5. Add the virtual host to /etc/hosts file
  6. Create a MySQL database for the Laravel App
  7. Edit the Laravel App’s environment variable file (.env file)
  8. Seed the Laravel App’s database
  9. Test the App in your browser

Clone the Repo Into User’s Home Directory

This is pretty simple to pull off, just run the following commands:

cd

This changes directory to the user’s home directory.

then

git clone https://github.com/f1amy/laravel-realworld-example-app.git

This command clones the git repository

then

cd laravel-realworld-example-app

This command changes directory to the laravel-realworld-example-app.

Run the Composer Create-Project Command

Once you are in the project’s folder, run the composer command to create the project:

composer create-project

This command would be composer create-project --prefer-dist laravel/laravel laravel if we were creating a new Laravel project. But since this is an existing project that contains a composer.json file, composer create-project will suffice. The composer.json file basically indicates the dependencies the app needs.

You may also use composer install or composer update . composer install is preferable if you want to install the versions of the dependencies specified in the composer.lock file. On the other hand, if you want the latest versions of the dependencies in spite of what is in the composer.lock file, use composer update .

After composer installs the dependencies, go back to the parent directory:

cd ..

then run these commands:

sudo chown -R www-data:www-data laravel-realworld-example-app

This command changes the owner and group of the directory and everything in the directory to www-data. www-data is the default user web servers like Apache use on Linux distros like Ubuntu and Debian.

then

sudo chmod -R 755 laravel-realworld-example-app

This command gives full permission to the user on the directory and everything in the directory. It gives only read and execute permission to group and others.

Move Project Directory to Apache Web Root Directory

After installing the project dependencies, changing owner, and changing permissions, move the project directory to the Apache web root directory (/var/www/html).

sudo mv laravel-realworld-example-app /var/www/html

Configure the App’s Virtual Host in Apache

Next, change directory to the apache sites-available configuration directory:

cd /etc/apache2/sites-available

Copy the 000-default.conf file and give it a new name (in my case, I’m naming the copy laravel.conf):

sudo cp 000-default.conf laravel.conf

Open the laravel.conf using nano:

sudo nano laravel.conf

Then edit its content as follows

<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName laravel.example.com
DocumentRoot /var/www/html/laravel-realworld-example-app/public/

<Directory /var/www/html/laravel-realworld-example-app/public/>
AllowOverride All
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

For the ServerAdmin and ServerName , you can use any domain name you want. I used laravel.example.com and admina@example.com in this case.

Once you are done editing, save and exit the file. Then run the following commands:

sudo a2dissite 000-default.conf

This disables the default Apache site.

Then:

sudo a2enmod rewrite
sudo a2ensite laravel.conf

This enables the Laravel App’s site and mod_rewrite.

Then:

sudo systemctl reload apache2

Add the Virtual Host to the Hosts Configuration File

After creating and enabling the virtual host for the Laravel app, add the host to your hosts configuration file.

First, open the hosts configuration file using nano:

sudo nano /etc/hosts

Then add the following to the file:

your-ip-address  ServerName-specified-in-the-virtual-host-file

In our case, this would be:

192.168.56.4   laravel.example.com

If you are doing this on a virtual machine, you should add 192.168.56.4 laravel.example.com to the hosts configuration file of the host machine. You may not be able to test the deployed app on the host machine’s browser using the domain name if you don’t do this.

Note that, as long as your virtual machine and host machine are on the same private network, your virtual machine’s IP address will work on the host machine’s browser even if you do not add it to the host machine’s hosts config file.

Create a Database for the App

When you install MySQL, the default authentication plugin for root will most likely be auth_socket . auth_socket is not a safe authentication plugin in production as it can allow unauthorized access to the database. So, to keep your root user safe, you must change the authentication plugin and create a password for root. To do these, run:

sudo mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'mysql_root_password';"

If you are not doing this for production, you may skip the step above and move to the next step.

To create a database for the app, log in to MySQL:

mysql -u root -p

After running the command above, you will get a prompt to enter a password. If you created a password for root, enter it.

When logged into MySQL, run the following command to create a database:

CREATE DATABASE LaravelApp;

You can use any name for the database, but we chose to use LaravelApp. Avoid using special characters in the database name.

After creating the database, run the following command to confirm that the database has been created:

SHOW DATABASES;

You should get a response like this:

As you can see, ‘LaravelApp’ is on the list. This confirms that the database has been created.

Edit the App’s Environment Variables

After creating the database, go back to the project directory:

cd /var/www/html/laravel-realworld-example-app

Then open the .env file using nano:

sudo nano .env

The database settings of the .env file of this repo looks like this by default:

DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=laravel-realworld
DB_USERNAME=sail
DB_PASSWORD=password

Edit the settings to:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=LaravelApp
DB_USERNAME=root
DB_PASSWORD=

For the DB_DATABASE config, use the name of the database you created in the previous step. For us, it is “LaravelApp”.

For the DB_USERNAME config, the default user is root. But you can create a new MySQL user and use that in place of root.

By default, there is noDB_PASSWORD for root, so we leave it blank. If you created a password for root or the MySQL user you created, enter that password here.

Seed the App’s Database

After editing the .env file, seed the database using the following command:

php artisan migrate --seed

Change Permissions to the Laravel Log File

sudo chown www-data:www-data /var/www/html/laravel-realworld-example-app/storage/logs/laravel.logsudo chmod 755 /var/www/html/laravel-realworld-example-app/storage/logs/laravel.log

Test the App in Your Browser

After you’ve successfully seeded the database, you can test the app in your browser by going to any of these URLs:

laravel.example.com/api/articles

laravel.example.com/api/tags

If you get a JSON response like any of the pictures below, you are done.

--

--