How to Set Up a Laravel Application on Your VPS

Laravel is a powerful PHP framework for building web applications. This guide will walk you through setting up Laravel on your VPS.


Step 1: Update Your System

Start by updating your system to ensure you have the latest packages.

  • Run the following commands:
    sudo apt update && sudo apt upgrade -y
    

Step 2: Install Required Dependencies

Laravel requires PHP, a web server (NGINX or Apache), Composer, and a database like MySQL.

  • Install PHP and extensions:
    sudo apt install php-cli php-mbstring php-xml php-bcmath php-curl unzip -y
    
  • Install a web server (e.g., NGINX):
    sudo apt install nginx -y
    
  • Install MySQL:
    sudo apt install mysql-server -y
    
  • Install Composer (dependency manager for PHP):
    curl -sS https://getcomposer.org/installer | php
    sudo mv composer.phar /usr/local/bin/composer
    

Step 3: Secure MySQL and Create a Database

Secure MySQL and set up a database for Laravel.

  • Run the MySQL secure installation wizard:
    sudo mysql_secure_installation
    
  • Log in to MySQL:
    sudo mysql -u root -p
    
  • Create a database and user:
    CREATE DATABASE laravel_db;
    CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'securepassword';
    GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;
    

Step 4: Download and Set Up Laravel

  • Navigate to your web directory:
    cd /var/www
    
  • Use Composer to create a new Laravel project:
    composer create-project --prefer-dist laravel/laravel laravel-app
    
  • Assign permissions to the Laravel directory:
    sudo chown -R www-data:www-data /var/www/laravel-app
    sudo chmod -R 775 /var/www/laravel-app/storage
    sudo chmod -R 775 /var/www/laravel-app/bootstrap/cache
    

Step 5: Configure the .env File

  • Navigate to the Laravel directory:
    cd /var/www/laravel-app
    
  • Open the .env file to configure your database settings:
    nano .env
    
  • Update the database configuration as follows:
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=laravel_db
    DB_USERNAME=laravel_user
    DB_PASSWORD=securepassword
    
  • Save and exit.

Step 6: Configure NGINX to Serve Laravel

Set up an NGINX server block for your Laravel application.

  • Create a new server block:
    sudo nano /etc/nginx/sites-available/laravel-app
    
  • Add the following configuration:
    server {
        listen 80;
        server_name yourdomain.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/php7.4-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }
    
  • Enable the server block and test the configuration:
    sudo ln -s /etc/nginx/sites-available/laravel-app /etc/nginx/sites-enabled/
    sudo nginx -t
    
  • Reload NGINX:
    sudo systemctl reload nginx
    

Step 7: Run Laravel Migrations

  • Navigate to the Laravel project directory and run migrations:
    cd /var/www/laravel-app
    php artisan migrate
    

Step 8: Test Your Laravel Application

  • Open your browser and navigate to your domain (e.g., http://yourdomain.com).
  • You should see the Laravel welcome page.

Conclusion

You have successfully set up a Laravel application on your VPS. Laravel is now ready to serve your web application. For enhanced performance and security, consider setting up HTTPS and caching solutions.

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