DEV Community

Cover image for How to set up an Apache2 virtual host with Laravel on Ubuntu
Julius Junior Kazibwe
Julius Junior Kazibwe

Posted on

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

3. Configure Apache for Virtual Hosts

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

sudo a2enmod rewrite
Enter fullscreen mode Exit fullscreen mode

Restart Apache to apply the changes:

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

5. Create Virtual Host Configuration

Create a virtual host configuration file for your Laravel project:

sudo nano /etc/apache2/sites-available/laravel.conf
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

6. Enable the Virtual Host

Enable the new virtual host:

sudo a2ensite laravel.conf
Enter fullscreen mode Exit fullscreen mode

Then, disable the default site if necessary:

sudo a2dissite 000-default.conf
Enter fullscreen mode Exit fullscreen mode

7. Update /etc/hosts

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

sudo nano /etc/hosts
Enter fullscreen mode Exit fullscreen mode

Add the following line:

127.0.0.1   example.test
Enter fullscreen mode Exit fullscreen mode

8. Restart Apache

Finally, restart Apache to apply all the changes:

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

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!

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay