Skip to content

Instantly share code, notes, and snippets.

@alfajrimutawadhi
Created June 16, 2023 06:19
Show Gist options
  • Save alfajrimutawadhi/2b9600ab13eca041e4bd02a6c21424ff to your computer and use it in GitHub Desktop.
Save alfajrimutawadhi/2b9600ab13eca041e4bd02a6c21424ff to your computer and use it in GitHub Desktop.
How to deploy Laravel to server (VPS)

How to deploy Laravel application to Virtual Private Server (VPS)


Some things you need :

  • Laravel project
  • VPS / Virtual Private Server
  • Domain (if any)

Here's how to deploy your Laravel application to the server

  1. Put your Laravel project into source code management (github/gitlab/others)
    Here I will use my company-management reporsitory.
    https://github.com/alfajrimutawadhi/company-management repo

  2. Create a personal access token
    This token is used to clone your project to the server later. You can directly create a personal access token at https://github.com/settings/tokens/new
    Write notes as you wish and check only the repo section, then immediately click generate at the very bottom.
    After success, copy or save the token for later use
    token

  3. Setup server
    You can login to the terminal server using ssh, to install the database and some required dependencies

    • Install the database according to your Laravel database (here I use mysql)

      sudo apt-get install mysql-server
      

      *do not forget to create a database after installing this

    • Install PHP-FPM and some PHP extensions

      sudo apt-get install software-properties-common
      sudo add-apt-repository -y ppa:ondrej/php
      sudo apt update
      

      *according to the PHP version you need

      sudo apt install php8.1-fpm php8.1-common php8.1-mysql php8.1-xml php8.1-ctype php8.1-curl php8.1-gd php8.1-dom php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-soap php8.1-zip php8.1-bcmath php8.1-fileinfo php8.1-pdo php8.1-tokenizer -y
      
    • Install NGINX

      sudo apt install -y nginx
      
    • Install composer

      sudo apt install -y composer
      
  4. Clone Laravel application

    • Copy your repository URL url-repo
    • Clone your repository into the folder /srv
      git clone <URLrepo> /srv/app
      
    • Enter your github username. Fill password section with personal access token that we created earlier (if needed)
  5. Resetup Laravel application

    • Go to your application folder and change the .env.example file to .env

      cd /srv/app
      mv .env.example .env
      
    • Reset .env file (here I use vim)

      vim .env
      

      *Change the APP_DEBUG value to false and fill in the APP_URL with your IP or domain
      Screenshot 2023-03-31 at 18 54 57

    • Added vendor, migration and optimization folders

      composer update
      composer install --optimize-autoloader --no-dev
      php artisan migrate
      php artisan key:generate
      php artisan config:cache
      php artisan route:cache
      php artisan view:cache
      sudo chmod -R 775 public && sudo chmod -R 777 storage
      
  6. Set NGINX configuration

    vim /etc/nginx/sites-enabled/default
    
    • Delete all contents of the file and please paste with the configuration below :
      server {
          listen 80;
          listen [::]:80;
          server_name domain.com; #change with your domain
          root /srv/app/public;
      
          add_header X-Frame-Options "SAMEORIGIN";
          add_header X-Content-Type-Options "nosniff";
      
          index index.php;
      
          charset utf-8;
      
          location / {
              try_files $uri $uri/ /index.php?$query_string;
          }
      
          location = /favicon.ico { access_log off; log_not_found off; }
          location = /robots.txt  { access_log off; log_not_found off; }
      
          error_page 404 /index.php;
      
          location ~ \.php$ {
              fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
              fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
              include fastcgi_params;
          }
      
          location ~ /\.(?!well-known).* {
              deny all;
          }
      }
    • restart NGINX
      nginx -s reload
      

Your Laravel application is accessible now 🥳

laravel


Additional

If you have a domain and you want your web application to be accessible using HTTPS, you can follow the method below:

  1. Enter your Laravel folder

    cd /srv/app
    
  2. Add this code in folder app/Providers/AppServiceProvider.php (here I use vim)

    public function boot()
    {
       if(config('app.env') === 'production') {
          \URL::forceScheme('https');
       }
    }
  3. Modify the Laravel .env file and re-setup config

    vim .env
    

    change APP_URL to https://domain.com
    Screenshot 2023-04-01 at 07 54 22
    then reset config

    php artisan config:cache
    
  4. Install certbot

    sudo apt install certbot python3-certbot-nginx
    
  5. Create SSL certificate

    certbot --nginx -d domain.com
    

    certbot

  6. Reload NGINX

    nginx -s reload
    

Your Laravel web application can be accessed using HTTPS 🚀

final

@NoelisTired
Copy link

Thanks for the guide, trying to find out how my css is working (it isn't right now)

@Bataino
Copy link

Bataino commented Feb 22, 2024

Did you force https ?

Check to see if there's a mixed content error

@alfajrimutawadhi
Copy link
Author

@Bataino yes, i did force to https, but you can do it another way

@Bataino
Copy link

Bataino commented Mar 12, 2024

Yeah...
@alfajrimutawadhi
What's the other way?

I was referring to @NoelisTired If he did force Https probably he should check his .htaccess file.

@alfajrimutawadhi
Copy link
Author

@Bataino , you can implement several third parties or packages in Laravel, one of which is octane

@fabrice-niyongabo
Copy link

What changes are needed when you want to run this on a VPS using a subdomain instead of a domain name?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment