How to Install Laravel on VPS for PHP Application Development

Laravel is one of the most popular PHP frameworks, known for its elegant syntax, robust features, and scalability. Installing Laravel on a Virtual Private Server (VPS) is ideal for developers looking to harness the power of VPS hosting for their applications, offering improved performance and flexibility. This guide will take you step-by-step through installing Laravel on a VPS, from setting up the server environment to configuring Laravel for deployment.


Step 1: Set Up Your VPS Environment

  • 1.1 Choose a VPS Provider

    • Overview of popular VPS providers (e.g., DigitalOcean, Linode, Vultr, AnonVM) with factors to consider, such as pricing, support, and server locations.
  • 1.2 Connect to Your VPS Using SSH

    • Guide to SSH access: from Windows (using PuTTY) and macOS/Linux (using Terminal).
       
      ssh root@your-server-ip
  • 1.3 Update and Upgrade Your Server

    • Use these commands to update your server packages:
       
      sudo apt update sudo apt upgrade

Step 2: Install Required Software (Apache, MySQL, and PHP)

  • 2.1 Install Apache or Nginx

    • Apache installation:
       
      sudo apt install apache2
    • Set up Nginx (if preferred):
       
      sudo apt install nginx
  • 2.2 Install MySQL for Database Management

    • Commands to install MySQL:
       
      sudo apt install mysql-server
    • Secure MySQL installation:
       
      sudo mysql_secure_installation
  • 2.3 Install PHP and Required Extensions

    • Commands to install PHP (version 8.0 or later recommended) and required modules for Laravel:
       
      sudo apt install php php-mbstring php-xml php-bcmath php-json php-zip php-curl php-mysql

Step 3: Create a Database for Laravel

  • 3.1 Log in to MySQL

     
    sudo mysql -u root -p
  • 3.2 Create Database and User

    • Commands to create a database and user for Laravel:
      sql
       
      CREATE DATABASE laravel_app; CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'your-password'; GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_user'@'localhost'; FLUSH PRIVILEGES;

Step 4: Install Composer

  • 4.1 What is Composer?

    • Explanation of Composer as a dependency manager for PHP, essential for Laravel installation.
  • 4.2 Install Composer

    • Command to download and install Composer globally:
       
      curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer
  • 4.3 Verify Composer Installation

     
    composer -v

Step 5: Install Laravel

  • 5.1 Create a Directory for Laravel

    • Create a project directory:
       
      mkdir /var/www/laravel-app cd /var/www/laravel-app
  • 5.2 Install Laravel via Composer

    • Command to install Laravel:
       
      composer create-project --prefer-dist laravel/laravel .

Step 6: Configure Apache or Nginx for Laravel

  • 6.1 Apache Configuration

    • Create a virtual host file for your Laravel application:
       
      sudo nano /etc/apache2/sites-available/laravel-app.conf
    • Example configuration:
      apache
       
      <VirtualHost *:80> ServerName your-domain.com DocumentRoot /var/www/laravel-app/public <Directory /var/www/laravel-app> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
    • Enable the site and rewrite module:
       
      sudo a2ensite laravel-app sudo a2enmod rewrite sudo systemctl restart apache2
  • 6.2 Nginx Configuration (Alternative)

    • Example configuration for Nginx:
      nginx
       
      server { listen 80; server_name your-domain.com; root /var/www/laravel-app/public; index index.php index.html; 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; } location ~ /\.ht { deny all; } }
    • Restart Nginx:
       
      sudo systemctl restart nginx

Step 7: Set Environment Variables for Laravel

  • 7.1 Configure Database in .env File

    • Open .env in the Laravel root directory:
       
      nano /var/www/laravel-app/.env
    • Update database settings:
      plaintext
       
      DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_app DB_USERNAME=laravel_user DB_PASSWORD=your-password
  • 7.2 Generate Laravel Application Key

     
    php artisan key:generate

Step 8: Testing Your Laravel Installation

  • 8.1 Clear Laravel Cache

     
    php artisan cache:clear
  • 8.2 Set Correct Permissions

    • Set permissions for the storage and bootstrap/cache directories:
       
      sudo chown -R www-data:www-data /var/www/laravel-app sudo chmod -R 755 /var/www/laravel-app
  • 8.3 Access Your Laravel Application

    • Visit http://your-domain.com to test if the application is running.

Step 9: Secure Your VPS and Laravel Application

  • 9.1 Enable SSL with Let’s Encrypt

    • Install Certbot and configure SSL:
       
      sudo apt install certbot python3-certbot-apache sudo certbot --apache
  • 9.2 Secure Server with Basic Firewall Rules

    • Example commands to set up UFW:
       
      sudo ufw allow OpenSSH sudo ufw allow 'Apache Full' # or Nginx Full if using Nginx sudo ufw enable
  • 9.3 Additional Security Recommendations

    • Tips such as disabling root login, regular backups, and updating software.

Conclusion

Installing Laravel on a VPS can greatly enhance the performance and scalability of your PHP applications. By following this guide, you’ve configured your VPS for optimal performance, installed Laravel, and set up a secure environment. Regular maintenance, backups, and security updates will help keep your application running smoothly and securely.

Was this answer helpful? 0 Users Found This Useful (0 Votes)

Powered by WHMCompleteSolution