Deploying a Laravel project on an Apache web server running on a Linux server is a crucial step in bringing your web application to life. Laravel, a popular PHP framework, provides a powerful and efficient way to build web applications. In this tutorial, we'll walk you through the process of deploying a Laravel project on an Apache web server, specifically on a Linux-based server.
Prerequisites
Before you begin, make sure you have the following:
-
A Linux Server: You can use a virtual private server (VPS) from providers like DigitalOcean, AWS, or a physical server running a Linux distribution like Ubuntu, CentOS, or Debian.
-
SSH Access: Ensure you can access your server via SSH. If you're using a VPS, you should have received SSH credentials when setting up the server.
-
Laravel Project: Have a Laravel project ready to deploy. If you haven't created one yet, you can follow the official Laravel documentation to get started.
-
Composer: Composer is a PHP dependency manager. Make sure it's installed on your server. You can install it by following the instructions here.
Step 1: Connect to Your Server
Use your terminal to connect to your server via SSH. Replace your_username
and your_server_ip
with your actual credentials:
ssh your_username@your_server_ip
Enter your password or SSH key passphrase when prompted.
Step 2: Update the Server
Before deploying your Laravel project, it's essential to ensure that your server is up-to-date. Run the following commands to update the package list and upgrade installed packages:
sudo apt update
sudo apt upgrade
For CentOS, use yum
instead of apt
:
sudo yum update
sudo yum upgrade
Step 3: Install Composer
Now, we need to install composer by the following command:
sudo apt-get install git composer -y
Step 3: Install Apache and PHP
Laravel runs on PHP, so you'll need to install both Apache web server and PHP. Use the following commands for Ubuntu:
sudo apt install apache2 php php-cli php-mbstring php-xml php-mysql php-json php-zip
For CentOS:
sudo yum install httpd php php-cli php-mbstring php-xml php-mysql php-json php-zip
After installation, start the Apache service and enable it to start on boot:
sudo systemctl start apache2
sudo systemctl enable apache2
Step 5: Download Laravel
Execute the following commands in your DocumentRoot for deploying a new Laravel application.
cd /var/www/html
composer create-project --prefer-dist laravel/laravel example-app
Now update the .env file
cd /example-app
cp .env.example .env
php artisan key:generate
Step 6: Configure Apache for Laravel
To host your Laravel project on Apache, you need to configure a virtual host. Create a new Apache configuration file for your project. Replace your_domain
with your actual domain or IP address, and your_laravel_directory
with the path to your Laravel project directory:
sudo nano /etc/apache2/sites-available/your_domain.conf
Add the following configuration to the file:
<VirtualHost *:80>
ServerAdmin webmaster@your_domain
ServerName your_domain
DocumentRoot /var/www/html/your_laravel_directory/public
<Directory /var/www/html/your_laravel_directory>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save the file and exit the text editor. Then, enable the virtual host and reload Apache:
Step 7: Set Permissions
Next, you need to grant the web server user (typically www-data
on Ubuntu and apache
on CentOS) permission to write to the storage and bootstrap/cache directories within your Laravel project. Run the following commands:
sudo chown -R www-data:www-data /var/www/your_laravel_directory/storage
sudo chown -R www-data:www-data /var/www/your_laravel_directory/bootstrap/cache
Step 8: Test Your Application
Visit your server's IP address or domain name in a web browser. You should see your Laravel application up and running.
Read also: Create New User And Add SSH Login In Linux Server
Conclusion
Congratulations! You've successfully deployed your Laravel project on an Apache web server running on a Linux server. You can now share your web application with the world. Remember to regularly update your server, maintain your Laravel application, and monitor server performance for a smooth user experience. Happy coding!