How to Install OpenCart on Ubuntu 20.04 with LEMP Stack
OpenCart is a well-known, open-source eCommerce platform developed to provide strong functionality, user-friendly handling, and centralized control of online shops through a single backend interface. With OpenCart, you can effortlessly create a comprehensive online store and adapt it using free add-ons to fit your business requirements.
This tutorial walks you through setting up OpenCart on an Ubuntu 20.04 server using the LEMP stack (Nginx, MySQL, PHP). Before beginning the installation, make sure your system fulfills the necessary prerequisites.
Install the Prerequisites
- Deploy an Ubuntu 20.04 LEMP server
- Access your server via SSH and sign in as the root user.
- Update the system packages on your server.
- Optionally, set up a domain name to resolve to your server’s IP address.
Set Up the OpenCart Database
First, log into the MySQL shell.
$ mysql -u root
Create a fresh database to be used by OpenCart.
mysql> create database opencart;
Establish a new MySQL user and assign a robust password to it.
mysql> create user 'admin'@'localhost' IDENTIFIED BY 'YOUR_STRONG_PASSWORD';
Give the newly created user full access permissions to the database.
mysql> GRANT ALL PRIVILEGES ON opencart.* TO 'admin'@'localhost';
Apply the permission changes and exit the MySQL interface.
mysql> FLUSH PRIVILEGES;
mysql> EXIT
Download OpenCart
Download the latest stable OpenCart release from its official GitHub page. In this example, version 3.0.3.8 is used, though it’s advised to verify and fetch the most recent version.
$ wget https://github.com/opencart/opencart/releases/download/3.0.3.8/opencart-3.0.3.8.zip
Unzip the OpenCart archive to extract the contents.
$ unzip opencart-3.0.3.8.zip -d opencart
Transfer the extracted OpenCart files to your web server’s document root. On Nginx, this is typically located at /usr/share/nginx/html
.
$ sudo mv opencart/upload/* /usr/share/nginx/html
Assign ownership of the web root directory to the www-data
user and group to ensure Nginx can access the files.
$ sudo chown -R www-data:www-data /usr/share/nginx/html
Prepare the Installation Files
To begin the OpenCart setup, rename the sample configuration files to their appropriate filenames for a production environment.
$ cd /usr/share/nginx/html
$ sudo mv config-dist.php config.php
$ sudo mv admin/config-dist.php admin/config.php
Configure the Firewall
Enable incoming web traffic by opening HTTP and HTTPS ports on your firewall.
$ sudo ufw allow 80
$ sudo ufw allow 8080
$ sudo ufw allow http
$ sudo ufw allow https
Install a Free SSL/TLS Certificate
As both OpenCart and modern web browsers rely on HTTPS for secure connections, setting up a free SSL/TLS certificate is crucial to prevent browser warnings. Begin by installing Certbot and the necessary Nginx plugin.
$ sudo apt install certbot python3-certbot-nginx
Use Certbot to request a new SSL certificate for your domain name.
$ certbot –nginx -d example.com www.example.com
During the process, you’ll be prompted to enter your email, accept the terms of service, and configure HTTPS preferences. Once complete, a functional SSL certificate will be installed automatically.
IMPORTANT NOTES:
– Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Install OpenCart
Open your browser and navigate to your server’s public IP address or the configured domain name to initiate the OpenCart setup.
http://example.com
On the welcome screen, select “Continue” to agree to the terms and conditions.
OpenCart will automatically verify if the server fulfills all necessary requirements. When all checks pass, click “Continue” to move forward.
Provide the database name, user, and password that you set up in the initial database configuration step of this guide.
OpenCart Database Settings
Click the continue button to finalize the installation. You will then be redirected to the OpenCart admin dashboard, where you can proceed with additional configurations and store customization.
Securing OpenCart
When using OpenCart on a live production environment, it is essential to enhance security by executing the following steps.
First, remove the installation directory to prevent unauthorized access to setup routines.
$ sudo rm -r /usr/share/nginx/html/install
Next, modify file permissions of critical OpenCart files to 644
to reduce the risk of unauthorized modifications.
$ sudo chmod 644 /usr/share/nginx/html/config.php
$ sudo chmod 644 /usr/share/nginx/html/index.php
$ sudo chmod 644 /usr/share/nginx/html/admin/config.php
$ sudo chmod 644 /usr/share/nginx/html/admin/index.php
$ sudo chmod 644 /usr/share/nginx/html/system/startup.php
To protect the administrator area, rename the default admin
directory to a more discreet name of your choice, such as privatemin
.
$ sudo mv /usr/share/nginx/html/admin privatemin
After renaming the directory, the login path for the OpenCart administration panel will change accordingly. In this example, it becomes:
https://example.com/privatemin
You may substitute privatemin
with any custom keyword you prefer to further obscure the login route.
Conclusion
Well done! You have successfully completed the installation of OpenCart on your Ubuntu 20.04 server. You can now begin configuring and launching your new online store by accessing your server’s IP address or the linked domain name in a web browser.