Introduction:
Deploying a Laravel application on a Virtual Private Server (VPS) can be an excellent choice for hosting your web application. A VPS offers better performance, scalability, and customization compared to shared hosting options. In this article, we will guide you through the process of deploying a Laravel application on a VPS, ensuring a smooth and secure deployment.
Prerequisites:
Before you begin the deployment process, ensure that you have the following prerequisites in place:
- A VPS: Choose a VPS provider that suits your needs and budget. Popular options include DigitalOcean, Linode, AWS, and Vultr.
- SSH Access: Ensure that you have SSH access to your VPS. SSH (Secure Shell) is used for secure remote access to the server.
- Domain Name: Have a domain name registered and pointed to your VPS IP address.
Step 1: Prepare Your VPS
- Connect to your VPS via SSH using your preferred terminal or SSH client.
- Update the system packages using the package manager. For example, on Ubuntu, use the following command:
sudo apt update && sudo apt upgrade
Step 2: Install Required Software
- Install essential software packages required to run Laravel and serve your application:
sudo apt install nginx mysql-server php php-fpm php-mysql composer
- Configure PHP settings to meet Laravel’s requirements. Edit the
php.ini
file as needed.
Step 3: Configure Nginx
- Create a new Nginx server block (virtual host) for your Laravel application. Create a new configuration file in the
/etc/nginx/sites-available/
directory (e.g.,your_domain.com
) and symlink it to thesites-enabled
directory:
sudo nano /etc/nginx/sites-available/your_domain.com
- Add the following configuration to the file, adjusting paths and domain name as needed:
server {
listen 80;
server_name your_domain.com;
root /var/www/your_laravel_project/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; // Adjust the PHP version if necessary
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
- Save the file and exit the text editor.
- Create a symbolic link to enable the server block:
sudo ln -s /etc/nginx/sites-available/your_domain.com /etc/nginx/sites-enabled/
- Test the Nginx configuration for syntax errors:
sudo nginx -t
- If there are no errors, reload Nginx to apply the changes:
sudo systemctl reload nginx
Step 4: Set Up the Database
- Log in to MySQL as the root user:
sudo mysql -u root
- Create a new database and user for your Laravel application:
CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 5: Deploy Your Laravel Application
- Upload your Laravel application files to the VPS, either manually or using version control (e.g., Git).
- Install Composer
sudo apt update
sudo apt install php-cli unzip
cd ~
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
HASH=`curl -sS https://composer.github.io/installer.sig`
echo $HASH
php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
Navigate to the root directory of your Laravel project and install the dependencies using Composer:
composer install --optimize-autoloader --no-dev
- Configure the
.env
file with your database credentials and other necessary configurations. - Generate a new application key:
php artisan key:generate
- Set appropriate permissions for Laravel’s storage and bootstrap/cache directories:
sudo chown -R www-data:www-data storage bootstrap/cache
- Migrate the database and seed if needed:
php artisan migrate --seed
Step 6: Set Up SSL Certificate (Optional)
To secure your website with SSL, consider obtaining a free SSL certificate from Let’s Encrypt.
Conclusion:
Congratulations! You have successfully deployed your Laravel application on a VPS. Your web application should now be accessible at your domain name. By following these steps and best practices, you can ensure a secure and efficient deployment of your Laravel project on a VPS, offering the best performance and scalability for your users. Happy coding!