How to Install OpenCart on Ubuntu 18.04
7 min read

How to Install OpenCart on Ubuntu 18.04

In this tutorial, you will install OpenCart on Ubuntu 18.04. OpenCart is one of the most popular open-source PHP e-Commerce platforms.
How to Install OpenCart on Ubuntu 18.04

Introduction

Before we begin talking about how to install OpenCart on Ubuntu 18.04, let’s briefly understand - What is OpenCart?

OpenCart is known to all as one of the most popular open-source PHP e-Commerce platforms. It is known for its user-friendly interface and wide range of features.

Its many features include User Management, Multi-Store, Affiliates, Discounts, Product Reviews, Multi-lingual, and multiple Payment Gateways.

In this tutorial, you will learn how to install OpenCart on Ubuntu 18.04 server. We will also address a few FAQs related to the Strapi.

Prerequisites

  • A domain name
  • Nginx installed on your Ubuntu server
  • SSL certificate

Update the system packages to the latest versions and install them using the following command:

sudo apt update && sudo apt upgrade
sudo apt install unzip

Step 1 - Creating MySQL Database

1) You may skip this step if your system already has MySQL or MariaDB. If that's not the case, you can download MySQL 5.7 using the following commands:

sudo apt install mysql-server mysql-client
Note: For new MySQL installations, it is recommended to run the mysql_secure_installation command to improve the security of the MySQL server.

2) After that, login to the MySQL shell using the following command:

sudo mysql

3) Now, run the following SQL Statement from within the MySQL shell to create a new database named opencart:

CREATE DATABASE opencart;

4) After that, create a MySQL user account named opencart, and grant all necessary permissions to it using the following command:

GRANT ALL ON opencart.* TO 'opencart'@'localhost' IDENTIFIED BY 'change-with-strong-password';
Note: Replace change-with-strong-password with a strong password.

5) When done, exit MySQL with the following command:

EXIT;

Step 2 - Installing and Configuring PHP

1) The default version which comes with Ubuntu 18.04 works fine for the installation. Since Nginx is being used as the webserver, you need to install the PHP-FPM package.

The following command will help you with the above process:

sudo apt install php7.2-common php7.2-cli php7.2-fpm php7.2-opcache php7.2-gd php7.2-mysql php7.2-curl php7.2-intl php7.2-xsl php7.2-mbstring php7.2-zip php7.2-bcmath php7.2-soap

2) Once the PHP packages are installed they'll start automatically. You can verify that with the help of the following command:

sudo systemctl status php7.2-fpm

3) The output would show that the FPM service is active and running:

Output

 ● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-02-25 10:45:42 UTC; 53s ago
     Docs: man:php-fpm7.2(8)
 Main PID: 27446 (php-fpm7.2)
   Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
    Tasks: 3 (limit: 505)
   CGroup: /system.slice/php7.2-fpm.service
           ├─27446 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)

4) After that, set the PHP options as per requirement by altering the php.ini file with sed:

sudo sed -i "s/memory_limit = .*/memory_limit = 1024M/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 256M/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/max_execution_time = .*/max_execution_time = 18000/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/;opcache.save_comments.*/opcache.save_comments = 1/" /etc/php/7.2/fpm/php.ini

Step 3 - Installing OpenCart

1) The latest stable version of OpenCart is OpenCart 3.0.3.6 at the time of writing.
Before commencing with the download, create a directory which will store the OpenCart files:

sudo mkdir -p /var/www/html/example.com

2) Now, download the latest version of OpenCart from the OpenCart repository with the following command:

cd /tmp
wget https://github.com/opencart/opencart/releases/download/3.0.3.1/opencart-3.0.3.1.zip

3) Next, extract the OpenCart archive and move the files to the domain's root directory:    

unzip opencart-*.zip
sudo mv /tmp/upload/* /var/www/html/example.com/

4) Next, using the cp command, copy the configuration files:

sudo cp /var/www/html/example.com/{config-dist.php,config.php}
sudo cp /var/www/html/example.com/admin/{config-dist.php,config.php}

5) Grant the required permissions so that the web server has complete access to the site's files and directories.

sudo chown -R www-data: /var/www/html

Step 4 - Configuring Nginx

1) Now, create the following file:

sudo nano /etc/nginx/sites-available/example.com
# Redirect HTTP -> HTTPS
server {
    listen 80;
    server_name www.example.com example.com;

    include snippets/letsencrypt.conf;
    return 301 https://example.com$request_uri;
}

# Redirect WWW -> NON WWW
server {
    listen 443 ssl http2;
    server_name www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    root /var/www/html/example.com;
    index index.php;

    # SSL parameters
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    # log files
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        expires max;
        log_not_found off;
    }

}
Note: Replace example.com with your OpenCart domain and set the correct path to the SSL certificate files. All HTTP requests will be redirected to HTTPS.

2) Check for any syntax errors before restarting:

sudo nginx -t

3) If there are no errors, you will be able to see the following output:

Output

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4) Now, restart Nginx server.

sudo systemctl restart nginx

Step 5 - Completing the OpenCart Installation

1) Once OpenCart is downloaded and the server configuration has been done, you need to proceed to the web based setup.

When you open your browser and enter your domain, you get to see a similar screen as shown below.

OpenCart License Agreement Tab

2) Choose the Language you want, then hit Continue after agreeing to the license agreement.

3) This reveals the information tab:

OpenCart Pre-Installation Tab

4) Click on Continue once you have cross-checked all pre-installation requirements.

5) The next screen will ask you to enter your database connection details. Enter the MySQL username and details you had created.

OpenCart Configuration Tab

6) Next, enter a username, password, and email address for the administration and select Continue.

7) Now, you'll see a page that confirms installation completion.

OpenCart Installation Complete Tab

8) Click on the Login to your Administration button to access your OpenCart admin dashboard. Enter the email and password, and you'll be redirected to the admin dashboard.

9) There will be a pop-up that asks you to move the storage directory outside of the web directory, when you log in for the first time.

OpenCart Security Notification

10) Select the default Automatically Move option and click on the red Move button. The directory where you are moving the storage directory must be accessible by the webserver.

You can now start customizing your OpenCart installation and add new products.

11) You’ll also need to delete the installation directory. So go back to the terminal and type the following rm command:

sudo rm -rf /var/www/html/example.com/install

FAQs to Install OpenCart on Ubuntu 18.04

1) Can I change my opencart version of 3.0.2.0 to 2.0.x.?

Simple: OC 3.x is still bleeding edge and not totally stable. It's not yet suitable for general users. Also, 2.x probably has a lot more available in the way of extensions and support, is long-established.
Don't think there's any "downgrade" path available. It will take a manual installation of the old software, and manual database conversion.

2) What can be done to tackle an installation error?

As a new user, the error 500 you're having, maybe due to your web server's access logs with the most recent event regarding the index 500 on your host console or on your root FTP domain access -> access_logs (or logs) folder.

Conclusion

We hope this detailed guide helped you understand how to install OpenCart on Ubuntu 18.04 server. To learn more about OpenCart installation on Ubuntu 18.04 server, check out the official OpenCart installation document.

If you have any queries, please leave a comment below and we’ll be happy to respond to them for sure.