Installing Composer on cPanel servers

Tutorial: How to use Composer
I. Composer 101
II. Installing Composer on cPanel servers
III. Working with Composer & Examples

With a tool like Composer it is generally best to have the ability to run it as any user on the server and from any directory. This is generally referred to as being ‘globally installed’ as any user can access the tool from any location. In this guide we will detail how to install Composer globally on a cPanel based server.

Pre-Flight Check

  • These instructions are intended specifically for installing Composer on a cPanel based server running WHM prior to version 58.
  • We’ll be logging in as root to a Liquid Web Fully Managed cPanel server.
As of WHM version 58 cPanel is now including a version of Composer by default, this can be accessed by simply calling composer from command line.
For more details see: What’s New in WHM 58 & What to Look For

Step #1: Get the installer

Acquire the Composer installer script with the following commands:

$ EXPECTED_SIGNATURE=$(wget https://composer.github.io/installer.sig -O - -q)
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '$EXPECTED_SIGNATURE') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

The above commands will: get the installer’s signature, get the installer file, verify the download by checking the signature and finally confirm if the installer is valid, or corrupt. You should see output similar to:

Installer verified

Step #2: Run the installer

To run the installer in a manner that will install composer globally run the following command:
php ./composer-setup.php --install-dir=/usr/local/bin --filename=composer

With this command run as root composer should now be globally installed on the server.

Step #3: Verify the install

In order to verify the composer installation was successfully you will want to do the following to test. First, as root, run the following command:
# composer -V

You should see something similar to:

Composer version 1.2.0 2016-07-19 01:28:52

If that checks out, you will then want to verify Composer works for the users as well. To confirm composer is working for the cPanel accounts SSH into your server as a cPanel user and do the same:

$ composer -V

You should see something similar to:

Composer version 1.2.0 2016-07-19 01:28:52

Composer 101

Tutorial: How to use Composer

Composer is a dependency manager for PHP, written in PHP. Specifically, it’s used to simplify the process of using PHP libraries in your projects. The use can range from getting a framework, including a library class, or open source projects; generally these packages are downloaded by Composer and then implemented by a developer in a website’s code.

Examples being: Silex MicroFramework, the infamous PHPMailer class, Laravel Framework, and many more – all of which can be found on Composer’s main repository Packagist.

To find a more documented list of classes, libraries, or frameworks that are available you can review the Packagist site.

So How Do I Use This Tool?

To use Composer you will want to be logged in via SSH, or TTY, and you will run the following command:

$ composer require {somepackage}

This will issue a command for Composer that essentially states you are requiring the provided package name in the current folder. What this does is requests the package given; if found, it then creates the following:

  • composer.json
  • /vendor
  • composer.lock

Ok, so that downloaded the necessary PHP files to use the library, or package, you are requesting. It created a “/vendor” folder where it downloaded all the ‘library’ files. Great, you’ve got the necessary files, but now what?

How Do I Use the Composer-provided Libraries?

To use Composer-provided libraries you may need to understand Composer a little bit better first; specifically you’ll need to understand how Composer’s autoloader works. Think of this as your ‘crash course’ to the autoloader.

To make more sense of this concept let’s go back to our ‘What is Composer’ section; as we know Composer is a “Dependency Manager for PHP”. This means it’s getting you a library, or some code, needed for your site or project. It will also grab any libraries, or other requirements, needed by that library to function – hence the name “dependency management tool”, it’s doing that part in the background for you. In order to make use of this tool, you need to make sure you’re properly including everything that is required.

That’s where Composer’s Autoloader comes in, the autoloader is a script which includes and loads any needed dependencies. Long story short, Composer will create for you a file called: “autoload.php” that is located in the “/vendor” folder it creates. This file is all you need to use/re-use the libraries, classes, or etc, that you are getting from Composer.

Using the Autoloader

In order to use a Composer-provided library you will simply need to add the following to your script:
require 'vendor/autoload.php';

Keep in mind that the part within single quotes should be the relative path to this file; this may need adjusting depending on how you are doing things.

Final Tips When Using Composer

Ultimately, including the `require autoloader.php` line allows you to utilize the packages, classes, libraries, etc, that you are asking composer to get for you. These dependencies are downloaded locally to where the ‘require’ command was run.

When working on cPanel servers you should only run and use composer as the cPanel user requiring those dependencies. Doing this will ensure the files are downloaded in a manner that keeps proper user ownership and permissions. However, if you accidentally run Composer as the root user you will simply need to adjust the file permissions and ownership accordingly.

While the usage of Composer is mainly done by developers, if you are experiencing any issues with file ownership, permissions, or if you have any questions feel free to contact our Heroic Support®.

Enabling Let’s Encrypt for AutoSSL on WHM based Servers

With the recent release of cPanel & WHM version 58 there has been the addition of an AutoSSL feature, this tool can be used to automatically provide Domain Validated SSLs for domains on your WHM & cPanel servers.

Initially this feature was released with support provided for only cPanel (powered by Comodo) based SSL certificates, with the plans to support more providers as things progressed. As of now, cPanel & WHM servers running version 58.0.17, and above, can now also use Let’s Encrypt as an SSL provider. More information on Let’s Encrypt can be found here.

Pre-Flight Check

  • These instructions are intended specifically for a managed Liquid Web server with cPanel.
  • The server should be running cPanel & WHM version 58.0.17, or higher.
  • Command line and root level access via SSH will be necessary to follow this tutorial.

Step #1: Enable Let’s Encrypt Auto SSL provider!

In order to install the Let’s Encrypt AutoSSL provider plugin you will simply log in to the server as the root user via SSH and execute the following command:

# /scripts/install_lets_encrypt_autossl_provider

Running this will add and install the necessary RPM files in order to support Let’s Encrypt as an AutoSSL provider. The command should yield results similar to the following:

Installed the cpanel-letsencrypt RPM! AutoSSL can now use Let’s Encrypt.

Step #2: Verify your work

To double check that this has been successful you can simply pull up WHM and load the ‘Manage AutoSSL’ page. Upon loading this page you should see a list similar to the following screenshot.

WHM AutoSSL w/ Let's Encrypt
If your server’s ‘Manage AutoSSL’ page shows the same options as above you have successfully enabled Let’s Encrypt for AutoSSL.

One thing to keep in mind is that there are some domain and subdomain limits that are enforced by Let’s Encrypt. More details on that can be found in cPanel documentation here: Manage AutoSSL – Domain and rate limits.

How to Disable MySQL Strict Mode

MySQL’s, and MariaDB’s, strict mode controls how invalid or missing values in data changing queries are handled; this includes INSERT, UPDATE, and CREATE TABLE statements. With MySQL strict mode enabled, which is the default state, invalid or missing data may cause warnings or errors when attempting to process the query.

When strict mode is disabled the same query would have its invalid, or missing, values adjusted and would produce a simple warning. This may seem like the preferred result, however with strict mode disabled certain actions may cause unexpected results; for instance, when the value being inserted exceeds the maximum character limit it will be truncated to fit the limit.

There are various reasons why MySQL’s strict mode may need to be disabled, however the most common is when a server is running WHMCS — this is a requirement of that tool.

Pre-Flight Check

  • These instructions are intended specifically for disabling MySQL strict mode on a managed Liquid Web server with cPanel.
  • The server should be running either MySQL 5.6/5.7 or MariaDB 10.x
  • Command line and root level access via SSH will be necessary to follow this tutorial.

Step #1: Make Backups, Always!

Whenever modifying files on a server it’s always best practice to take some form of a backup beforehand. This ensures you have a way to revert changes if something goes awry; it’s also beneficial because it helps track when and what changes were made.

While logged into SSH with the root user, do the following:

cp -a /usr/my.cnf{,.strict.bak}
cp -a /etc/my.cnf{,.strict.bak}

The above command uses ‘BASH brace expansion’ in order to make a backup copy of the file in its original directory.

Step #2: Disable MySQL Strict Mode

Depending on the server and the current configurations you may need to edit one, or both, of the following files on the server. Generally, the relevant configuration lines are only in one of them, however, it could be in either one without causing issues; so generally it’s best to check both.

To edit the files, you will open the file with your favorite command line editor. In this example, we use ‘vim’.

vim /usr/my.cnf
vim /etc/my.cnf

In vim, you can press “a” or “i” to enter text insertion mode; pressing the escape key (Esc) on your keyboard returns you to command mode. For a refresher on editing files with vim, see our New User Tutorial: Overview of the Vim Text Editor.

Within each file above you will be looking for a line with the following content:

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

If you find a line similar to the above that is setting the `sql_mode` variable then you will need to replace it with the following line to disable MySQL strict mode.

sql_mode=""

Once this adjustment has been made, or you’ve confirmed the file does not need to be adjusted you will then save and close the file.

Step #3: Restart the MySQL Service

Finally, to make these changes effective you will need to restart the MySQL service as it will only read the configuration files when it initially loads up. In order to force MySQL to use the new configuration files you will do the following:

For CentOS 7 servers:
systemctl restart mysql

For CentOS 6 and prior:
/etc/init.d/mysql restart

After issuing this command on the server the MySQL service will be restarted and will load the changes made. If all the directions were followed and completed, then MySQL strict mode should now be disabled.

To verify that the process was completed properly you can run the following:

mysql -e "SELECT @@sql_mode;"

The output may look similar to the following:

+--------------------------------------------+
| @@sql_mode
+--------------------------------------------+
| NO_AUTO_CREATE_USER
+--------------------------------------------+

If you have any questions or are not comfortable making these changes yourself, please feel free to contact Heroic Support®.

How to Access cPanel File Manager

 
This tutorial assumes you’ve already logged in to cPanel.

  1. Scroll down a little to find the File Manager link. Now click on File Manager.

    filemgr-cpanel-1-access-scene3_1

    Notice that File Manager has been opened in a new tab of your browser.
    You’re now in the File Manager area.

  2. To log out of the File Manager, just close the tab.

    filemgr-cpanel-1-access-scene4_1

    Now you can log out of cPanel. Click the log out button at the top of the screen.

    filemgr-cpanel-1-access-scene6_1

  3. Click on Logout to log out of cPanel.

 

Understanding Menu Items in File Manager

 
This tutorial assumes you’ve already logged in to cPanel’s File Manager.
The cPanel File Manager has powerful features to manage your files. Let’s check all of the features you can use in this web-based file manager.

  1. You can create new files and new folders with these menu items.

    filemgr-cpanel-2-menuitems-scene2a

    You can copy files and folders into  the same or other folders. You can also move them easily.

    filemgr-cpanel-2-menuitems-scene2b

  2. To delete files, simply click on Delete.

    filemgr-cpanel-2-menuitems-scene2c

    An FTP client is no longer needed to upload and download files and folders. You can simply do it via these buttons!

    filemgr-cpanel-2-menuitems-scene2d

    You can even edit text based files like HTML, PHP and text files with the help of these menu items! No need to use web page editors for small changes.

    filemgr-cpanel-2-menuitems-scene2e

  3. Some web based script installations will require you to change file and folder permissions, which you can do with this button.

    filemgr-cpanel-2-menuitems-scene3a

  4. View files by clicking this button.

    filemgr-cpanel-2-menuitems-scene3b

  5. Have too many files to upload? Just compress it and upload it in a ZIP file and extract it with this button.

    filemgr-cpanel-2-menuitems-scene3c

  6. Too many files to download? Just compress it with this button.

    filemgr-cpanel-2-menuitems-scene3d

  7. This area shows the file and folder structure of your website.

    filemgr-cpanel-2-menuitems-scene4a

  8. This area shows the contents of the selected file or folder (in this case, the public_html folder).

    filemgr-cpanel-2-menuitems-scene4b

  9. These navigation buttons let you go to other folders, reload file lists and select or deselect files and folders.

    filemgr-cpanel-2-menuitems-scene4d

  10. You can also search files here.

    filemgr-cpanel-2-menuitems-scene6_1

    We will learn all these navigation items in detail in the next tutorials.

 

Understanding File Structure in File Manager

 
This tutorial assumes you’ve already logged in to cPanel’s File Manager.

  1. All of your website’s files should be in the public_html folder. When you are in File Manager, always select it first to access your files.

    filemgr-cpanel-3-structure-scene02_1

  2. You can find which folder you are accessing now by checking the path here.

    filemgr-cpanel-3-structure-Scene3b

    The public_ftp folder has all your public ftp account data.

  3. Click on public_ftp to continue.

    filemgr-cpanel-3-structure-scene05_1

    Unless you are an advanced user, we strongly advise you not to touch or modify the contents of this folder.

    filemgr-cpanel-3-structure-Scene5b

    You are now in your public_ftp folder. You can see all the data uploaded by anonymous FTP users.

  4. Let’s check the Mail folder. Click on Mail.

    filemgr-cpanel-3-structure-Scene5c

    You are now in the mail folder. Here you’ll see the same folders as you see in your mail client. All of your email data will be stored in these folders.
    Unless you are an advanced user, we recommend that you do not modify the contents of this folder either.
    Now let’s have a glance at the other folders.
    This folder contains the cPanel software data.

    filemgr-cpanel-3-structure-Scene7a

    This folder contains encrypted passwords.

    filemgr-cpanel-3-structure-Scene7b

    As the folder name suggests, this folder is .trash, all deleted files and folders are moved here.

    filemgr-cpanel-3-structure-Scene7c

    This folder contains access logs for your website.

    filemgr-cpanel-3-structure-Scene7d

  5. You can access any of the folders or sub folders directly by typing the path here.

    filemgr-cpanel-3-structure-Scene8a

    Type your desired path and click the Go button.

    filemgr-cpanel-3-structure-Scene8b

    You are now in your desired folder.

 

How To Create New Files and Folders in File Manager

 
This tutorial assumes you’ve already logged in to cPanel’s File Manager.

  1. Click on New File.

    filemgr-cpanel-4-new-scene02_1

    Leave the public_html path as it is because we always need to upload files in this folder. You will learn how to upload files in subfolders a little later.

  2. Type the file name here.

    filemgr-cpanel-4-new-scene03_1

  3. Then click Create New File.

    filemgr-cpanel-4-new-scene04_1

    Your file has been created successfully.

  4. Let’s create a new folder now. Click on New Folder.

    filemgr-cpanel-4-new-scene05_1

  5. Type the folder name here.

    filemgr-cpanel-4-new-scene06_1

  6. Please type the path where you want to create this new folder.In this case, we will keep it as it is.

    filemgr-cpanel-4-new-Scene6a

  7. Click Create New Folder to continue.

    filemgr-cpanel-4-new-Scene6b

    Your folder was created successfully.

    filemgr-cpanel-4-new-Scene7a

    You can also see it in the left margin.

    filemgr-cpanel-4-new-Scene7b

 

How To Copy and Move Files in File Manager

 
This tutorial assumes you’ve already logged in to cPanel’s File Manager.

  1. Select the file you want to copy. Let’s select testfile.html.

    filemgr-cpanel-5-copy-scene02_1

  2. Click on Copy.

    filemgr-cpanel-5-copy-scene03_1

  3. Please type the file destination, in this case, we will use the “test” folder, then click on Copy File(s).

    filemgr-cpanel-5-copy-scene04_1

    You have successfully copied the file. To check it, either double click on the “folder name” in the right margin, or single click on the “folder name” in the left margin.

    filemgr-cpanel-5-copy-scene05_1

    It’s there. You have successfully copied the file.

  4. Now let’s Move newtest.html to thepublic_html folder. Select newtest.html, and click Move file.

    filemgr-cpanel-5-copy-scene07_1

  5. Make sure your path is public_html only. Now click on Move file(s).

    filemgr-cpanel-5-copy-scene08_1

  6. Notice that the newtest.html file is gone from this list. To confirm that it has moved to public_html, click it in the left margin.

    filemgr-cpanel-5-copy-scene10_1

    Congratulations, you have successfully moved the file. You can also copy multiple files by simply pressing the control key, selecting them and dragging the files to the destination folder in the right margin. Or you can move a single file by just dragging it to the destination folder in the left margin.

 

How To Upload and Download Files with File Manager

 
This tutorial assumes you’ve already logged in to cPanel’s File Manager.

  1. Click Upload to go to the upload page.

    filemgr-cpanel-7-upload-scene02_1

    Now let’s explore the content of the page.

    filemgr-cpanel-7-upload-scene03_1

    This info tells you that you have a file size limit of 999.58 MB for this account. This may be different depending on the account type you have. Please note that the upload page will be opened in a new tab of the browser. If checked, this option allows you to overwrite files if they are already found in the same folder.
    You can set permissions for the files here. We will learn about this in the next tutorial. For now leave it as is.

  2. Now let’s upload a file. Click the Browse button to select the file from your computer.

    filemgr-cpanel-7-upload-scene04_1

  3. Now select the file you want to upload. Let’s upload “Chrysanthemum”. Double click it to continue.

    filemgr-cpanel-7-upload-scene05_1

    You will see the file name in the box. The uploading process will start automatically, and soon a progress bar at the bottom right will appear.

    filemgr-cpanel-7-upload-scene07_1

    The process has been started.
    Information will be provided about the upload progress… Such as, the amount uploaded, percentage, estimated time and upload speed.

    filemgr-cpanel-7-upload-scene08_1

  4. Now the upload has completed.

    filemgr-cpanel-7-upload-scene09_1

    Close this tab and go back to the File Manager.

  5. Click Reload to see the file.

    filemgr-cpanel-7-upload-scene10_1

    You have successfully uploaded the file.

  6. Now let’s download a file. Select “testfile.html” by single clicking it, and click the Download button.

    filemgr-cpanel-7-upload-scene12_1

  7. You will see a popup window asking you to open or save the file. Select Save File.

    filemgr-cpanel-7-upload-scene13_1

  8. Good! Now click OK to continue.

    filemgr-cpanel-7-upload-scene14_1

    That’s it! You have successfully downloaded the file.