Skip to content

Instantly share code, notes, and snippets.

@puilp0502
Last active January 15, 2018 07:42
Show Gist options
  • Save puilp0502/e1cd2b4723b5954e8eaa43a2b1e053b3 to your computer and use it in GitHub Desktop.
Save puilp0502/e1cd2b4723b5954e8eaa43a2b1e053b3 to your computer and use it in GitHub Desktop.
Install uWSGI and nginx
#!/bin/bash
# uWSGI & nginx installation script
# Created at 2017-06-27 by Frank Yang (https://github.com/puilp0502)
# Tested on Ubuntu Server 16.04 LTS
# Check if user is root
if [[ $UID -ne '0' ]]; then
echo "This script needs to be run as root; exiting..."
exit 0
fi
# Install python and devtools (required to install uwsgi)
apt-get update --qq
echo ""
echo "##################################################"
echo "# Installing python & devtools #"
echo "##################################################"
echo ""
apt-get install -qq -y python3 python3-dev gcc
# Install pip the pythonic way
echo ""
echo "##################################################"
echo "# Installing pip #"
echo "##################################################"
echo ""
curl https://bootstrap.pypa.io/get-pip.py > get-pip.py
python3 get-pip.py -q
rm get-pip.py
# Install virtualenv
echo ""
echo "##################################################"
echo "# Installing virtualenv & virtualenvwrapper #"
echo "##################################################"
echo ""
pip install -q virtualenv virtualenvwrapper
cat <<EOF >> ~/.bashrc
# Virtualenvwrapper config
export WORKON_HOME=$HOME/.venv
export VIRTUALENVWRAPPER_PYTHON=`which python3`
export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh
source /usr/local/bin/virtualenvwrapper.sh
EOF
# Install uwsgi
echo ""
echo "##################################################"
echo "# Installing uWSGI #"
echo "##################################################"
echo ""
pip install -q uwsgi
# Configure uWSGI systemd daemon
cat <<EOF > /etc/systemd/system/uwsgi.service
[Unit]
Description=uWSGI Emperor service
[Service]
RuntimeDirectory=uwsgi
RuntimeDirectoryMode=755
User=emperor
Group=www-data
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
EOF
systemctl enable uwsgi
# Install nginx
echo ""
echo "##################################################"
echo "# Installing nginx #"
echo "##################################################"
echo ""
apt-get install -y -qq nginx
adduser -q --gecos "" --shell /bin/false --no-create-home --ingroup www-data --disabled-password --disabled-login emperor
mkdir -p /etc/uwsgi/sites
chown -R emperor:www-data /etc/uwsgi
# Start service
echo ""
echo "##################################################"
echo "# Starting service #"
echo "##################################################"
echo ""
echo -n "Starting nginx..."
systemctl start nginx
if [[ $? == '0' ]]; then
echo ' [OK]'
else
echo ' [FAILED]'
fi
echo -n "Starting uWSGI..."
systemctl start uwsgi
if [[ $? == '0' ]]; then
echo ' [OK]'
else
echo ' [FAILED]'
fi
if [[ $1 == '--shutup' ]]; then
exit 0;
else
echo ""
echo "##################################################"
echo "# Installation Completed #"
echo "##################################################"
echo ""
echo "Installation has been completed!"
echo "source ~/.bashrc to enable virtualenvwrapper."
echo ""
sleep 1
echo "Now you need to actually configure your site."
echo "https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-ubuntu-16-04#install-and-configure-nginx-as-a-reverse-proxy"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment