DEV Community

Cover image for Step by step guide to host your Laravel project on Plesk
massimo colella
massimo colella

Posted on • Updated on

Step by step guide to host your Laravel project on Plesk

My goal is to create a guide for those who have a shared infrastructure and do not have access to cloud computing.
This tutorial expects:

  • An account on a shared hosting, within a Linux Plesk webserver
  • Your Laravel application, ready to be published
  • You are confident with the Plesk control panel

Table Of Contents

Put your code on

There are two methods available to publish your code:

  • uploading your code with an FTP/SFTP/FTPS account
  • publishing through a github repository

Using S/FTP/S

When you setup a subscription on a Plesk environment, this is your initial file system:

Plesk File system

This step depends on the permissions your user has been granted:

If you have full rights, delete the httpdocs and the error_docs folders, then upload your code just inside the “Home directory”, on the main root level.

If you do not have the permissions to delete the original folders, you can work just inside the “httpdocs” folder. Publish your code into the httpdocs folder, then you will set the "/httpdocs/public/" folder as your Document root.

Note: Remember skipping the upload of the local “vendor” and “node_modules” folders, as these will be recreated dynamically on Plesk.

Using a git repo

Please refer to this great article by Michael Lohr:
https://dev.to/michidk/automatically-deploy-websites-to-plesk-using-github-actions-or-gitlab-ci-56gj

Configure the hosting

The web root of your project is initially setup on “httpdocs”. We will change that, matching the Laravel requirements.
Open your Plesk subscription panel, then go to “Hosting settings”. Change the “Document root” value to match the Laravel “public” folder.

Plesk Hosting settings

Composer install everything

Click the “PHP Composer” icon on the control panel, then the “Scan” button, to load the details of your project. Plesk will find your Laravel composer.json file and scan it for the packages to install.
If you have a very small project, you do not have to do more than “installing” the dependencies, by clicking the right button. Else, you will need more memory so, if your hosting provider enables it, click the "Environment variables" in the Composer control panel

Plesk Composer settings

and specify one of these two environment variables with settings like these (for example, 4Gb memory):

memory_limit = 4G
COMPOSER_MEMORY_LIMIT = 4G

If composer does not get enough memory to complete its jobs, you can use unlimited memory limit (if your hosting provider gives you grants):

memory_limit = -1
COMPOSER_MEMORY_LIMIT = -1

Given that a composer install, or update action will not be run so often, I would recommend the "unlimited setting" only as a one-time action, so do not leave it on the server.

Compile with Node.js

The most recent available node version on my Plesk environment is actually the number 17.7.2. I have found a good working setup with the version 14.x. So you can downgrade it (unless you need an exact version), selecting the 14.x version. Follow these steps:

  • Create a file called “.npmrc” on the root file system.
  • Paste this line inside

scripts-prepend-node-path=true

then save it and go to the Node.js settings of your Plesk subscription.

  • Enable the Node.js environment in your subscription;
  • Downgrade Node.js to the 14.x version;

Node.js Control panel on Plesk

  • Click the “NPM Install” button, wait for the modules to be downloaded, then click the “Run Script” button;
  • Type “dev” in the “Script name and parameters” field, then click the “Run” button on the bottom of the modal. If all goes well, you will see an output like this:

Executing Node.js

If Node.js fails to compile your assets, try creating this environment variable instead of using the .npmrc file:

scripts-prepend-node-path=true

We will use the Node.js environment just to compile our js/css/other assets. If your project does not need a running Node.js code to be alive in production mode, we will disable it after the compile actions has finished executing, because Node.js wants to route our http requests instead of Laravel.
Else we will need to setup an app.js routing file:

  • Create a file named “app.js” on the root, inside the “Home directory”.
  • Paste this code inside, modifying it with your main domain, then save it.
var http = require('http');
var fs = require('fs');

// create a http server
http.createServer(function (req, res) {    
        // redirect to your main domain with 301 (Moved Permanently) HTTP code in the response
        res.writeHead(301, { "Location": "HTTPS://WWW.YOURDOMAIN.COM/" });
        return res.end();
}).listen(8085);
Enter fullscreen mode Exit fullscreen mode

Using Artisan commands

We have two ways to execute our artisan commands: ssh terminals inside the browser, or via scheduled tasks.

Artisan via ssh terminals inside the browser

If our subscription allows it, open the SSH terminal and find the right php executable:

find /opt/plesk/php/*/bin/php

This command will give us a response like:

-bash-4.2$ find /opt/plesk/php/*/bin/php
/opt/plesk/php/5.2/bin/php
/opt/plesk/php/5.3/bin/php
/opt/plesk/php/5.4/bin/php
/opt/plesk/php/5.5/bin/php
/opt/plesk/php/5.6/bin/php
/opt/plesk/php/7.0/bin/php
/opt/plesk/php/7.1/bin/php
/opt/plesk/php/7.2/bin/php
/opt/plesk/php/7.3/bin/php
/opt/plesk/php/7.4/bin/php
/opt/plesk/php/8.0/bin/php

Enter fullscreen mode Exit fullscreen mode

So we will execute the artisan commands prepending the right php exec version, this way:

/opt/plesk/php/8.0/bin/php artisan –help

Unfortunately, the web user doesn't have permission to setup custom aliases, because these will be deleted at the end of the CLI session.

Using the Scheduled Tasks

This method could be a bit slower, but it works and could be very useful.

  • Open the Scheduled Tasks panel inside your subscriptions, then add a new Task.
  • Disable the “Active” checkbox, then select the “Run a PHP script” task type.
  • You can browse inside your file system and select the “artisan” file.
  • You can then write your artisan commands in the “argument” field, as in the picture below.

Artisan commands

Executing it will give you the expected output.

Executing artisan commands

You can save your new task without any arguments, and use it opening and changing them, according to your needs. Or you can save different scheduled tasks. I suggest the creation of these main common useful tasks:

  • migrate
  • migrate:rollback
  • optimize:clear

So that, if you need, you can run them with a simple click every time you need.

Remember: if you put your Laravel project in production, your migrate actions will not be available because each time you try to execute the artisan command, it will ask you for a confirmation that you cannot give because the Plesk scheduled task does not interact with the user. So you should put your project in dev mode before executing migrations or any other command that need interactions with you.

I hope you find this post useful, happy coding!

Top comments (0)