Install OpenCart 2 on an Ubuntu 14.04 VPS with Nginx and PHP-FPM

install-opencart-2-on-an-ubuntu-14-04-vps-with-nginx-and-php-fpmIn this tutorial, we will explain how to install OpenCart 2 on an Ubuntu 14.04 VPS with MariaDB, PHP-FPM and Nginx. OpenCart is an open-source, feature-rich, easy-to-use, and search engine-friendly PHP-based e-commerce solution. This guide should work on other Linux VPS systems as well but was tested and written for Ubuntu 14.04 VPS.

Login to your VPS via SSH

ssh your_user@myVPS

Update the system and install the necessary packages

user@myVPS:~# sudo apt-get update && sudo apt-get -y upgrade
user@myVPS:~# sudo apt-get install python-software-properties software-properties-common git curl openssl vim

Install MariaDB 10.0

user@myVPS:~# sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
user@myVPS:~# sudo add-apt-repository 'deb http://mirror.pw/mariadb/repo/10.0/ubuntu trusty main'
user@myVPS:~# sudo apt-get install mariadb-server

When the installation is complete, run the following command to secure your installation:

mysql_secure_installation

Next, we need to create a database for our OpenCart installation.

mysql -uroot -p
MariaDB [(none)]> CREATE DATABASE opencart;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON opencart.* TO 'opencartuser'@'localhost' IDENTIFIED BY 'opencartuser_passwd';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> \q

Download and extract OpenCart 2

Create a root directory for your website and extract OpenCart 2 zip file

user@myVPS:~# mkdir -p ~/your_shop.com
user@myVPS:~# cd ~/your_shop.com
user@myVPS:~# wget https://github.com/opencart/opencart/archive/2.0.1.1.zip
user@myVPS:~# unzip 2.0.1.1.zip
user@myVPS:~# mv opencart-2.0.1.1/upload/* .
user@myVPS:~# rm -rf opencart-2.0.1.1 2.0.1.1.zip

Install and configure PHP and Nginx

Installing PHP and Nginx is pretty easy, just run the following command:

user@myVPS:~# sudo apt-get install nginx php5-fpm php5-cli php5-mcrypt php5-gd php5-mysqlnd php5-curl
user@myVPS:~# sudo php5enmod mcrypt

Create a new PHP-FPM pool for your user:

user@myVPS:~# sudo vim /etc/php5/fpm/pool.d/your_user.conf
[your_user]
user = your_user  
group = your_user  
listen = /var/run/php5-fpm-your_user.sock  
listen.owner = your_user
listen.group = your_user  
listen.mode = 0666  
pm = ondemand  
pm.max_children = 5  
pm.process_idle_timeout = 10s;  
pm.max_requests = 200  
chdir = /  

Do not forget to change your_user with your username.
Restart PHP-FPM

user@myVPS:~# sudo service php5-fpm restart

Generate SSL certificate:

user@myVPS:~# sudo mkdir -p /etc/nginx/ssl
user@myVPS:~# cd /etc/nginx/ssl
user@myVPS:~# sudo openssl genrsa -des3 -passout pass:x -out opencart.pass.key 2048
user@myVPS:~# sudo openssl rsa -passin pass:x -in opencart.pass.key -out opencart.key
user@myVPS:~# sudo rm opencart.pass.key
user@myVPS:~# sudo openssl req -new -key opencart.key -out opencart.csr
user@myVPS:~# sudo openssl x509 -req -days 365 -in opencart.csr -signkey opencart.key -out opencart.crt

Next, create a new Nginx server block:

user@myVPS:~# sudo vim /etc/nginx/sites-available/your_shop.com
server {
    listen      80;
    server_name www.your_shop.com;

    add_header Strict-Transport-Security max-age=2592000;
    return 301 http://your-shop.com$request_uri;
}

server {
    listen      80;
    server_name your_shop.com;

    root /home/your_user/your_shop.com;

    index index.html index.htm index.php;

    charset utf-8;

    access_log  /var/log/nginx/your_shop.com.access.log;
    error_log   /var/log/nginx/your_shop.com.error.log;

    rewrite /admin$ $scheme://$host$uri/ permanent;

    location / {
         try_files $uri @opencart;
    }

    location @opencart {
        rewrite ^/(.+)$ /index.php?_route_=$1 last;
    }


    location /admin {
        index index.php;
    }

    rewrite ^/sitemap.xml$ /index.php?route=feed/google_sitemap last;
    rewrite ^/googlebase.xml$ /index.php?route=feed/google_base last;
    rewrite ^/download/(.*) /index.php?route=error/not_found last;

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

    sendfile off;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm-your_user.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    location ~ /\.ht {
        deny all;
    }
}


server {
    listen 443 ssl spdy;
    server_name www.your_shop.com;

    ssl on;
    ssl_certificate     /etc/nginx/ssl/opencart.crt;
    ssl_certificate_key /etc/nginx/ssl/opencart.key;

    return 301 https://your-shop.com$request_uri;
}

server {
    listen      443 ssl spdy;
    server_name your_shop.com;

    ssl on;
    ssl_certificate     /etc/nginx/ssl/opencart.crt;
    ssl_certificate_key /etc/nginx/ssl/opencart.key;
    ssl_session_timeout 5m;

    ssl_ciphers               'AES128+EECDH:AES128+EDH:!aNULL';
    ssl_protocols              TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    root /home/your_user/your_shop.com;

    index index.html index.htm index.php;

    charset utf-8;

    access_log  /var/log/nginx/your_shop.com.ssl.access.log;
    error_log   /var/log/nginx/your_shop.com.ssl.error.log;

    rewrite /admin$ $scheme://$host$uri/ permanent;

    location / {
         try_files $uri @opencart;
    }

    location @opencart {
        rewrite ^/(.+)$ /index.php?_route_=$1 last;
    }


    location /admin {
        index index.php;
    }

    rewrite ^/sitemap.xml$ /index.php?route=feed/google_sitemap last;
    rewrite ^/googlebase.xml$ /index.php?route=feed/google_base last;
    rewrite ^/download/(.*) /index.php?route=error/not_found last;

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

    sendfile off;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm-your_user.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    location ~ /\.ht {
        deny all;
    }
}

Do not forget to change your_user with your username.

Activate the server block by creating a symbolic link and restart Nginx:

user@myVPS:~# sudo ln -s /etc/nginx/sites-available/your_shop.com /etc/nginx/sites-enabled/your_shop.com
user@myVPS:~# sudo /etc/init.d/nginx restart

Install OpenCart 2

Create empty configuration files:

user@myVPS:~# cd ~/your_shop.com
user@myVPS:~# touch {admin,.}/config.php

Open your browser, type the address, and follow the installation wizard. During the installation process, you will be asked to provide the mysql database, username, and password (enter the  mysql database, username, and password you created in the previous step).

Enable SSL

After the installation is complete to enable SSL, login to the admin dashboard and go to System -> Settings -> Server Tab and select Use SSL and click save. You also need to make the following modification in the config.php file.

user@myVPS:~# vim ~/your_shop.com/config.php

Change define('HTTPS_SERVER', 'http://your_shop.com/');
to define('HTTPS_SERVER', 'https://your_shop.com/');

That’s it. You have successfully installed OpenCart 2 on your Ubuntu VPS.


Of course, you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

12 thoughts on “Install OpenCart 2 on an Ubuntu 14.04 VPS with Nginx and PHP-FPM”

  1. at service ngnix restart I get

    the name org(dot)freedesktop.policykit1 was not provided by any service files.

    looked online for a solution, found none!

    Reply
  2. Hi thank you for the step by step on installing opencart on ubuntu.

    I went thru all the steps but wen I try to access the website it shows the message on browser:

    Welcome to nginx!

    If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

    For online documentation and support please refer to nginx.org.
    Commercial support is available at nginx.com.

    Thank you for using nginx.

    what went wrong?

    Kind Regards

    Reply
    • Did you create a new Nginx server block as described in the tutorial?

      Do you have other configuration files in /etc/nginx/sites-enabled/ directory?

      Reply
  3. Great tutorial. Thank you very much.

    I had an issue with one line in the site config. I had to remove this to prevent my site going in a request loop.

    return 301 http://your_shop.com$request_uri;

    Reply
  4. Great and detailed tutorial.
    When editing php-fpm pool for the user, what should be the username? the nginx user (www-data)? or the user that I ssh to server?

    Reply
  5. Thanks for such a great informative information share here on Open Cart Install..

    Reply

Leave a Comment