Laravel is an open-source PHP framework that is widely used by developers to build web applications. If you’re planning to deploy a Laravel application on an Ubuntu server with Apache, this article will guide you through the process.
Step 1: Set up an Ubuntu server
First, you need to have a Ubuntu server up and running. You can install Ubuntu on a virtual machine or a cloud server like AWS or DigitalOcean. Make sure your server has Apache, PHP, and MySQL installed. You can follow the official Ubuntu documentation for installing these packages.
Step 2: Install Composer
Composer is a dependency manager for PHP that is used to install Laravel and its dependencies. To install Composer, run the following commands in your terminal:
sudo apt update
sudo apt install curl php-cli php-mbstring git unzip
cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Step 3: Install Laravel
Next, you need to install Laravel on your server. Navigate to the directory where you want to install Laravel and run the following command:
composer create-project --prefer-dist laravel/laravel myapp
This command will create a new Laravel project in a directory named “myapp”.
Step 4: Configure Apache
To configure Apache to serve your Laravel application, you need to create a new virtual host configuration file. Create a new configuration file under /etc/apache2/sites-available/ directory using the following command:
sudo nano /etc/apache2/sites-available/myapp.conf
Add the following configuration to the file:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html/myapp/public
<Directory /var/www/html/myapp>
AllowOverride All
</Directory>
</VirtualHost>
Save the file and enable the virtual host using the following commands:
sudo a2ensite myapp.conf
sudo systemctl reload apache2
Step 5: Set up file permissions
To ensure that Laravel can write to certain directories, you need to set the correct file permissions. Run the following commands:
sudo chown -R www-data:www-data /var/www/html/myapp
sudo chmod -R 755 /var/www/html/myapp
Step 6: Configure .env file
Lastly, you need to configure the .env file in your Laravel project with your database credentials and other settings. Make sure to secure this file and never expose sensitive information.
That’s it! Your Laravel application should now be deployed on your Ubuntu server with Apache. You can now access your application using your server’s IP address or domain name.