Deploy a Laravel Project to Production with Apache

Deploy a Laravel Project to Production with Apache

"Learn how to deploy a Laravel project to production using the Apache web server. This guide covers the steps needed to set up your Laravel application on a production server, including configuration and optimization tips."

LaravelDeploymentApacheWeb DevelopmentPHPBackendTutorialLinux

Published: March 14, 2026

Deploy a Laravel Project to Production with Apache

What’s up, everyone! In this article, we will deploy a Laravel project to production using the Apache web server, set the correct permissions for the storage and cache folders, configure the .env file, install dependencies, and optimize the application for production.

Prerequisites

Before we start, make sure you have the following:

  • Laravel project: Ready to be deployed.
  • Production server: Apache installed and configured.
  • SSH access: Access to the server.
  • Database server: (e.g., MySQL) Set up and ready to use.

1.0 Prepare Your Laravel Project

This article is for existing Laravel projects, so we will assume you already have a Laravel application ready to deploy.

Check that your project structure looks correct, for example:

├── app
├── artisan
├── bootstrap
├── composer.json
├── composer.lock
├── config
├── database
├── package.json
├── package-lock.json
├── phpunit.xml
├── public
├── README.md
├── resources
├── routes
├── storage
├── tests
├── vite.config.js
├── .editorconfig
├── .env.example
├── .gitattributes
├── .gitignore
└── ..

1.1 Upload Your Laravel Project to the Server

You can use FTP, FileZilla, SCP, or simply clone the repository from GitHub to upload your Laravel project to the server. Make sure you upload all files and folders, including hidden files like .env and .gitignore.

1.2 Connect to Your Server via SSH

You need to connect to your server via SSH to run the commands required to set up your Laravel application. You can use a terminal or an SSH client like PuTTY.

For example, if you are using a terminal, you can run the following command:

ssh astronautmarkus@your-server-ip

Then enter the password for the astronautmarkus user when prompted:

astronautmarkus@your-server-ip's password: ********

After that, you will be connected to your server and can start running the commands needed to set up your Laravel application.

1.3 Install Dependencies

Navigate to your project directory and run the following command to install the required dependencies.

Note: We will name the project folder my-laravel-project, but you can replace it with your own project name.

cd my-laravel-project
composer install --optimize-autoloader --no-dev

This command will install the dependencies defined in your composer.json file, optimize the autoloader for better performance, and skip installing development dependencies.

1.4 Install NPM dependencies if you are using Vite

If you are using Laravel 12, you are probably using Vite as your frontend build tool. In that case, install the npm dependencies and build the assets for production.

npm install
npm run build

These commands compile your assets for production, ensuring your application runs efficiently on the server.

1.5 Copy .env.example to .env

If you haven’t already, create a .env file by copying .env.example. This file contains the environment variables your Laravel application needs to run.

cp .env.example .env

1.5.1 Configure the .env file

Open the .env file in a text editor and update the required environment variables, such as database connection details, application URL, and any other production-specific settings.

For example, you might need to update the following variables:

APP_ENV=production
APP_DEBUG=false
APP_URL=http://your-domain.com
DB_CONNECTION=mysql
DB_HOST=
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_user
DB_PASSWORD=your_database_password

Update these values to match your production environment.

1.6 Generate Application Key

Run the following command to generate a unique application key for your Laravel application:

php artisan key:generate

This command will set the APP_KEY value in your .env file, which is essential for the security of your application.

1.7 Migrate the Database

Run the following command to migrate your database and create the necessary tables:

php artisan migrate:fresh --seed

Note: Use the --seed flag if you want to seed your database with initial data. If you don’t want to seed the database, simply run php artisan migrate:fresh.

Caution: This will delete all existing data in your database.

2.0 Configure Apache

Now that we have our Laravel application set up, we need to configure Apache to serve our application.

2.1 Move the Laravel project to the Apache root directory

The Apache root directory is usually located at /var/www/html. In this example, we will move our Laravel project to a new directory called /var/www/my-laravel-project. I won’t use the html directory because I want to keep things clean and organized, but you can move your project there if you prefer.

sudo mv my-laravel-project /var/www/

2.2 Set the correct permissions

This is a very important step. You need to set the correct permissions for the storage and bootstrap/cache directories and change directory ownership to the Apache user (usually www-data).

sudo chown -R www-data:www-data /var/www/my-laravel-project
sudo chmod -R 775 /var/www/my-laravel-project/storage
sudo chmod -R 775 /var/www/my-laravel-project/bootstrap/cache

Note: After changing ownership to www-data, you should run terminal commands as the www-data user, for example:

  sudo -u www-data php artisan migrate

Use this for all commands you need to run in the terminal. For example, if you want to run npm install or npm run build, run them as the www-data user.

www-data is the user Apache runs as, so we need to give it ownership of the Laravel project files and directories. The chmod command sets permissions for the storage and bootstrap/cache directories to allow read, write, and execute permissions for the owner and group, while allowing read and execute permissions for others.

2.3 Create a new Apache virtual host configuration

Next, we need to create a new Apache virtual host configuration file for our Laravel application. You can create this file in the /etc/apache2/sites-available/ directory.

sudo nano /etc/apache2/sites-available/my-laravel-project.conf

Then, you can use the default virtual host configuration as a template and modify it to fit your Laravel application. Here is an example configuration:

<VirtualHost *:80>
    ServerName your-domain.com
    ServerAlias www.your-domain.com
    DocumentRoot /var/www/my-laravel-project/public
    <Directory /var/www/my-laravel-project/public>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/my-laravel-project_error.log
    CustomLog ${APACHE_LOG_DIR}/my-laravel-project_access.log combined
</VirtualHost>

Note: If you want a more secure setup, check out Deploy a Reverse Proxy with Nginx on a Raspberry Pi - Blog AstronautMarkus, where I share a more secure configuration for Apache and Nginx.

Make sure to replace your-domain.com with your actual domain name and adjust the DocumentRoot and <Directory> paths if you moved your Laravel project to a different location.

2.4 Enable the new virtual host and rewrite module

After creating the virtual host configuration file, you need to enable it and the rewrite module in Apache.

sudo a2ensite my-laravel-project.conf
sudo a2enmod rewrite

If you want the “pro mode”, you can use a symbolic link to enable the site instead of copying the file into the sites-enabled directory:

sudo ln -s /etc/apache2/sites-available/my-laravel-project.conf /etc/apache2/sites-enabled/

Hell yeah!

2.5 Restart Apache

Finally, you need to restart Apache to apply the changes.

sudo systemctl restart apache2

Finally, visit your domain in a web browser to see your Laravel application running in production.

Conclusion

In this article, we walked through the steps to deploy a Laravel project to production using Apache. We covered how to prepare your Laravel project, configure Apache, and set the correct permissions for your application. By following these steps, you can successfully deploy your Laravel application to a production server and make it accessible to users. Remember to keep your application secure and optimized for performance in a production environment. Hell yeah!

Share this post

AstronautMarkus Profile

AstronautMarkusDev (Marcos Reyes M.)