Skip to content

Commit 49f6fc8

Browse files
committed
Version 1.2
1 parent f1f9799 commit 49f6fc8

File tree

4 files changed

+145
-26
lines changed

4 files changed

+145
-26
lines changed

Dockerfile

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
# Base Apache2 Server for Running Django Apps
2-
FROM ubuntu:16.04
2+
FROM btr1975/apache-py3-django:1.1
33

44
LABEL maintainer="e_ben_75-python@yahoo.com" \
55
important-stopping-note="Stop the server by running apache2ctl stop, this will keep from process hang" \
66
description="This is a base Apache2, Python 3.5.2, Python3 mysqlclient, and mod_wsgi for py3 web server for Django." \
7-
image-version="1.1" \
7+
image-version="1.2" \
88
django-version="1.11.2" \
99
django-localflavor-version="1.5.1" \
1010
mysqlclient-version="1.3.10"
1111

12-
# Copies the apache conf python script
12+
# Copies the apache conf python script and shell script
1313

1414
COPY ./apache-site-conf.py /bin/
15+
COPY ./apache-site-conf.sh /bin/
1516

1617
# Arguments for Apache conf file builder script if not used, they will use default settings
1718
# SITE_SERVER_NAME = ServerName
@@ -24,18 +25,8 @@ ARG SITE_SERVER_ADMIN
2425

2526
WORKDIR /DjangoSites
2627

27-
RUN apt-get update && apt-get install -y apache2 \
28-
python3 \
29-
python3-pip \
30-
libmysqlclient-dev \
31-
libapache2-mod-wsgi-py3 \
32-
&& apt-get clean \
33-
&& chmod 755 /DjangoSites \
34-
&& chmod 755 /bin/apache-site-conf.py \
35-
&& pip3 install --upgrade pip \
36-
&& pip3 install Django==1.11.2 \
37-
&& pip3 install django-localflavor==1.5.1 \
38-
&& pip3 install mysqlclient==1.3.10
28+
RUN chmod 755 /bin/apache-site-conf.py \
29+
&& chmod 755 /bin/apache-site-conf.sh
3930

4031
EXPOSE 80 443
4132

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Created By: Benjamin P. Trachtenberg
44

55
### Contact Information: e_ben_75-python@yahoo.com
6+
### Send me an e-mail if you have any questions
67

78
### LinkedIn: [Ben Trachtenberg](https://www.linkedin.com/in/ben-trachtenberg-3a78496)
89
### Docker Hub: [Image](https://hub.docker.com/r/btr1975/apache-py3-django/)
@@ -27,11 +28,23 @@ This image is for Python Django Apps written in Python 3
2728
* Django-Localflavor: 1.5.1
2829
* mysqlclient: 1.3.10
2930

31+
### Image Versions in apache-py3-django:1.2
32+
33+
* Python: 3.5.2
34+
* Apache: 2.4.18 (Ubuntu) with mod_wsgi py3 version
35+
* Django: 1.11.2
36+
* Django-Localflavor: 1.5.1
37+
* mysqlclient: 1.3.10
38+
* apache-site-conf.py: 1.0.1.prod
39+
* apache-site-conf.sh: 1.0 prod
40+
3041
### Docker-Image Version Tags
3142
* latest
3243
* 1.0
3344
* 1.1
45+
* 1.2
3446

35-
#### -- Version News: apache-py2-django:1.1 --
36-
* Added a Python script that builds the apache conf file
37-
* Swapped out MySQL-Connector, for mysqlclient, to many issues with MySQL-Connector
47+
#### -- Version News: apache-py2-django:1.2 --
48+
* Updated Python script that builds the apache conf file
49+
* Added a bash script that builds the apache conf file
50+
* Both scripts do the same thing, so use whichever you want.

apache-site-conf.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,30 @@
66
__credits__ = None
77
__license__ = 'The MIT License (MIT)'
88
__status__ = 'prod'
9-
__version_info__ = (1, 0, 0, __status__)
9+
__version_info__ = (1, 0, 1, __status__)
1010
__version__ = '.'.join(map(str, __version_info__))
1111
__maintainer__ = 'Benjamin P. Trachtenberg'
1212
__email__ = 'e_ben_75-python@yahoo.com'
1313

1414

15-
def create_conf(folder_name):
15+
def create_conf(folder_name, dir_count):
1616
"""
1717
Function to create apache conf files for loaded sites
1818
Currently only non-SSL Sites
19-
:param folder_name: The folder name of the site
19+
:param
20+
folder_name: The folder name of the site
21+
dir_count: Count of dirs
2022
:return:
2123
"""
2224
temp_list = list()
2325
temp_list.append('<VirtualHost *:80>')
24-
if os.environ.get('SITE_SERVER_NAME'):
26+
if os.environ.get('SITE_SERVER_NAME') and dir_count == 1:
2527
temp_list.append(' ServerName %s' % (os.environ.get('SITE_SERVER_NAME'),))
2628

2729
else:
2830
temp_list.append(' # ServerName www.example.com')
2931

30-
if os.environ.get('SITE_SERVER_ADMIN'):
32+
if os.environ.get('SITE_SERVER_ADMIN') and dir_count == 1:
3133
temp_list.append(' ServerAdmin %s' % (os.environ.get('SITE_SERVER_ADMIN'),))
3234

3335
else:
@@ -99,7 +101,22 @@ def add_line_break(list_line):
99101

100102
site_to_enable = output_file(temp_list, folder_name)
101103

102-
sub.call('a2ensite %s' % (site_to_enable,), stderr=sub.STDOUT, shell=True)
104+
sub.call('a2ensite %s > /dev/null' % (site_to_enable,), stderr=sub.STDOUT, shell=True)
105+
106+
107+
def count_dir():
108+
"""
109+
Function to count how many dirs in a dir
110+
:return:
111+
An integer of how many dirs
112+
"""
113+
temp_list_count = list()
114+
115+
for item_name in os.listdir(os.getcwd()):
116+
if os.path.isdir(item_name):
117+
temp_list_count.append(item_name)
118+
119+
return len(temp_list_count)
103120

104121

105122
def main():
@@ -108,11 +125,13 @@ def main():
108125
:return: None
109126
110127
"""
128+
dir_count = count_dir()
129+
111130
for item_name in os.listdir(os.getcwd()):
112131
if os.path.isdir(item_name):
113-
create_conf(item_name)
132+
create_conf(item_name, dir_count)
114133

115-
sub.call('a2dissite 000-default.conf', stderr=sub.STDOUT, shell=True)
134+
sub.call('a2dissite 000-default.conf > /dev/null', stderr=sub.STDOUT, shell=True)
116135

117136

118137
main()

apache-site-conf.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/bin/bash
2+
3+
# This script is to build apache conf files for Django apps
4+
# Version: 1.0 Prod
5+
# Author: Benjamin P. Trachtenberg
6+
# Contact: e_ben_75-python@yahoo.com
7+
8+
declare -r apachedirname="/etc/apache2/sites-available"
9+
apacheconfname=""
10+
declare -r djangodirname="/DjangoSites"
11+
12+
# Check for DjangoSites Directory
13+
if [[ ! -d $djangodirname ]]; then
14+
echo "No DjangoSites Directory Found!"
15+
exit 1
16+
fi
17+
18+
# Check for Apache sites-available directory
19+
if [[ ! -d $apachedirname ]]; then
20+
echo "No Apache sites-available Directory Found!"
21+
exit 1
22+
fi
23+
24+
if [[ -f "/etc/apache2/sites-enabled/000-default.conf" ]]; then
25+
a2dissite 000-default.conf > /dev/null
26+
fi
27+
28+
for directory in $(ls $djangodirname); do
29+
if [[ -d $djangodirname/$directory ]]; then
30+
apacheconfname="${directory,,}.conf"
31+
if [[ ! -f "$apachedirname/$apacheconfname" ]]; then
32+
echo "Creating apache config $apachedirname/$apacheconfname"
33+
echo "<VirtualHost *:80>" >> $apachedirname/$apacheconfname
34+
35+
if [[ $(ls -d $djangodirname/*/ | wc -l) = 1 && $SITE_SERVER_NAME ]]; then
36+
echo " ServerName $SITE_SERVER_NAME" >> $apachedirname/$apacheconfname
37+
38+
else
39+
echo " # ServerName www.example.com" >> $apachedirname/$apacheconfname
40+
41+
fi
42+
43+
if [[ $(ls -d $djangodirname/*/ | wc -l) = 1 && $SITE_SERVER_ADMIN ]]; then
44+
echo " ServerAdmin $SITE_SERVER_ADMIN" >> $apachedirname/$apacheconfname
45+
46+
else
47+
echo " ServerAdmin webmaster@localhost" >> $apachedirname/$apacheconfname
48+
49+
fi
50+
51+
echo "" >> $apachedirname/$apacheconfname
52+
echo " Alias /media/ /DjangoSites/$directory/media/" >> $apachedirname/$apacheconfname
53+
echo "" >> $apachedirname/$apacheconfname
54+
echo " <Directory /DjangoSites/$directory/media>" >> $apachedirname/$apacheconfname
55+
echo " Order deny,allow" >> $apachedirname/$apacheconfname
56+
echo " Require all granted" >> $apachedirname/$apacheconfname
57+
echo " </Directory>" >> $apachedirname/$apacheconfname
58+
echo "" >> $apachedirname/$apacheconfname
59+
echo " Alias /static/ /DjangoSites/$directory/static/site/" >> $apachedirname/$apacheconfname
60+
echo "" >> $apachedirname/$apacheconfname
61+
echo " <Directory /DjangoSites/$directory/static/site>" >> $apachedirname/$apacheconfname
62+
echo " Order deny,allow" >> $apachedirname/$apacheconfname
63+
echo " Require all granted" >> $apachedirname/$apacheconfname
64+
echo " </Directory>" >> $apachedirname/$apacheconfname
65+
echo "" >> $apachedirname/$apacheconfname
66+
echo " WSGIScriptAlias / /DjangoSites/$directory/apache/django.wsgi process-group=$directory" >> $apachedirname/$apacheconfname
67+
echo " WSGIDaemonProcess $directory" >> $apachedirname/$apacheconfname
68+
echo " WSGIProcessGroup $directory" >> $apachedirname/$apacheconfname
69+
echo "" >> $apachedirname/$apacheconfname
70+
echo " <Directory /DjangoSites/$directory/apache>" >> $apachedirname/$apacheconfname
71+
echo " Order deny,allow" >> $apachedirname/$apacheconfname
72+
echo " Require all granted" >> $apachedirname/$apacheconfname
73+
echo " </Directory>" >> $apachedirname/$apacheconfname
74+
echo "" >> $apachedirname/$apacheconfname
75+
echo " # Available loglevels: trace8, ..., trace1, debug, info, notice, warn," >> $apachedirname/$apacheconfname
76+
echo " # error, crit, alert, emerg." >> $apachedirname/$apacheconfname
77+
echo " # It is also possible to configure the loglevel for particular" >> $apachedirname/$apacheconfname
78+
echo " # modules, e.g." >> $apachedirname/$apacheconfname
79+
echo " LogLevel info" >> $apachedirname/$apacheconfname
80+
echo "" >> $apachedirname/$apacheconfname
81+
echo " ErrorLog ${APACHE_LOG_DIR}/error.log" >> $apachedirname/$apacheconfname
82+
echo " CustomLog ${APACHE_LOG_DIR}/access.log combined" >> $apachedirname/$apacheconfname
83+
echo "" >> $apachedirname/$apacheconfname
84+
echo "</VirtualHost>" >> $apachedirname/$apacheconfname
85+
echo "" >> $apachedirname/$apacheconfname
86+
echo "# vim: syntax=apache ts=4 sw=4 sts=4 sr noet" >> $apachedirname/$apacheconfname
87+
88+
a2ensite $apacheconfname > /dev/null
89+
90+
else
91+
echo "Can not create apache config $apachedirname/$apacheconfname"
92+
echo "It already exists remove it and run the script again"
93+
94+
fi
95+
fi
96+
done

0 commit comments

Comments
 (0)