Skip to content

Commit dc14a45

Browse files
authored
Merge pull request #70 from RSE-Sheffield/chore/deploy-env
Add .env file to deployment script
2 parents 4e16fb6 + 40484da commit dc14a45

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

deploy.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ venv_dir="$sort_dir/venv"
1717
pip="$venv_dir/bin/pip"
1818
python_version="python3.12"
1919
python="$venv_dir/bin/python"
20+
env_file="$sort_dir/.env"
2021

2122
# Install British UTF-8 locale so we can use this with PostgreSQL.
2223
# This is important to avoid the limitations of the LATIN1 character set.
@@ -25,6 +26,7 @@ sudo locale-gen en_GB.UTF-8
2526
sudo update-locale
2627

2728
# Create Python virtual environment
29+
mkdir --parents "$sort_dir"
2830
apt update -qq
2931
apt install --upgrade --yes -qq "$python_version" "$python_version-venv"
3032
python3 -m venv "$venv_dir"
@@ -33,6 +35,11 @@ python3 -m venv "$venv_dir"
3335
$pip install --quiet -r requirements.txt
3436
cp --recursive * "$sort_dir/"
3537

38+
# Create environment file
39+
sudo touch "$env_file"
40+
sudo chown gunicorn:gunicorn "$env_file"
41+
sudo chmod 600 "$env_file"
42+
3643
# Install static files into DJANGO_STATIC_ROOT
3744
# This runs in a subshell because it's changing directory
3845
(cd "$sort_dir" && exec $python manage.py collectstatic --no-input)

docs/deployment.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ We can run commands and Bash scripts as the superuser (`root`) using the [`sudo`
5252

5353
To configure the environment variables for the service, you can either edit the `.env` file and/or add them to the systemd service using `systemctl edit`.
5454

55-
To edit the environment file:
55+
To create and edit the environment file, which should have access restricted to only the Python app (the Gunicorn service):
5656

5757
```bash
5858
sudo mkdir --parents /opt/sort
59+
sudo touch /opt/sort/.env
60+
sudo chown gunicorn:gunicorn /opt/sort/.env
61+
sudo chmod 600 /opt/sort/.env
5962
sudo nano /opt/sort/.env
6063
```
6164

0 commit comments

Comments
 (0)