Skip to content

Instantly share code, notes, and snippets.

@Nezteb
Last active February 25, 2016 05:33
Show Gist options
  • Save Nezteb/7678df448a07f3a7b433 to your computer and use it in GitHub Desktop.
Save Nezteb/7678df448a07f3a7b433 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Noah Betzen
# CentOS 7.1 Minimal Image
# DjangoCMS 3.2.0 Install Steps
# CONFIRMED WORKING AS OF 2-24-16
# Server address for nginx
serverAddress=`/sbin/ifconfig -a | awk '/(cast)/ { print $2 }' | cut -d':' -f2 | head -1` # Fancy stuff to get IP only
# DjangoCMS variables you should set
djangocmsUsername=djangocms
djangocmsEmail=test@gmail.com
djangocmsPassword=`openssl rand -base64 16` # If you don't want a secure password, change this ;)
djangocmsTimeZone=America/New_York
djangocmsBaseDirName=djangocms
djangocmsSubDirName=my_djangocms
# MySQL variables you should set
mysqlUsername=mysql
mysqlPassword=`openssl rand -base64 16` # If you don't want a secure password, change this ;)
mysqlDatabaseName=djangocms
# Path variables you should set
serverFilesPath=/usr/share/nginx/html
#yum update -y # Uncomment this if you want updates (recommended on fresh server installs)
yum install -y epel-release
#yum upgrade -y # Uncomment this if you want updates (recommended on fresh server installs)
# Install requisite packages
yum install -y nginx
#yum install -y httpd
yum install -y mariadb mariadb-server mariadb-devel git curl python python-django python-pip mysql-python mysql-connector-python
systemctl enable nginx
systemctl start nginx
systemctl enable mariadb
systemctl start mariadb
# Configure MySQL
mysql --execute="CREATE DATABASE $mysqlDatabaseName;"
mysql --execute="CREATE USER '$mysqlUsername'@'localhost' IDENTIFIED BY '$mysqlPassword';"
mysql --execute="GRANT ALL PRIVILEGES ON $mysqlDatabaseName . * TO '$mysqlUsername'@'localhost';"
mysql --execute="FLUSH PRIVILEGES;"
# Restart neccesary services
systemctl restart mariadb
# All of this nonsense is for installing pillow later...
# Issue documented here: https://github.com/divio/django-cms-tutorial/issues/7#issuecomment-40634726
# Also here: https://github.com/divio/django-cms-tutorial/wiki
yum reinstall -y python-imaging
ln -s /usr/lib/`uname -i`-linux-gnu/libfreetype.so /usr/lib/
ln -s /usr/lib/`uname -i`-linux-gnu/libjpeg.so /usr/lib/
ln -s /usr/lib/`uname -i`-linux-gnu/libz.so /usr/lib/
yum install -y libjpeg-devel zlib-devel gcc python-devel
pip install --upgrade pip
pip install --upgrade virtualenv
# Remove default nginx site files
cd $serverFilesPath
rm -f ./*
virtualenv env_djangocms
source env_djangocms/bin/activate
pip install pillow gunicorn djangocms-installer
# The password may break the installer below if it's not URL encoded first
URLmysqlPassword=$(python -c "import urllib; print urllib.quote('''$mysqlPassword''', safe='')")
# Install djangocms
mkdir $djangocmsBaseDirName
cd $djangocmsBaseDirName
echo -e "mysql://$mysqlUsername:$URLmysqlPassword@localhost:3306/$mysqlDatabaseName\n `# Database configuration (in URL format) [default sqlite://localhost/project.db]` \
\n `# django CMS version (choices: 2.4, 3.0, 3.1, 3.2, stable, develop) [default stable]` \
\n `# Django version (choices: 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, stable) [default stable]` \
\n `# Activate Django I18N / L10N setting (choices: yes, no) [default yes]` \
\n `# Install and configure reversion support (choices: yes, no) [default yes]` \
en\n `# Languages to enable. Option can be provided multiple times, or as a comma separated list. Only language codes supported by Django can be used here` \
$djangocmsTimeZone\n `# Optional default time zone [default America/New_York]` \
\n `# Activate Django timezone support (choices: yes, no) [default yes]` \
\n `# Activate CMS permission management (choices: yes, no) [default yes]` \
yes\n `# Use Twitter Bootstrap Theme (choices: yes, no) [default no]` \
\n `# Use custom template set [default no]` \
yes\n `# Load a starting page with examples after installation (english language only). Choose "no" if you use a custom template set. (choices: yes, no) [default no]`" | djangocms -p . my_djangocms
# Create django superuser
echo "from django.contrib.auth.models import User; User.objects.create_superuser('$djangocmsUsername', '$djangocmsEmail', '$djangocmsPassword')" | ./manage.py shell
# Collect static files
echo -e "yes\n" | python manage.py collectstatic
# Create gunicorn config
cd $serverFilesPath/env_djangocms
touch gunicorn_config.py
cat << EOF > gunicorn_config.py
command = '$serverFilesPath/env_djangocms/bin/gunicorn'
pythonpath = '$serverFilesPath/$djangocmsBaseDirName/$djangocmsSubDirName'
bind = '127.0.0.1:8001'
workers = 3
user = 'nobody'
EOF
# Fix wsgi.py so that it sets path correctly
cd $serverFilesPath/$djangocmsBaseDirName/$djangocmsSubDirName
sed -i "12iimport sys" wsgi.py
sed -i "13ibase = os.path.dirname(os.path.dirname(__file__))" wsgi.py
sed -i "14ibase_parent = os.path.dirname(base)" wsgi.py
sed -i "15isys.path.append(base)" wsgi.py
sed -i "16isys.path.append(base_parent)" wsgi.py
# Run gunicorn on the config
cd $serverFilesPath/$djangocmsBaseDirName/$djangocmsSubDirName
nohup gunicorn -c $serverFilesPath/env_djangocms/gunicorn_config.py wsgi:application &
# Create nginx config file
cd /etc/nginx/conf.d
touch djangocms.conf
cat << EOF > djangocms.conf
server {
listen 80;
server_name $serverAddress;
location /static/ {
alias /usr/share/nginx/html/djangocms/static/;
}
location / {
proxy_pass_header Server;
proxy_set_header Host \$http_host;
proxy_redirect off;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Scheme \$scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_pass http://127.0.01:8001/;
}
}
EOF
# Set permissions correctly
cd $serverFilesPath
chown -R nginx:nginx .
chmod -R 775 .
# Restart neccesary services
systemctl restart nginx
# This is only if you want to run the server without apache/nginx
#nohup python manage.py runserver 0.0.0.0:8000 &
# Echo credentials
echo "!!! MAKE SURE YOU SAVE THESE !!!"
echo "Your credentials are:"
echo "User: $djangocmsUsername"
echo "Password: $djangocmsPassword"
echo "User: $mysqlUsername"
echo "Password: $mysqlPassword (URL encoded: $URLmysqlPassword)"
echo "!!! MAKE SURE YOU SAVE THESE !!!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment