Switch to nginx + postgres in vagrant

This commit is contained in:
Martin Pépin 2017-08-09 22:07:49 +01:00
parent cb1d253517
commit ad15c45237
9 changed files with 107 additions and 105 deletions

View file

@ -136,7 +136,7 @@ TEMPLATES = [
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': DBNAME,
'USER': DBUSER,
'PASSWORD': DBPASSWD,

View file

@ -19,9 +19,9 @@ DEBUG = True
# ---
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/static/'
STATIC_ROOT = '/srv/gestiocof/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_ROOT = '/srv/gestiocof/media/'
MEDIA_URL = '/media/'

View file

@ -1,39 +0,0 @@
<VirtualHost *:80>
ServerName default
DocumentRoot /var/www/html
ProxyPreserveHost On
ProxyRequests Off
ProxyPass /static/ !
ProxyPass /media/ !
# Pour utiliser un sous-dossier (typiquement /gestion/), il faut faire a la
# place des lignes suivantes:
#
# RequestHeader set Daphne-Root-Path /gestion
# ProxyPass /gestion/ws/ ws://127.0.0.1:8001/ws/
# ProxyPass /gestion http://127.0.0.1:8001/gestion
# ProxyPassReverse /gestion http://127.0.0.1:8001/gestion
#
# Penser egalement a changer les /static/ et /media/ dans la config apache
# ainsi que dans les settings django.
ProxyPass /ws/ ws://127.0.0.1:8001/ws/
ProxyPass / http://127.0.0.1:8001/
ProxyPassReverse / http://127.0.0.1:8001/
Alias /media /vagrant/media
Alias /static /var/www/static
<Directory /vagrant/media>
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/static>
Order deny,allow
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

View file

@ -9,44 +9,48 @@ DBPASSWD="4KZt3nGPLVeWSvtBZPSM3fSzXpzEU4"
# Installation de paquets utiles
apt-get update && apt-get upgrade -y
apt-get install -y python3-pip python3-dev python3-venv libmysqlclient-dev \
libjpeg-dev git redis-server
apt-get install -y python3-pip python3-dev python3-venv libpq-dev postgresql \
postgresql-contrib libjpeg-dev nginx git redis-server
# Configuration et installation de mysql. Le mot de passe root est le même que
# le mot de passe pour l'utilisateur local - pour rappel, ceci est une instance
# locale de développement.
echo "mysql-server mysql-server/root_password password $DBPASSWD" | debconf-set-selections
echo "mysql-server mysql-server/root_password_again password $DBPASSWD" | debconf-set-selections
# Postgresql
sudo -u postgres createdb $DBNAME
sudo -u postgres createuser -SdR $DBUSER
sudo -u postgres psql -c "ALTER USER $DBUSER WITH PASSWORD '$DBPASSWD';"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DBNAME TO $DBUSER;"
apt-get install -y mysql-server
mysql -uroot -p$DBPASSWD -e "CREATE DATABASE $DBNAME; GRANT ALL PRIVILEGES ON $DBNAME.* TO '$DBUSER'@'localhost' IDENTIFIED BY '$DBPASSWD'"
mysql -uroot -p$DBPASSWD -e "GRANT ALL PRIVILEGES ON test_$DBNAME.* TO '$DBUSER'@'localhost'"
mysql -uroot -p$DBPASSWD -e "ALTER DATABASE databasename CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
# Configuration de redis
# Redis
REDIS_PASSWD="dummy"
redis-cli CONFIG SET requirepass $REDIS_PASSWD
redis-cli -a $REDIS_PASSWD CONFIG REWRITE
# Installation et configuration d'Apache
apt-get install -y apache2
a2enmod proxy proxy_http proxy_wstunnel headers
cp /vagrant/provisioning/apache.conf /etc/apache2/sites-available/gestiocof.conf
a2ensite gestiocof
a2dissite 000-default
service apache2 restart
mkdir /var/www/static
chown -R ubuntu:www-data /var/www/static
# Contenu static
mkdir -p /srv/gestiocof/{media,static}
chown -R ubuntu:www-data /srv/gestiocof
# Nginx
ln -s -f /vagrant/provisioning/nginx.conf /etc/nginx/sites-enabled/gestiocof.conf
rm -f /etc/nginx/sites-enabled/default
systemctl reload nginx
# Environnement virtuel python
sudo -H -u ubuntu python3 -m venv ~ubuntu/venv
sudo -H -u ubuntu ~ubuntu/venv/bin/pip install -U pip
sudo -H -u ubuntu ~ubuntu/venv/bin/pip install -r /vagrant/requirements-devel.txt
# Daphne + runworker
cp /vagrant/provisioning/daphne.service /etc/systemd/system/daphne.service
cp /vagrant/provisioning/worker.service /etc/systemd/system/worker.service
systemctl enable daphne.service
systemctl enable worker.service
systemctl start daphne.service
systemctl start worker.service
# Mise en place du .bash_profile pour tout configurer lors du `vagrant ssh`
cat >> ~ubuntu/.bashrc <<EOF
# On utilise la version de développement de GestioCOF
export DJANGO_SETTINGS_MODULE='cof.settings.dev'
# Permet d'utiliser les utilitaires pythons locaux
export PATH="\$PATH:\$HOME/.local/bin"
# Charge le virtualenv
source ~/venv/bin/activate
@ -54,26 +58,11 @@ source ~/venv/bin/activate
cd /vagrant
EOF
# On va dans /vagrant où se trouve gestioCOF
cd /vagrant
# Installation du virtualenv, on utilise désormais python3
sudo -H -u ubuntu python3 -m venv ~ubuntu/venv
sudo -H -u ubuntu ~ubuntu/venv/bin/pip install -U pip
sudo -H -u ubuntu ~ubuntu/venv/bin/pip install -r requirements.txt -r requirements-devel.txt
# Préparation de Django
if [ ! -f cof/settings/secret.py ]; then sudo -H -u ubuntu ln -s secret_example.py cof/settings/secret.py; fi
sudo -H -u ubuntu DJANGO_SETTINGS_MODULE='cof.settings.dev' DBUSER=$DBUSER DBNAME=$DBNAME DBPASSWD=$DBPASSWD bash provisioning/prepare_django.sh
ln -s -f cof/settings/secret_example.py cof/settings/secret.py
sudo -H -u ubuntu DJANGO_SETTINGS_MODULE='cof.settings.dev' bash provisioning/prepare_django.sh
# Installation du cron pour les mails de rappels
sudo -H -u ubuntu crontab provisioning/cron.dev
# On installe Daphne et on demande à supervisor de le lancer
sudo -H -u ubuntu ~ubuntu/venv/bin/pip install daphne
apt-get install -y supervisor
cp /vagrant/provisioning/supervisor.conf /etc/supervisor/conf.d/gestiocof.conf
sed "s/{DBUSER}/$DBUSER/" -i /etc/supervisor/conf.d/gestiocof.conf
sed "s/{DBNAME}/$DBNAME/" -i /etc/supervisor/conf.d/gestiocof.conf
sed "s/{DBPASSWD}/$DBPASSWD/" -i /etc/supervisor/conf.d/gestiocof.conf
service supervisor restart

View file

@ -0,0 +1,16 @@
Description="GestioCOF"
After=syslog.target
After=network.target
[Service]
Type=simple
User=ubuntu
Group=ubuntu
TimeoutSec=300
WorkingDirectory=/vagrant
Environment="DJANGO_SETTINGS_MODULE=cof.settings.dev"
ExecStart=/home/ubuntu/venv/bin/daphne -u /srv/gestiocof/gestiocof.sock \
cof.asgi:channel_layer
[Install]
WantedBy=multi-user.target

40
provisioning/nginx.conf Normal file
View file

@ -0,0 +1,40 @@
upstream gestiocof {
# Daphne listens on a unix socket
server unix:/srv/gestiocof/gestiocof.sock;
}
server {
listen 80;
server_name localhost;
# All the trafic is routed to Daphne
location ~ ^/ {
# A copy-paste of what we have in production
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-SSL-Client-Serial $ssl_client_serial;
proxy_set_header X-SSL-Client-Verify $ssl_client_verify;
proxy_set_header X-SSL-Client-S-DN $ssl_client_s_dn;
# Reverse-proxy
proxy_pass http://gestiocof;
}
# Static files
location ~ ^/static/ {
root /srv/gestiocof/static/;
access_log off;
add_header Cache-Control "public";
expires 7d;
}
# Uploaded media
location ~ ^/media/ {
root /srv/gestiocof/static/;
access_log off;
add_header Cache-Control "public";
expires 7d;
}
}

View file

@ -1,20 +0,0 @@
[program:worker]
command=/home/ubuntu/venv/bin/python /vagrant/manage.py runworker
directory=/vagrant/
user=ubuntu
environment=DBUSER={DBUSER},DBNAME={DBNAME},DBPASSWD={DBPASSWD},DJANGO_SETTINGS_MODULE="cof.settings.dev"
autostart=true
autorestart=true
redirect_stderr=true
stopasgroup=true
redirect_stderr=true
[program:interface]
command=/home/ubuntu/venv/bin/daphne -b 127.0.0.1 -p 8001 cof.asgi:channel_layer
environment=DBUSER={DBUSER},DBNAME={DBNAME},DBPASSWD={DBPASSWD},DJANGO_SETTINGS_MODULE="cof.settings.dev"
directory=/vagrant/
redirect_stderr=true
autostart=true
autorestart=true
stopasgroup=true
user=ubuntu

View file

@ -0,0 +1,16 @@
[Unit]
Description="GestioCOF"
After=syslog.target
After=network.target
[Service]
Type=simple
User=ubuntu
Group=ubuntu
TimeoutSec=300
WorkingDirectory=/vagrant
Environment="DJANGO_SETTINGS_MODULE=cof.settings.dev"
ExecStart=/home/ubuntu/venv/bin/python manage.py runworker
[Install]
WantedBy=multi-user.target

View file

@ -7,7 +7,7 @@ django-djconfig==0.5.3
django-grappelli==2.8.1
django-recaptcha==1.0.5
django-redis-cache==1.7.1
mysqlclient==1.3.7
psycopg2
Pillow==3.3.0
six==1.10.0
unicodecsv==0.14.1