Skip to content

Instantly share code, notes, and snippets.

@daif
Created November 10, 2019 08:00
Show Gist options
  • Save daif/8f0eac798a289b140f9fa39d90e5335f to your computer and use it in GitHub Desktop.
Save daif/8f0eac798a289b140f9fa39d90e5335f to your computer and use it in GitHub Desktop.
ERPNext installer
#!/bin/bash
# Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
##################################################################
#
# Project: ERPnext 12 installer
# Version: 1.0.0
# Date: 2019-11-10
# Author: Daif Alazmi <daif@daif.net>
#
##################################################################
# Default configurations
TIMEZONE='Asia/Riyadh'
ERP_USER='erpnext'
ERP_PWD=''
SITE_URL='erpnext.daif.net'
SITE_PWD=''
MYSQL_PASS=''
SRVR_ADDR=`curl -s -4 ifconfig.co`
SITE_ADDR=`dig +short $SITE_URL`
SERVER_OS=`/usr/bin/lsb_release -ds| awk '{print $1}'`
SERVER_VER=`/usr/bin/lsb_release -ds| awk '{print $2}' | cut -d. -f1,2`
# Exit if the current user is not root.
[[ $EUID -ne 0 ]] && echo -e "\033[0;31m \n>\n> Error: This script must be run as root! ... \n>\n\033[0m" && exit 1
# Exit if server ip is not equal site ip.
[[ $SITE_ADDR != $SRVR_ADDR ]] && echo -e "\033[0;31m \n>\n> Error: The server IP ($SRVR_ADDR) is not equal the site ($SITE_URL) IP ($SITE_ADDR)! ... \n>\n\033[0m" && exit 1
# Exit if server is not Ubuntu 18.04
[[ $SERVER_OS != 'Ubuntu' || $SERVER_VER != '18.04' ]] && echo -e "\033[0;31m \n>\n> Error: This script required Ubuntu 18.04 ... \n>\n\033[0m" && exit 1
# Exit if ERPNext password is not set
[[ $ERP_PWD == '' ]] && echo -e "\033[0;31m \n>\n> Error: please set ERP_PWD ... \n>\n\033[0m" && exit 1
# Exit if ERPNext admin password is not set
[[ $SITE_PWD == '' ]] && echo -e "\033[0;31m \n>\n> Error: please set SITE_PWD ... \n>\n\033[0m" && exit 1
# Exit if mysql password is not set
[[ $MYSQL_PASS == '' ]] && echo -e "\033[0;31m \n>\n> Error: please set MYSQL_PASS ... \n>\n\033[0m" && exit 1
##################################################################
# 1 - Updating system
##################################################################
echo -e "\033[0;33m \n>\n> Updating system packages... \n>\n\033[0m"
apt -y update
apt -y upgrade
##################################################################
# 2 - Set timezone
##################################################################
echo -e "\033[0;33m \n>\n> Setting timezone to ${TIMEZONE}... \n>\n\033[0m"
timedatectl set-timezone ${TIMEZONE}
timedatectl
##################################################################
# 3 - Installing tools
##################################################################
echo -e "\033[0;33m \n>\n> Installing requirements... \n>\n\033[0m"
apt -y install git build-essential python3 python3-setuptools python3-dev python3-pip wkhtmltopdf supervisor
##################################################################
# 4 - Updating pip
##################################################################
echo -e "\033[0;33m \n>\n> Updating pip to the latest version... \n>\n\033[0m"
wget https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py --force-reinstall
##################################################################
# 5 - Upgrading pip
##################################################################
echo -e "\033[0;33m \n>\n> Upgrading python packages... \n>\n\033[0m"
pip install --upgrade setuptools cryptography ansible pip
##################################################################
# 6 - Installing nodejs, redis, yarn
##################################################################
echo -e "\033[0;33m \n>\n> Installing nodejs, redis, yarn... \n>\n\033[0m"
curl --silent --location https://deb.nodesource.com/setup_10.x | bash -
apt -y install gcc g++ make nodejs redis-server
npm install -g yarn
##################################################################
# 7 - Starting redis-server
##################################################################
echo -e "\033[0;33m \n>\n> Starting redis-server... \n>\n\033[0m"
systemctl start redis-server
systemctl enable redis-server
##################################################################
# 8 - Installing nginx and MariaDB
##################################################################
echo -e "\033[0;33m \n>\n> Installing nginx and mariadb... \n>\n\033[0m"
apt -y install nginx
apt -y install mariadb-server mariadb-client libmysqlclient-dev
##################################################################
# 9 - Installing crudini tool, it is required python 2 :(
##################################################################
echo -e "\033[0;33m \n>\n> Installing crudini tool... \n>\n\033[0m"
apt -y install python-pip
pip2 install --upgrade setuptools
pip2 install wheel
pip2 install iniparse ini
wget -q https://raw.githubusercontent.com/pixelb/crudini/master/crudini -O /usr/bin/crudini
chmod +x /usr/bin/crudini
##################################################################
# 10 - Changing MariaDB configuration
##################################################################
echo -e "\033[0;33m \n>\n> Changing MariaDB configuration... \n>\n\033[0m"
crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld innodb-file-format barracuda
crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld innodb-file-per-table 1
crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld innodb-large-prefix 1
crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld character-set-client-handshake FALSE
crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld character-set-server utf8mb4
crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld collation-server utf8mb4_unicode_ci
service mysql restart
echo -e "Done \n"
##################################################################
# 11 - Securing database
##################################################################
echo -e "\033[0;33m \n>\n> Securing database... \n>\n\033[0m"
mysql -e "DELETE FROM mysql.user WHERE User='';"
mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
mysql -e "DROP DATABASE IF EXISTS test;"
mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';"
mysql -e "UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root';"
mysql -e "UPDATE mysql.user SET Password=PASSWORD('${MYSQL_PASS}') WHERE User='root';"
mysql -e "FLUSH PRIVILEGES;"
echo -e "Database password = ${MYSQL_PASS} \n"
##################################################################
# 12 - Creating erpnext user
##################################################################
echo -e "\033[0;33m \n>\n> Creating ${ERP_USER} user... \n>\n\033[0m"
useradd -m -s /bin/bash ${ERP_USER}
echo ${ERP_USER}:${ERP_PWD} | chpasswd
usermod -aG sudo ${ERP_USER}
echo "${ERP_USER} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/${ERP_USER}
echo -e "Done \n"
##################################################################
# 13 - Installing bench
##################################################################
echo -e "\033[0;33m \n>\n> Installing bench... \n>\n\033[0m"
su ${ERP_USER} -c "cd /home/${ERP_USER}/; git clone https://github.com/frappe/bench bench-repo"
su ${ERP_USER} -c "cd /home/${ERP_USER}/; pip install --user -e bench-repo"
ln -s /home/${ERP_USER}/.local/bin/virtualenv /usr/local/bin/virtualenv
ln -s /home/${ERP_USER}/.local/bin/honcho /usr/local/bin/honcho
ln -s /home/${ERP_USER}/.local/bin/bench /usr/local/bin/bench
##################################################################
# 14 - Installing frappe
##################################################################
echo -e "\033[0;33m \n>\n> Installing frappe... \n>\n\033[0m"
su ${ERP_USER} -c "cd /home/${ERP_USER}/; bench init frappe-bench"
su ${ERP_USER} -c "cd /home/${ERP_USER}/frappe-bench/; bench setup supervisor --yes"
su ${ERP_USER} -c "cd /home/${ERP_USER}/frappe-bench/; bench setup nginx --yes"
ln -s /home/${ERP_USER}/frappe-bench/config/nginx.conf /etc/nginx/conf.d/frappe-bench.conf
ln -s /home/${ERP_USER}/frappe-bench/config/supervisor.conf /etc/supervisor/conf.d/frappe-bench.conf
supervisorctl reread
supervisorctl restart all
service supervisor restart
su ${ERP_USER} -c "cd /home/${ERP_USER}/frappe-bench/; bench restart"
##################################################################
# 15 - Enabling Site based multi-tenancy
##################################################################
echo -e "\033[0;33m \n>\n> Enabling Site based multi-tenancy... \n>\n\033[0m"
su ${ERP_USER} -c "cd /home/${ERP_USER}/frappe-bench/; bench config dns_multitenant on"
echo -e "Done \n"
##################################################################
# 16 - Downloading ERPNext
##################################################################
echo -e "\033[0;33m \n>\n> Downloading erpnext... \n>\n\033[0m"
su ${ERP_USER} -c "cd /home/${ERP_USER}/frappe-bench/; bench get-app --branch version-12 erpnext https://github.com/frappe/erpnext"
##################################################################
# 17 - Creating new site
##################################################################
echo -e "\033[0;33m \n>\n> Creating new site ${SITE_URL}... \n>\n\033[0m"
su ${ERP_USER} -c "cd /home/${ERP_USER}/frappe-bench/; bench new-site ${SITE_URL} --mariadb-root-password $MYSQL_PASS --admin-password $SITE_PWD"
su ${ERP_USER} -c "cd /home/${ERP_USER}/frappe-bench/; bench --site ${SITE_URL} install-app erpnext"
su ${ERP_USER} -c "cd /home/${ERP_USER}/frappe-bench/; bench setup nginx --yes"
service nginx reload
##################################################################
# 18 - Installing lets-encrypt for the site
##################################################################
echo -e "\033[0;33m \n>\n> Installing lets-encrypt... \n>\n\033[0m"
mkdir /etc/letsencrypt/
echo "email=webmaster@${SITE_URL}" >> /etc/letsencrypt/cli.ini
echo "agree-tos = true" >> /etc/letsencrypt/cli.ini
echo "no-eff-email = true" >> /etc/letsencrypt/cli.ini
echo "authenticator = standalone" >> /etc/letsencrypt/cli.ini
su ${ERP_USER} -c "cd /home/${ERP_USER}/frappe-bench/; (sleep 10; echo 'y'; sleep 20; echo 'y'; sleep 20; echo 'y';) | sudo bench setup lets-encrypt ${SITE_URL}"
rm /etc/sudoers.d/${ERP_USER}
@daverjorge46
Copy link

Hi Daif, very good job. I have used the script for an installation and I have not had any errors during the installation process but when launching the website, only the default page of nginx appears. Then I have done the installation manually following the script and I don't get any errors during the process, but the same thing happens, I only see the default page of nginx. I have tried with port 80 and with port 8000, but nothing .. Could you suggest what could be wrong? Thank You,

@daverjorge46
Copy link

I got it. It's Ok, I found it.

@edwardleehl7097
Copy link

I got it. It's Ok, I found it.

how you solve the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment