Skip to content

Instantly share code, notes, and snippets.

@ssbb ssbb/0_README.md
Last active Aug 29, 2015

Embed
What would you like to do?
Automatically create Django environment on server.

Usage

./start_env.sh example.com

#!/bin/bash
BTICK='`'
USER_NAME=$1
USER_PASSWORD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1)
MYSQL_PASSWORD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1)
echo "Creating user..."
useradd $USER_NAME -m -s /bin/bash
echo $USER_NAME:$USER_PASSWORD | chpasswd
echo "User created."
echo
echo "Creating MySQL user and database..."
Q1="CREATE DATABASE IF NOT EXISTS ${BTICK}$USER_NAME${BTICK} DEFAULT CHARACTER SET = 'utf8' DEFAULT COLLATE 'utf8_general_ci';"
Q2="GRANT ALL ON ${BTICK}$USER_NAME${BTICK}.* TO '$USER_NAME'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}"
mysql -uroot -pmCZ2QsGWBvgtz7vnZQxK -e "$SQL"
echo "MySQL user and database created."
echo
echo "Bootstrap user home directory..."
mkdir /home/$USER_NAME/{project,logs,conf}
NGINX_CONF=$(cat nginx.conf)
SUPERVISOR_CONF=$(cat supervisor.conf)
RUN_CONF=$(cat run.sh)
NGINX_CONF="${NGINX_CONF//PROJECT_NAME/$USER_NAME}"
SUPERVISOR_CONF="${SUPERVISOR_CONF//PROJECT_NAME/$USER_NAME}"
RUN_CONF="${RUN_CONF//PROJECT_NAME/$USER_NAME}"
echo "$NGINX_CONF" > /home/$USER_NAME/conf/nginx.conf
echo "$SUPERVISOR_CONF" > /home/$USER_NAME/conf/supervisor.conf
echo "$RUN_CONF" > /home/$USER_NAME/conf/run.sh
chmod +x /home/$USER_NAME/conf/run.sh
ln -s /home/$USER_NAME/conf/nginx.conf /etc/nginx/sites-enabled/$USER_NAME
ln -s /home/$USER_NAME/conf/supervisor.conf /etc/supervisor/conf.d/$USER_NAME.conf
service nginx reload
service nginx restart
supervisorctl reload
echo "Set right permissions..."
SUDO_COMMAND="$USER_NAME ALL = (root) NOPASSWD:/usr/bin/supervisorctl restart $USER_NAME"
echo $SUDO_COMMAND >> /etc/sudoers
virtualenv /home/$USER_NAME/.env
echo "source /home/$USER_NAME/.env/bin/activate" >> /home/$USER_NAME/.bashrc
chown -R $USER_NAME:$USER_NAME /home/$USER_NAME/
echo
echo "---------------------"
echo "User:"
echo "username: $1"
echo "password: $USER_PASSWORD"
echo
echo "MySQL:"
echo "username: $1"
echo "database: $1"
echo "password: $MYSQL_PASSWORD"
server {
listen 80;
server_name www.PROJECT_NAME;
return 301 http://PROJECT_NAME$request_uri;
}
server {
listen 80;
server_name PROJECT_NAME;
location / {
client_max_body_size 20m;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unix:/tmp/PROJECT_NAME.sock;
}
location /media {
alias /home/PROJECT_NAME/project/media;
}
location /static {
alias /home/PROJECT_NAME/project/static;
}
access_log /home/PROJECT_NAME/logs/ng_access.log;
error_log /home/PROJECT_NAME/logs/ng_error.log;
}
[program:PROJECT_NAME]
command=/home/PROJECT_NAME/conf/run.sh
directory=/home/PROJECT_NAME/project
user=PROJECT_NAME
autostart=true
autorestart=true
redirect_stderr=True
stderr_logfile=/home/PROJECT_NAME/logs/sv_errors.log
stdout_logfile=/home/PROJECT_NAME/logs/sv_access.log
#!/bin/sh
export LANG=ru_RU.UTF-8
export LC_ALL=ru_RU.UTF-8
NAME=PROJECT_NAME
HOMEDIR=/home/${NAME}
PROJECTDIR=${HOMEDIR}/project
VIRTUALENV=${HOMEDIR}/.env
GUNICORN=${VIRTUALENV}/bin/gunicorn_django
SOCKFILE=/tmp/${NAME}.sock
MANAGE=${PROJECTDIR}/manage.py
. ${VIRTUALENV}/bin/activate
exec ${MANAGE} run_gunicorn --bind unix:${SOCKFILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.