Install Saleor/Ubuntu 18/Nginx

#1

Installation Saleor for Linux

Located origin guideline at: https://saleor.readthedocs.io/en/latest/

Note

If you prefer using containers or have problems with configuring PostgreSQL, Redis and Elasticsearch, try Using Docker for Development instructions.

Prerequisites

Before you are ready to run Saleor you will need additional software installed on your computer.

Python 3

Saleor requires Python 3.5 or later. A compatible version comes preinstalled with most current Linux systems. If that is not the case consult your distribution for instructions on how to install Python 3.6 or 3.7.

Node.js

Version 10 or later is required. See the installation instructions.

PostgreSQL

Saleor needs PostgreSQL version 9.4 or above to work. Use the PostgreSQL download page to get instructions for your distribution.

Gtk+

Some features like PDF creation require that additional system libraries are present.

Debian / Ubuntu

Debian 9.0 Stretch or newer, Ubuntu 16.04 Xenial or newer:

$ sudo apt-get install build-essential python3-dev python3-pip python3-cffi libcairo2 libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info

Fedora

$ sudo yum install redhat-rpm-config python-devel python-pip python-cffi libffi-devel cairo pango gdk-pixbuf2

Archlinux

$ sudo pacman -S python-pip cairo pango gdk-pixbuf2 libffi pkg-config

Gentoo

$ emerge pip cairo pango gdk-pixbuf cffi

Installation

  1. Clone the repository (or use your own fork):

adduser: saleor

git clone https://github.com/mirumee/saleor.git

  1. Enter the directory:

cd saleor/

  1. Install all dependencies:We strongly recommend creating a virtual environment before installing any Python packages.

apt install virtualenv virtualenvwrapper

virtualenv ./venv

source ./venv/bin/activate

sudo pip3 install -r requirements.txt

exit

pip3 install -r requirements.txt

or

python3 -m pip install -r requirements.txt

if pip3 command do not work

  1. Set SECRET_KEY environment variable.We try to provide usable default values for all of the settings. We’ve decided not to provide a default for SECRET_KEY as we fear someone would inevitably ship a project with the default value left in code.

export SECRET_KEY= mysecretkey

Warning: Secret key should be a unique string only your team knows. Running code with a known  `SECRET_KEY`  defeats many of Django’s security protections, and can lead to privilege escalation and remote code execution vulnerabilities. Consult [Django’s documentation](https://docs.djangoproject.com/en/1.11/ref/settings/#secret-key) for details.
  1. Create a PostgreSQL user:See PostgreSQL’s createuser command for details.
Note: You need to create the user to use within your project. Username and password are extracted from the  `DATABASE_URL`  environmental variable. If absent they both default to  `saleor` .

Warning: While creating the database Django will need to create some PostgreSQL extensions if not already present in the database. This requires a superuser privilege.

For local development you can grant your database user the  `SUPERUSER`  privilege. For publicly available systems we recommend using a separate privileged user to perform database migrations.
  1. Create a PostgreSQL databaseSee PostgreSQL’s createdb command for details.
Note: Database name is extracted from the  `DATABASE_URL`  environment variable. If absent it defaults to  `saleor` .
  1. Prepare the database: Edit /saleor/saleor/seting.py to add your user/passwd of PostgreSQL
DATABASES = {
    'default': dj_database_url.config(
        default='postgres://saleor:salor@localhost:5432/saleor',
        conn_max_age=600)}

$ python3 manage.py migrate

  • In this case need grand saleor user on PostgreSQL a superuser privilege

sudo su - postgres

psql

“postgres password”

\du; for see the user on db select the user that do you want be superuser and:

ALTER USER “saleor” with superuser;

Warning: This command will need to be able to create database extensions. If you get an error related to the  `CREATE EXTENSION`  command please review the notes from the user creation step.
  1. Install front-end dependencies:

$ npm install

Note: If this step fails go back and make sure you’re using new enough version of Node.js. Run the below command to upgrade nodejs

npm audit fix

npm install -g npm

  1. Prepare front-end assets:

$ npm run build-assets

  1. Compile e-mails:

npm run build-emails

Run demo store information with below command:

python3 manage.py populatedb

  1. Start the development server:

Create store superuser by:

python3 manage.py createsuperuser

Run demo server by this command:

$ python3 manage.py runserver 0.0.0.0:8000

you will need to add 0.0.0.0 or your FDNS host to /saleor/saleor/setting.py located at

ALLOWED_HOSTS = get_list(
    os.environ.get('ALLOWED_HOSTS', 'localhost,127.0.0.1,0.0.0.0'))
  1. Create init.d script to start runserver at boot

touch /etc/init.d/saleor

add below code to this file:

#! /bin/sh                                                                                          
### BEGIN INIT INFO                                                                                 
# Provides:          Django-Server                                                                  
# Required-Start:    $all                                                                           
# Required-Stop:                                                                                    
# Default-Start:     2 3 4 5                                                                        
# Default-Stop:      0 1 6                                                                          
# Short-Description: Django Server                                                                  
### END INIT INFO                                                                                   

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin                          
                                                                        
# If you need to source some other scripts, do it here                                              

case "$1" in                                                                                        
  start)                                                                                            
    log_begin_msg "Starting Saleor Server"                                                          
    python3 "/home/USER/saleor/manage.py" runserver 0.0.0.0:8000  --insecure &      
    log_end_msg $?                                                                                  
    exit 0                                                                                          
    ;;                                                                                              
  stop)                                                                                             
    log_begin_msg "Stopping Saleor Server"                                                          

    # do something to kill the service or cleanup or nothing                                        

    log_end_msg $?                                                                                  
    exit 0                                                                                          
    ;;                                                                                              
  *)                                                                                                
    echo "Usage: /etc/init.d/saleor {start|stop}"                                            
    exit 1                                                                                          
    ;;                                                                                              
esac  

Run below command to start this script by default

chmod +x /etc/init.d/saleor

update-rc.d /etc/init.d/saleor defaults

  1. Nginx config
    Create new Nginx config file located at:

vi /etc/nginx/conf.d/saleor.conf

and add below line to this file

#
# this is the nginx config for saleor
#

upstream saleor {
    server 127.0.0.1:8000;
}


server {

    # replace 'localhost' with your fqdn if you want to use saleor from remote
    server_name gimitec.com;

    root /home/USER/saleor;

    access_log /var/log/nginx/saleor.access.log;
    error_log  /var/log/nginx/saleor.error.log;

    client_max_body_size 500M;

    location ~ ^/(assets/|robots.txt|humans.txt|favicon.ico) {
        expires max;
    }    

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header CLIENT_IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 300;
        proxy_pass http://saleor;

        gzip on;
        gzip_types text/plain text/xml text/css image/svg+xml application/javascript application/x-javascript application/json application/xml;
        gzip_proxied any;
    }



}

server {
    if ($host = gimitec.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name gimitec.com;
    listen 80;
    return 404; # managed by Certbot

}

nginx -t

Test to make sure syntax is ok

sudo certbot --nginx

To creat SSL for your site with certbot https://certbot.eff.org/lets-encrypt/ubuntubionic-nginx

For this guilde please make sure that your server already with: Python3, postgreSQL, Nodejs, Nginx, Certbot, installed before to do this job, try to “Google” for detail how to install both of them on your Ubuntu 18.