5
Deployment
Denis Nuțiu edited this page 2018-10-09 20:34:36 +03:00
Table of Contents
Installing Stuff
You will need to install the following stuff:
sudo apt-get update
sudo apt-get install python3-pip python3-dev nginx mysql-server libmemcached-dev zlib1g-dev
Database Setup
We assume that you've installed mysql-server
and ran mysql_secure_installation
.
From here on you'll need to create the database and a database user.
- Login to the mysql console
mysql -u root -p
and enter your password. - Create the database:
create database score;
- Create a new user and grant all privileges.
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
FLUSH PRIVILEGES;
Warning: It's not secure to grant all privileges to a single user on all databases!
4. Exit the mysql shell by typing exit
.
Virtual Environment
- Install virtualenv by typing
sudo pip3 install virtualenv
- Create and activate virtualenv
virtualenv bsflask
source bsflask/bin/activate
- Install the requirements.
pip3 install -r requirements.txt
Python Path
You will need to update PYTHONPATH with the directory of the app.
Either run this or update your rc file:
cd benchmark_scoreboard
export PYTHONPATH=$PYTHONPATH:$(pwd)
If it's not working you may add the export to bsflask/bin/activate
You may rename config_lock.py
to config.py
and edit it according to your needs.
Systemd
- Create a new systemd unit.
sudo nano /etc/systemd/system/benchmark_scoreboard.service
- Add the following: WARNING: UPDATE THE PATH AND THE USERNAME
[Unit]
Description=Gunicorn instance to serve benchmark-scoreboard
After=network.target
[Service]
User=denis
Group=www-data
WorkingDirectory=/home/denis/benchmark-scoreboard
Environment="BSFLASK_ENV=production"
Environment="BSFLASK_MYSQL_USERNAME=change"
Environment="BSFLASK_MYSQL_PASSWORD=change"
Environment="BSFLASK_MYSQL_HOSTNAME=localhost"
Environment="BSFLASK_MYSQL_DATABASE=score"
Environment="PATH=/home/denis/benchmark-scoreboard/bsflask/bin"
Environment="PYTHONPATH=$PYTHONPATH:/home/denis/benchmark-scoreboard/"
ExecStart=/home/denis/benchmark-scoreboard/bsflask/bin/gunicorn -c python:src.gconfig src.application:app
[Install]
WantedBy=multi-user.target
- Start and enable the systemd service.
sudo systemctl start benchmark_scoreboard
sudo systemctl enable benchmark_scoreboard
Nginx
Nginx will be used as a proxy to Gunicorn.
- Create a server block configuration.
sudo nano /etc/nginx/sites-available/benchmark_scoreboard
- Update it with your server domain or ip:
server {
listen 80;
server_name server_domain_or_IP;
location / {
include proxy_params;
proxy_pass http://unix:/home/denis/benchmark-scoreboard/benchmark_scoreboard.sock;
}
}
- Enable the configuration
sudo ln -s /etc/nginx/sites-available/benchmark_scoreboard /etc/nginx/sites-enabled
-
Test if everyting is okay:
sudo nginx -t
-
Remove the default page and restart the server
sudo rm /etc/nginx/sites-enabled/default
sudo systemctl restart nginx
Ufw rules
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow tcp
sudo ufw allow 'Nginx Full'