Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Deploy a Laravel App on a VPS

This guide walks you through putting a Laravel application on your own VPS using Ubuntu, Nginx and MySQL. It needs root access.

VPS and Cloud Servers2 min read13 steps

What is Laravel?

Laravel is a popular tool, called a framework, for building websites in PHP. A framework is like a ready-made kit of parts that saves you time. "Deploy" means moving your finished app from your computer to a live server.

The steps below use Ubuntu. Other systems work in a similar way, with different install commands.

Steps

  1. Log in to your VPS over SSH as root or a sudo user.
  2. Update the server and install the needed software:
    sudo apt update
    sudo apt install nginx mysql-server php-fpm php-mysql php-mbstring php-xml php-curl php-zip unzip git -y
    This installs the web server, database, PHP and helper tools.
  3. Install Composer, the tool that downloads Laravel's parts:
    sudo apt install composer -y
  4. Create a database. Open MySQL with sudo mysql, then run:
    CREATE DATABASE laravel_db;
    CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'ChooseAStrongPassword';
    GRANT ALL ON laravel_db.* TO 'laravel_user'@'localhost';
    EXIT;
  5. Upload your app to /var/www/myapp. You can use Git:
    cd /var/www
    sudo git clone YOUR_REPOSITORY_URL myapp
  6. Install the app's parts:
    cd /var/www/myapp
    composer install --no-dev --optimize-autoloader
  7. Copy the settings file and make a key:
    cp .env.example .env
    php artisan key:generate
  8. Open .env with a text editor such as nano .env. Fill in DB_DATABASE, DB_USERNAME and DB_PASSWORD. Set APP_ENV=production and APP_DEBUG=false.
  9. Build the database tables:
    php artisan migrate --force
  10. Give the web server write access to two folders:
    sudo chown -R www-data:www-data storage bootstrap/cache
    sudo chmod -R 775 storage bootstrap/cache
  11. Create a site file for Nginx at /etc/nginx/sites-available/myapp. Point its root to /var/www/myapp/public, set server_name to your domain, and send PHP requests to PHP-FPM.
  12. Switch the site on and reload Nginx:
    sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl reload nginx
    The nginx -t line checks your file for mistakes first.
  13. Point your domain's A record to the VPS IP address. Then open your domain in a browser.
Tip: Always point the web root at the public folder. Never expose the whole app folder, as it holds your private .env file.

Warning: migrate changes your database. Take a backup first if the database already holds real data.

Quick recap

  • Install Nginx, MySQL, PHP and Composer.
  • Create a database and upload the app.
  • Run composer install, set up .env and run migrate.
  • Fix folder permissions and set the Nginx root to public.
  • Point your domain to the server.