Setting up an e-commerce OpenCart system

Your Virtual Store

© Lead Image © Helder Almeida, 123rf.com

© Lead Image © Helder Almeida, 123rf.com

Article from Issue 236/2020
Author(s):

With OpenCart, a free, open source e-commerce system, you can put a store online in a matter of minutes.

If you have a brick and mortar store, it makes sense to expand your business and build some presence on the Internet, today's biggest marketplace.

Merchants have many options when it comes to selling their wares online. A popular option is to sell your products on a third-party platform, like Amazon or eBay. Another alternative is to use specialized hosting, which also involves a third party that sets up an e-commerce instance for you and maintains it for a monthly fee.

If you don't want to be dependent on a service provider, hosting your own e-commerce site might be an attractive option. Hosting your own site has the advantage of not binding you to a hosting provider. If your website is in a data center and they change their terms and conditions, you can migrate it somewhere else. If there is a feature you need, you can include it yourself – as long as you have the foresight to use a free and open source (FOSS) e-commerce system.

While there are many FOSS e-commerce systems available, such as PrestaShop, OsCommerce, and Magento, I am going to focus on OpenCart [1], a solution written in PHP that is easy to configure and comparatively cheap to run.

Installation

OpenCart is available as a Bitnami stack [2]. (For more information on Bitnami, see the June 2019 issue of Linux Magazine [3].) Using a Bitnami stack lets you set an instance in a matter of minutes without complications.

If you want to follow a more conventional approach, you will need a web server with FastCGI support, a suitable PHP version, and a database (see Table 1). In addition, you will need the PHP modules listed in Table 2. Configuring a web server is outside of this article's scope, but the Building a LAMP Server box serves as a starting point. OpenCart also works with OpenBSD's custom httpd server, but the setup is not officially documented.

Building a LAMP Server

LAMP (Linux, Apache, MySQL, PHP/Perl/Python) servers are common service stacks for hosting applications such as OpenCart. In this example, I am using MariaDB, a drop-in replacement for MySQL.

Assuming a Devuan environment, the following command will install the necessary components for a LAMP server:

# apt-get install apache2 libapache2-mod-php php php-cgi php-curl php-zip php-gd php-mcrypt php-mbstring php-mysql php-xml mariadb-server mariadb-client

Since you will be hosting a store, TLS encryption is required to ensure secure communication between the server and the customers.

The following commands will enable TLS:

# a2enmod ssl
# a2ensite default-ssl

If Apache does not create a certificate automatically in your distribution, you will have to create it manually [4]. Self-signed certificates are not suitable for production, but they are fine for testing. If you intend to move this server to production, you will need to purchase a certificate from a certificate authority and install it [5]. Alternatively, you can use the Let's Encrypt service to obtain a free certificate [6].

You must configure the database OpenCart will use. The first step is to tighten the database's security by running the following script:

# mysql_secure_installation

Using the above script, enter a root password for MariaDB (keep in mind, it is for MariaDB's root, not the operating system's root user!). Read the prompt carefully and follow the instructions. Disable anonymous database users, remote database root login, and the test database, and then restart the Apache service:

# /etc/init.d/apache2 restart

Next, you create a database and grant OpenCart the privileges necessary to use it. First, open the MariaDB prompt:

# mysql -u root -p
<enter password>

Then create a database called opencart and a user of the same name with access rights. Choose a good password, and use it instead of $PASSWORD in the following example:

MariaDB [(none)]> create database opencart;
MariaDB [(none)]> grant all privileges on opencart.* TO 'opencart'@'localhost' identified by '$PASSWORD';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> quit;

Table 2

Required PHP modules

cURL

Zip

Zlib

GD library

Mcrypt

mbstring

XML

Table 1

OpenCart 3.0.3.2 Requirements

Requirement

Comment

Web server

Apache recommended

PHP version

5.4 or above

Database

MySQL (MySQLi recommended)

Assuming you are installing OpenCart on a LAMP server, you need to download and unpack OpenCart to a suitable directory. OpenCart's source code comes with detailed instructions for unpacking. Listing 1 (a condensed summary) assumes a fresh Apache install on a Devuan system.

Listing 1

Unpacking OpenCart 3.0.3.2

# apt-get install curl unzip
# cd /var/www/html
# rm index.html
# curl -LO https://github.com/opencart/opencart/releases/download/3.0.3.2/opencart-3.0.3.2.zip
# unzip opencart-3.0.3.2.zip
# rm *.json *.lock *.md *.txt *.xml *.zip
# mv upload/* ./
# rm -r upload
# find . -exec chmod 777 '{}' \;
# mv config-dist.php config.php
# mv admin/config-dist.php admin/config.php

The final step is to trigger the OpenCart installation scripts. If you have followed the instructions in Building a LAMP Server, you will have a web server listening on ports 80 and 443 of all network interfaces. Just visit your web server with a web browser and follow the instructions. The wizard will guide you through the installation process (Figure 1). It will check that your web service stack has all the necessary components, request your database access credentials (Figure 2), and let you create an administration account for OpenCart.

Figure 1: The install scripts will ensure that you fulfill the installation prerequisites before proceeding.
Figure 2: OpenCart needs the access credentials to the database in order to work properly.

For additional pointers on using OpenCart, see the official documentation. In particular, take a look at OpenCart's Basic Security Practices [7]. At the very least, you should remove the install directory.

# rm -r /var/www/html/install

Exploring Your New Store

By default, the administration interface is available under the admin directory. It can be reached by visiting https://yourserver/admin, where yourserver is the host's IP address. Once you log in, you will need to complete some additional work in order to have a functional store.

OpenCart's interface is self-explanatory. To get started, go to System | Settings (Figure 3), where you can manage the store's core configuration. Here, the site name can be set, along with email settings and other important parameters. You can also enable Maintenance Mode here. If you select Maintenance Mode, visitors will be prevented from using the site and will see an explanatory message (Figure 4). However, administrators who are logged in can still browse the store during maintenance mode.

Figure 3: In System | Settings, you can select your store's name along with other important parameters.
Figure 4: If Maintenance Mode has been selected, users will see a message letting them know that the store is currently offline for scheduled maintenance.

System | Localisation is where you configure localization, such as tax information and shipping zones. In the Taxes section, you can create broad categories of taxable items to which you can assign different tax rates.

The Catalog section allows you to upload new products to the store's catalog (Figure 5) and to group them in categories. Go to Catalog | Information to specify your site's terms and conditions, along with other legal information you may want to provide to your users.

Figure 5: Upload products under Catalog | Products.

Customizing OpenCart

OpenCart offers some basic features by default (see Table 3). However, in practice, OpenCart falls short on many necessary features. Perhaps the biggest roadblock is lack of support for credit card processors. In addition, you may find the custom theme too generic. OpenCart also does not have the capability of charging additional fees for cash on delivery (COD). Finally, EU Cookie warnings are not supported out of the box.

Table 3

OpenCart's Default Functions

Type

Functions Offered

Payment methods

COD, wire transfer, check, PayPal, BrainTree

Shipment modules

Flat rate, weight-dependent rate, free delivery, pick-up at store

Anti-spam functions

Basic Captcha(not recommended), Google Captcha

Marketing

Discount coupons, affiliate program, gift vouchers, site map

Consequently, if you want to go into production, you will need to install extensions. The OpenCart marketplace [8] offers a wide array of extensions to power your website (Figure 6). You will have to spend money for some extensions (or build your own), but the good news is that OpenCart is much cheaper than alternatives like PrestaShop in terms of extensions.

Figure 6: OpenCart has an active marketplace [8] where you can purchase extensions.

OpenCart's default theme is a bit dry (Figure 7), and you will likely want to change it. OpenCart is built around the Model-View-Controller design pattern, so the interface is implemented separately from the system's core functionality. The interface is supported by Twig templates, which can be modified in Design | Theme Editor (Figure 8). While this works for small modifications, you are better off downloading a new theme if you want to make bigger changes.

Figure 7: The default theme works, but you may find it too generic.
Figure 8: Use the Theme Editor to modify the Twig templates that shape the user interface.

Use the Extensions tab to install and configure your extensions and themes (Figure 9).

Figure 9: OpenCart's functionality can be expanded via extensions.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • e-Commerce Platforms

    If you're looking to start your own web store, you don't need to spend thousands of dollars on software. We test three affordable e-commerce platforms.

  • Integrating Online Payment

    These days you don’t have to build your whole e-commerce system from scratch. Several companies offer online shopping support for vendors who want to stay light on in-house overhead.

  • osCommerce

    sucLooking to build an online store for yourself or for a client? If you’re thinking about cobbling together a customized solution with a database and a home-built shopping cart, don’t bother; look no further than osCommerce.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News