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

2. Install PHP…


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


Print Share Comment Cite Upload Translate Updates
APA

Jrius Kaxibwe | Sciencx (2024-09-13T14:38:24+00:00) How to set up an Apache2 virtual host with Laravel on Ubuntu. Retrieved from https://www.scien.cx/2024/09/13/how-to-set-up-an-apache2-virtual-host-with-laravel-on-ubuntu/

MLA
" » How to set up an Apache2 virtual host with Laravel on Ubuntu." Jrius Kaxibwe | Sciencx - Friday September 13, 2024, https://www.scien.cx/2024/09/13/how-to-set-up-an-apache2-virtual-host-with-laravel-on-ubuntu/
HARVARD
Jrius Kaxibwe | Sciencx Friday September 13, 2024 » How to set up an Apache2 virtual host with Laravel on Ubuntu., viewed ,<https://www.scien.cx/2024/09/13/how-to-set-up-an-apache2-virtual-host-with-laravel-on-ubuntu/>
VANCOUVER
Jrius Kaxibwe | Sciencx - » How to set up an Apache2 virtual host with Laravel on Ubuntu. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/13/how-to-set-up-an-apache2-virtual-host-with-laravel-on-ubuntu/
CHICAGO
" » How to set up an Apache2 virtual host with Laravel on Ubuntu." Jrius Kaxibwe | Sciencx - Accessed . https://www.scien.cx/2024/09/13/how-to-set-up-an-apache2-virtual-host-with-laravel-on-ubuntu/
IEEE
" » How to set up an Apache2 virtual host with Laravel on Ubuntu." Jrius Kaxibwe | Sciencx [Online]. Available: https://www.scien.cx/2024/09/13/how-to-set-up-an-apache2-virtual-host-with-laravel-on-ubuntu/. [Accessed: ]
rf:citation
» How to set up an Apache2 virtual host with Laravel on Ubuntu | Jrius Kaxibwe | Sciencx | https://www.scien.cx/2024/09/13/how-to-set-up-an-apache2-virtual-host-with-laravel-on-ubuntu/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.