-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdjango-staging-install.sh
141 lines (94 loc) · 4.73 KB
/
django-staging-install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
#django/apache production install script
#isntall pip, apache, and mod-wsgi
sudo yum -y install python-pip httpd mod_wsgi
echo "Starting HTTP service..."
sudo systemctl enable httpd.service
echo "Starting Apache Server..."
sudo systemctl start httpd.service
echo "Cloning grant's github..."
sudo yum -y install git
git clone https://github.com/grantypantyyy/NTI-310_GG.git
#isntall current epel release
sudo yum -y install epel-release
echo "Beginning Django Web Framework install..."
echo "Current version of Python:"
python --version
echo "Installing virtualenv to give Django it's own version of Python..."
sudo yum -y install python-pip
sudo pip install virtualenv
cd /opt
# we're going to install our django libs in /opt, often used for optional or add-on
# we want to make this env accessible to the GCloud user because we don't want to have to run it as root
sudo mkdir django
sudo chown -R ggrism01 django
sleep 5
cd django
sudo virtualenv django-env
echo "Activating virtualenv..."
source /opt/django/django-env/bin/activate
echo "To switch out of virtualenv, type deactivate."
echo "Now using this version of Python:"
which python
sudo chown -R ggrism01 /opt/django
echo "Installing Django"
pip install Django
echo "Django admin is version:"
django-admin --version
django-admin startproject project1
echo "Adjusting settings.py allowed_hosts..."
sed -i "s,ALLOWED_HOSTS = \[\],ALLOWED_HOSTS = \['*'\],g" /opt/django/project1/project1/settings.py
echo "This is the new django project directory..."
echo 'STATIC_ROOT = os.path.join(BASE_DIR, "static/")' >> /opt/django/project1/project1/settings.py
sudo yum -y install tree
tree project1
#generate password for Superuser #<----- ***permission denied***
#sudo RPASS=$(mkpasswd -l 12)
#sudo echo -n "$RPASS" > /root/django_admin_pass
#sudo chmod 600 /root/django_admin_pass
#make initial migrations using sqllite
cd /opt/django/project1
/opt/django/django-env/bin/python manage.py makemigrations
/opt/django/django-env/bin/python manage.py migrate
echo yes | /opt/django/django-env/bin/python manage.py collectstatic
deactivate
#tell django to use apache and mod_wsgi, adjust permissions
sudo cp /NTI-310-GG/httpd.conf /etc/httpd/conf/httpd.conf
sudo usermod -a -G ggrism01 apache
sudo setenforce 0
sudo systemctl restart httpd
echo "Django is now accessible from the web at [server IP] and admin site and [server IP]/admin"
#configure django database settings
ip1=$(gcloud compute instances list | grep postgres-b-staging | awk '{print $4}')
sed -i "s/ 'ENGINE': 'django.db.backends.sqlite3',/ 'ENGINE': 'django.db.backends.postgresql_psycopg2',/g" /opt/django/project1/project1/settings.py
sed -i "s/ 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),/ 'NAME': 'project1',/g" /opt/django/project1/project1/settings.py
sed -i "80i 'USER': 'project1'," /opt/django/project1/project1/settings.py
sed -i "81i 'PASSWORD': 'P@ssw0rd1'," /opt/django/project1/project1/settings.py
sed -i "82i 'HOST': 'NEEDTOADDIP'," /opt/django/project1/project1/settings.py
sed -i "83i 'PORT': '5432'," /opt/django/project1/project1/settings.py
sed -i "s/'USER': 'project1',/ 'USER': 'project1',/g" /opt/django/project1/project1/settings.py
sed -i "s/'PASSWORD': 'P@ssw0rd1',/ 'PASSWORD': 'P@ssw0rd1',/g" /opt/django/project1/project1/settings.py
sed -i "s/'HOST': 'NEEDTOADDIP',/ 'HOST': '$ip1',/g" /opt/django/project1/project1/settings.py
sed -i "s/'PORT': '5432',/ 'PORT': '5432',/g" /opt/django/project1/project1/settings.py
#prepare django for postgresql integration -- install postgres dev packages
source /opt/django/django-env/bin/activate
sudo yum -y install python-devel postgresql-devel
sudo yum -y install gcc
#install psycopg2 to allow us to use the project1 database on postgres server
pip install psycopg2
#migrate databasae <----***** postgres server must be setup and running to complete the following actoions *****
cd /opt/django/project1
python manage.py makemigrations #*******
python manage.py migrate
#create superuser for admin login
cd /opt/django/project1
#/opt/django/django-env/bin/python manage.py createsuperuser
#manage.py docs for automataing
echo "from django.contrib.auth.models import User; User.objects.create_superuser('grant', '[email protected]', 'P@ssw0rd1')" | python manage.py shell
#/opt/django/django-env/bin/python manage.py createsuperuser --username jwade --email [email protected] --password P@ssw0rd1 --noinput
deactivate
#allow django to connect to the db on httpd <--- solves issue with django being able to reconnect to the db
sudo setsebool -P httpd_can_network_connect_db on
#restart httpd service
sudo systemctl restart httpd
echo "Django is now installed and connected to postgres db and accessible from the web."