How to set up an Apache2 virtual host with Laravel on Ubuntu



This content originally appeared on DEV Community and was authored by Jrius Kaxibwe

To set up an Apache2 virtual host with Laravel on Ubuntu, follow these steps:

1. Install Apache2

If not already installed, install Apache2 with the following command:

sudo apt update
sudo apt install apache2

2. Install PHP and Required Extensions

Make sure you have PHP and the required extensions for Laravel installed:

sudo apt install php libapache2-mod-php php-mysql php-xml php-mbstring php-curl

3. Configure Apache for Virtual Hosts

Ensure that the mod_rewrite module is enabled, as Laravel relies on it:

sudo a2enmod rewrite

Restart Apache to apply the changes:

sudo systemctl restart apache2

4. Set Up Laravel Directory

Place your Laravel project in the /var/www/ directory or wherever you prefer. Here’s an example:

sudo mv /path/to/your/laravel-project /var/www/laravel

Make sure the Apache user has the correct permissions:

sudo chown -R www-data:www-data /var/www/laravel
sudo chmod -R 755 /var/www/laravel

5. Create Virtual Host Configuration

Create a virtual host configuration file for your Laravel project:

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

Add the following content (replace example.test with your domain):

<VirtualHost *:80>
    ServerName example.test
    DocumentRoot /var/www/laravel/public

    <Directory /var/www/laravel/public>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/laravel_error.log
    CustomLog ${APACHE_LOG_DIR}/laravel_access.log combined
</VirtualHost>

6. Enable the Virtual Host

Enable the new virtual host:

sudo a2ensite laravel.conf

Then, disable the default site if necessary:

sudo a2dissite 000-default.conf

7. Update /etc/hosts

Edit your /etc/hosts file to map your domain to localhost:

sudo nano /etc/hosts

Add the following line:

127.0.0.1   example.test

8. Restart Apache

Finally, restart Apache to apply all the changes:

sudo systemctl restart apache2

9. Test in Browser

Visit http://example.test in your browser, and you should see your Laravel application.

Let me know if you encounter any issues!


This content originally appeared on DEV Community and was authored by Jrius Kaxibwe