WF AQ

Knowledge base

Installing Laravel (any version)

Prerequisites

To complete today’s tutorial, you’re only going to need one prerequisite, which is:

  • Admin access to Plesk
  • Plesk System User with ssh access (if not, we create one in this article)
  • The server must have mcrypt installed

Getting Setup

Right, so what do we need to do to set it up? Thanks to the modern UI, and intelligent layout available in Plesk 12, it’s a real no brainer, requiring only a few steps to have it live; which are as follows:

  • Create a Sub-Domain
  • Setup SSH Access
  • Create The Database
  • Install Composer
  • Install Laravel
  • Configure Laravel
  • View The Application
  • Install node and npm

Let’s work through the process now.

Create a Sub-Domain

When you first login to your account, click the Domains in the left-hand side navigation bar, under Hosting Services. To keep this simple, we’re going to create a new sub-domain, specifically under conetix.com.

On the far right of that domain’s row in the table, click “Manage Hosting“. This will display the domain and sub-domain entries, which you can see in the screenshot below.

Above the first entry, you’ll see three buttons: “Add New Domain“, “Add New Subdomain” and “Add New Domain Alias“. Click “Add New Subdomain”, and on the form which appears, enter “laravel” for the subdomain name. The Document root field will be pre-populated, based on the Subdomain name you entered. But at the end, add in “/laravel/public”.

The reason for this is that the command we’ll run to install Laravel creates a sub-directory, called laravel, and the application’s bootstrap file is located in the public directory beneath that.

When it’s finished you’ll be back on the domains page you started at, with your new domain available at the bottom of the list. In the entry, you’ll see all the pertinent details, along with links to more, as well as to make changes.

Setup SSH Access

Now we need to check that SSH access is setup properly, so that we can login and run a few command line scripts. If you’re not too familiar with command line scripts, that’s ok. The one’s I’ve listed here are quite simple and have sufficient documentation, should it be needed.

In the second section, titled “System user“, make sure that the last option, “Access to the server over SSH“, is set to /bin/bash or /bin/sh and click OK at the bottom.

The reason for this is because these scripts need that environment to run in. sh may work, but bash works best for this example. Now you’re ready to login and begin installing the application.

Note: if you’re not sure of the user’s password, either regenerate it or check with your systems administrator.

Create The Database

Now we have one last pre-install step to do, create a database. Back in the sub-domain settings, above the “Show Less” tab, on the right you’ll see a Databases option.

Next to it, click “Add New Database“, where you’ll be directed to create new database form, as in the screenshot below.

On that page, set “Database name” to “admin_laravel“, leave “Create a new database user” checked and set “Database user name“ to “laravel_user“. Set a secure password under “NewPassword/Confirm Password“. Make a note of these details as you will need them later in the article. For “Access control“, choose the first option, “Allow local connections only“, then click OK.

You’ll then be redirected to the databases list, where you’ll see the new database last in the list (or the only one if this is the first). Feel free to inspect it if you like, but there’s no need to to complete this tutorial.

Install Composer

To install composer, login as the Plesk System User we created earlier and cd to ‘/var/www/vhosts/conetix.com/laravel.conetix.com’ substituting ‘conetix.com’ based on your domain setup; then run the command below:

curl -s https://getcomposer.org/installer | php --

This downloads Composer, piping it through to PHP, in the process creating a self-contained file we can use, called composer.phar.

Install Laravel

N.B.: quando il sistema operativo supporta di default una versione php per esempio 5.6 ma sono isntallate più versioni (p.e. 7.0 e 7.1) l’interprete php da utilizzare va puntato direttamente e si trova in 

/opt/plesk/php/[versione]/bin/php

Per esempio:

/opt/plesk/php/[versione]/bin/php ./composer.phar create-project laravel/laravel --prefer-dist


N.B.: se è necessario utilizzare npm una volta installato node.js nel dominio, il path da utilizzare è:

/opt/plesk/node/XX/bin/npm

dove XX è il numero di versione node.js.

 

Now we need to install Laravel. From your current directory, run the command below:

php ./composer.phar create-project laravel/laravel --prefer-dist

Configure Laravel

Assuming that there are no errors, timeouts or permission issues, now we need to configure the setup. So cd to laravel the new directory and using vim or your editor of choice edit app/config/app.php. In the file, set the following options:

Setting Option
debug true
url ‘http://laravel.conetix.com’

Save the changes, then edit app/config/database.php and find the section marked ‘connections’ and make sure the settings are as below for the mysql sub-option (these are the same details used when setting up the database earlier):

Setting Option
host ‘localhost’
database ‘admin_laravel’
username ‘laravel_user’

For the password, insert your generated password. Saving that, you’re now ready to run the default, Laravel, application.

Set encryption key

php artisan key:generate

Result is a kind of :

Application key [base64:k/XGJzTR0vCEP/nVmU866vXAjzYbQoA452AXn5cjIOU=] set successfully.

Open /config/app.php file and set the ‘key’ field as generated before:

‘key’ => env(‘APP_KEY’,’base64:k/XGJzTR0vCEP/nVmU866vXAjzYbQoA452AXn5cjIOU=’),

UPDATE


Può capitare che dopo l’installazione e all’avvio dell’applicazione appaia un messaggio del tipo:


UnexpectedValueException The stream or file “/var/www/html/upsecurit/storage/logs/laravel.log” could not be opened: failed to open stream: Permission denied

In questo caso il problema dipende dai diritti di scrittura su alcune cartelle e da SElinux.

Procedere quindi in questo modo (fonte: https://stackoverflow.com/questions/30306315/laravel-5-laravel-log-could-not-be-opened-permission-denied)

 

Three things need to be done:

  1. The folders Storage and Bootstrap/Cache need to have the right SELinux context. This can be achieved via the following commands.
    semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/<Laravel Site>/storage(/.*)?"
    
    semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/<Laravel Site>/bootstrap/cache(/.*)?"
    
  2. The SELinux context needs to be applied on the directories.
    restorecon -Rv "/var/www/<Laravel Site>/storage"
    
    restorecon -Rv "/var/www/<Laravel Site>/bootstrap/cache"
    
  3. The Apache user needs to have the rights to create files in both directories. This can be achieved via a ACL in CentOS 7.
    setfacl -R -m u:apache:rwX storage/
    
    setfacl -R -m u:apache:rwX bootstrap/cache/
    

The last thing you need to do is enabling SELinux again.