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

Jrius Kaxibwe - Sep 13 - - Dev Community

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!

.
Terabox Video Player