Skip to content

Instantly share code, notes, and snippets.

@anhtran
Last active November 5, 2024 05:02
Show Gist options
  • Save anhtran/07f1c7f45ec0d58d09e1 to your computer and use it in GitHub Desktop.
Save anhtran/07f1c7f45ec0d58d09e1 to your computer and use it in GitHub Desktop.
To setup a Debian 10, 11, 12 server from scratch for Python apps, Go apps (this guide may also work with Debian 8 Jessie, Debian 9 Stretch)
#!/bin/bash
# most of commands should be run with root or sudo
# may add -y flag to ignore comfirmation messages
# adjust files as your demand
# may fix locale issues
export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales
locale-gen en_US.UTF-8
dpkg-reconfigure locales
# update apt cache first
apt update
apt upgrade
apt dist-upgrade
# essentials
apt install -y curl xclip git webp
apt install -y build-essential libssl-dev
apt install -y tcl8.5
apt install -y software-properties-common python-software-properties
apt install -y apt-transport-https
apt install -y software-properties-common dirmngr
# tools
apt install -y vim unzip htop mosh sudo ncdu multitail ncftp tmux rsync zip jq lftp
# Py3 dependencies
apt install -y build-essential
apt install -y libncurses5-dev libncursesw5-dev libreadline6-dev
apt install -y libdb-dev libgdbm-dev libsqlite3-dev libssl-dev
apt install -y libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
apt install -y libcurl4-openssl-dev libpam0g-dev libsystemd-dev
apt install -y zlib1g-dev libsqlite3-dev tk-dev
apt install -y libssl-dev openssl
apt install -y libffi-dev
# certbot
apt install -y python-certbot-nginx
# very useful libs for python modules
apt install -y libxml2-dev libxslt1-dev libxslt-dev
apt install -y python-dev python-setuptools python3-dev python3-setuptools
apt install -y libtiff4-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.5-dev tk8.5-dev python-tk
apt install -y graphicsmagick libgraphicsmagick++1-dev libboost-python-dev libmagic1
apt install -y python-imaging
apt install -y python-pip
pip install virtualenv
# Important security setup
# More: http://feross.org/how-to-setup-your-linode/
apt install -y fail2ban
# auto load iptables
apt install -y iptables-persistent
# Optionals
apt install -y memcached libmemcached-dev gettext silversearcher-ag git zsh
apt install -y lsof # https://www.thegeekstuff.com/2012/08/lsof-command-examples
# netstat and monitor tools
apt install -y nethogs iftop
# manual installation section
# zsh: so many handful utilities \o/
apt-get install git zsh
curl -L http://install.ohmyz.sh | sh
chsh -s /bin/zsh
zsh
# install yarn for node.js apps
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn
# Java for Debian 9 Stretch
# https://linoxide.com/debian/install-java-8-debian-gnulinux-9-stretch/
# https://tecadmin.net/install-oracle-java-11-on-debian-9-stretch/
# Scala
wget https://downloads.lightbend.com/scala/2.12.2/scala-2.12.2.deb
dpkg -i scala-2.12.2.deb
# sbt
echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
apt-get update
apt-get install sbt
# MongoDB
# https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/
# Resilio Sync
echo "deb http://linux-packages.resilio.com/resilio-sync/deb resilio-sync non-free" | tee /etc/apt/source
wget https://linux-packages.resilio.com/resilio-sync/key.asc
apt-key add key.asc
apt update
apt install resilio-sync
# PHP7
sudo apt install apache2
sudo apt install apt-transport-https lsb-release ca-certificates
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
sudo apt update
sudo apt install php7.2 php7.2-common
sudo apt install libapache2-mod-php7.2
sudo apt install php7.2-pgsql php7.2-mbstring php7.2-json php7.2-gd php7.2-ldap php7.2-zip
# may fix for some packages
ln -s /usr/include/freetype2 /usr/include/freetype
# Monitor tools
# init script
apt-get install -y monit
apt-get remove -y monit
# custom build
wget https://mmonit.com/monit/dist/monit-5.25.2.tar.gz
tar xzf monit-5.25.2.tar.gz
./configure
make
sudo make install
which monit # get path
vim /etc/init.d/monit # change constant DAEMON
# Mysql
sudo apt-get update
sudo apt-get install mysql-server
mysql_secure_installation
# custom build NGINX
# https://www.vultr.com/docs/how-to-compile-nginx-from-source-on-ubuntu-16-04
cd
if [ ! -d /var/lib/postgresql ]; then
# PG10
# https://computingforgeeks.com/install-postgresql-12-on-debian/
echo "deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main" | tee /etc/apt/sources.list.d/postgres.list
wget http://www.postgresql.org/media/keys/ACCC4CF8.asc
apt-key add ACCC4CF8.asc
apt-get update
apt-get install -y postgresql-10
apt-get install -y libpq-dev
# fix some errors
apt-get install -y postgresql-server-dev-all
apt-get install -y postgresql-common
fi
if [ ! -d /etc/redis ]; then
# Redis
wget http://download.redis.io/releases/redis-5.0.7.tar.gz
tar xzf redis-5.0.7.tar.gz
cd redis-5.0.7
make
make test
make install
cd ..
./redis-5.0.7/utils/install_server.sh
fi
if [ ! -f /usr/bin/nodejs ]; then
# Node v12 LTS
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
apt-get install -y nodejs
fi
# Install java8 in 2020:
# https://adoptopenjdk.net/installation.html?variant=openjdk8&jvmVariant=hotspot#x64_linux-jdk
if [ ! -d /etc/elasticsearch ]; then
# Elastic Search
#wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/deb/elasticsearch/2.4.6/elasticsearch-2.4.6.deb
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.16.deb
dpkg -i elasticsearch-5.6.16.deb
update-rc.d elasticsearch defaults
fi
if [ ! -d /etc/rabbitmq ]; then
# Erlang and RabbitMQ
wget https://packages.erlang-solutions.com/erlang-solutions_2.0_all.deb
dpkg -i erlang-solutions_2.0_all.deb
apt update
apt install -y erlang
echo "deb https://packagecloud.io/rabbitmq/rabbitmq-server/debian/ buster main" | sudo tee /etc/apt/sources.list.d/rabbitmq.list
echo "deb-src https://packagecloud.io/rabbitmq/rabbitmq-server/debian/ buster main" | sudo tee /etc/apt/sources.list.d/rabbitmq.list
curl -1sLf 'https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey' | sudo apt-key add -
apt update
apt install -y rabbitmq-server
# for DEBIAN 11: https://packagecloud.io/rabbitmq/rabbitmq-server
fi
if [ ! -d /opt/mongodb ]; then
# MongoDB
# New link https://www.mongodb.org/dl/linux/x86_64-debian92
curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-debian92-v4.2-latest.tgz
tar -zxvf mongodb-linux-x86_64-debian92-v4.2-latest.tgz
mkdir -p /opt/mongodb
cp -R -n mongodb-linux-x86_64-debian92-v4.2-latest/ /opt/mongodb
fi
if [ ! -d /opt/python-3.9.5 ]; then
# install python 3.9.x from source
wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tar.xz
tar xf Python-3.9.5.tar.xz
cd Python-3.9.5
./configure --prefix=/opt/python-3.9.5 --enable-shared --enable-optimizations
make
make install
cd ..
# sometimes you will need this
echo "/opt/python-3.9.5/lib" | tee /etc/ld.so.conf
/sbin/ldconfig
fi
if [ ! -d ./.oh-my-zsh ]; then
apt-get install -y git zsh
curl -L http://install.ohmyz.sh | sh
chsh -s /bin/zsh
zsh
fi
cd
echo "OK"
# create new sudo user
adduser admin
usermod -aG sudo admin
su - admin
# login with private key tutorial
# https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server
# create DB on Postgres 15
sudo -u postgres psql
CREATE DATABASE database_name;
CREATE ROLE myuser;
ALTER ROLE "myuser" WITH LOGIN;
GRANT ALL PRIVILEGES ON DATABASE database_name to myuser;
ALTER DATABASE database_name OWNER TO myuser;
ALTER USER "myuser" WITH PASSWORD 'new_password';
# optional
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO myuser;
ALTER USER myuser CREATEDB;
ALTER USER myuser WITH SUPERUSER;
ALTER USER myuser WITH NOSUPERUSER
# fix permissions errors
psql mydatabase -c "GRANT ALL ON ALL TABLES IN SCHEMA public to dbuser;"
psql mydatabase -c "GRANT ALL ON ALL SEQUENCES IN SCHEMA public to dbuser;"
psql mydatabase -c "GRANT ALL ON ALL FUNCTIONS IN SCHEMA public to dbuser;"
# show multi versions
grep -H '^port' /etc/postgresql/*/main/postgresql.conf
# check ports
sudo ss -tunelp | grep 5432
# backup and restore Postgres
pg_dump -U myuser -h localhost db_name > db.sql
psql -h localhost -U myuser db_name < db.sql
pg_dump -U myuser -h localhost db_name | ssh -i ~/keys/my_key_rsa [email protected] 'cd /home/admin/webapps/;cat > db.sql'
# create vhost for RabbitMQ
sudo rabbitmqctl add_user username password
sudo rabbitmqctl add_vhost my_vhost
sudo rabbitmqctl set_permissions -p my_vhost username ".*" ".*" ".*"
# rabbitmqctl set_user_tags test administrator
# drop role psql
REVOKE CONNECT ON DATABASE mydb FROM myuser;
REVOKE ALL PRIVILEGES ON DATABASE "mydb" from myuser;
DROP ROLE myuser;
# init, backup and restore MySQL
mysql -u mysql_user -p
CREATE DATABASE db_name CHARACTER SET utf8 COLLATE utf8_general_ci;
show databases;
create user db_user;
grant all on db_name.* to 'db_user'@'localhost' identified by 'db_password';
mysqldump -u db_user -p db_name > db_backup.sql
mysql -u db_user -p db_name < db_backup.sql
# create database and user in mysql 8
CREATE DATABASE sample_com_prod CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
CREATE USER 'sample_com_user' IDENTIFIED WITH mysql_native_password BY 'xxx';
grant all on sample_com_prod.* to sample_com_user;
# fix Python 3.7 error while loading shared libraries
# https://stackoverflow.com/questions/43333207/python-error-while-loading-shared-libraries-libpython3-4m-so-1-0-cannot-open
@bobvo23
Copy link

bobvo23 commented Mar 20, 2016

sweet 👍

@HungHuynh
Copy link

💃

@dungtran18
Copy link

Ngon á! :))

@kenjiakito
Copy link

python 3.6.6:
If you get the following error message
zipimport.ZipImportError: can't decompress data; zlib not available
=> Resolve: install the ‘zlib1g-dev’ package
apt-get install zlib1g-dev
are run ‘make’ and ‘make install’ again.

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