Deploying a Laravel application can be a daunting task for developers new to the process. However, with a clear understanding of the steps involved and the available tools, you can streamline the deployment process and ensure a smooth transition from development to production.
Contents
Steps to Deploy Laravel Application:
Step 1: Prepare Your Environment
Choose Your Hosting Provider
Select a hosting provider that meets the needs of your Laravel application. Popular choices include DigitalOcean, Linode, AWS, and shared hosting providers like Bluehost or SiteGround.
Server Requirements
Ensure your server meets Laravel’s requirements:
- PHP >= 8.0
- Composer
- A database (MySQL, PostgreSQL, etc.)
- A web server (Nginx or Apache)
Step 2: Set Up Your Server
Update Your Server
Start by updating your server’s package list and upgrading existing packages.
1 2 |
sudo apt update sudo apt upgrade |
Install PHP and Dependencies
1 |
sudo apt install php php-fpm php-mysql php-xml php-mbstring php-zip unzip curl git |
Install Composer
Composer is essential for managing PHP dependencies.
1 2 |
curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer |
Step 3: Set Up Your Database
Install and Configure MySQL
If using MySQL, install and secure it:
1 2 |
sudo apt install mysql-server sudo mysql_secure_installation |
Create a database and user for your Laravel application:
1 2 3 4 |
CREATE DATABASE your_database_name; CREATE USER 'your_database_user'@'localhost' IDENTIFIED BY 'your_database_password'; GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_database_user'@'localhost'; FLUSH PRIVILEGES; |
Configure Environment Variables
Update your .env file with the database credentials:
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database_name DB_USERNAME=your_database_user DB_PASSWORD=your_database_password |
Step 4: Deploy Your Laravel Application
Clone Your Repository
Clone your Laravel application from your version control system
1 2 |
git clone https://github.com/yourusername/your-repository.git cd your-repository |
Install Dependencies
Install the PHP dependencies using Composer:
1 |
composer install --optimize-autoloader –no-dev |
Set Permissions
Ensure the storage and bootstrap/cache directories are writable:
1 2 3 |
sudo chown -R www-data:www-data /path-to-your-project sudo chmod -R 775 /path-to-your-project/storage sudo chmod -R 775 /path-to-your-project/bootstrap/cache |
Run Migrations and Seeders
Run database migrations and seeders to set up your database schema and initial data:
1 2 |
php artisan migrate --force php artisan db:seed –force |
Generate Application Key
Generate a new application key:
1 |
php artisan key:generate |
Step 5: Configure Your Web Server
Nginx Configuration
Create a new Nginx configuration file:
1 |
sudo nano /etc/nginx/sites-available/your-site |
Add the following configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
server { listen 80; server_name yourdomain.com; root /path-to-your-project/public; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } } |
Enable the configuration and restart Nginx:
1 2 3 |
sudo ln -s /etc/nginx/sites-available/your-site /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl restart nginx |
Apache Configuration
If using Apache, create a new virtual host configuration:
1 |
sudo nano /etc/apache2/sites-available/your-site.conf |
Add the following configuration:
1 2 3 4 5 6 7 8 9 10 11 |
<VirtualHost *:80> ServerName yourdomain.com DocumentRoot /path-to-your-project/public <Directory /path-to-your-project/public> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> |
Enable the site and rewrite module:
1 2 3 |
sudo a2ensite your-site.conf sudo a2enmod rewrite sudo systemctl restart apache2 |
Step 6: Final Steps
Set Up SSL
Secure your site with SSL. You can use Let’s Encrypt for a free SSL certificate:
1 2 |
sudo apt install certbot python3-certbot-nginx sudo certbot –nginx |
Monitor and Maintain
Set up monitoring tools to keep an eye on server performance and application health. Tools like New Relic, Sentry, or even simple log monitoring can be valuable.
Conclusion:
Congratulations! Your Laravel application should now be up and running on your server. By following these steps and leveraging the right tools, you can successfully deploy your application and ensure optimal performance. Consider hiring experienced Laravel developers for automated deployments, setting up continuous integration, and regularly updating your dependencies and server packages to keep everything secure.
Happy Coding!