-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApacheWebServerSetup
74 lines (65 loc) · 2.92 KB
/
ApacheWebServerSetup
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
1. Step One— Install and Enable mod_wsgi
-> install mod_wsgi: sudo apt-get install libapache2-mod-wsgi-py3
-> enable mod_wsgi: sudo a2enmod wsgi
2. Step Two - Create the App
-> web application
-> api application
-> database setup (chmod write permission for CRUD functionality)
3. Step Three
- Install Flask
-> install/update pip: sudo apt-get install python-pip
-> install virtualenv: sudo pip install virtualenv
-> activating the virtual environment: source venv/bin/activate
-> install flask: sudo pip3 install flask
-> run __init__.py : sudo python3 __init__.py
4. Step Four
– Configure and Enable a New Virtual Host
-> sudo nano /etc/apache2/sites-available/APIApp.conf
-> configure as followed:
<VirtualHost *:80>
ServerName server.yangyaochia.com
ServerAdmin root@server.yangyaochia.com
WSGIScriptAlias / /var/www/APIApp/apiapp.wsgi
<Directory /var/www/APIApp/APIApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/APIApp/APIApp/static
<Directory /var/www/APIApp/APIApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
-> enable virtual host: sudo a2ensite APIApp
- What if you want to configure multiple virtual hosts on the same machine?
-> you could as well configure multiple virtual host with different host names by the same way as described as above.
5. Step Five – Create the .wsgi File
-> cd /var/www/APIApp
-> sudo nano apiapp.wsgi
-> add following lines
#!/usr/bin/python
activate_this = '/var/www/APIApp/APIApp/venv/bin/activate_this.py'
// the above path should be absolute path
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/APIApp/")
from FlaskApp import app as application
application.secret_key = 'super_key'
6. Step Six - Restart Apache2
-> sudo service apache2 restart
-> and you will be able to access your virtual host without any further issues.
-> use cURL/POSTMAN/browser to test the CRUD functionality
-> if there are issues getting the result, try the command below to see the root cause
sudo tail /var/log/apache2/error.log
7. Step Seven - Verification
- need to verify whether your mod_wsgi indeed use the correct venv python
- execute "pip list" on terminal
to List installed packages under normal environment and virtual environment
- if finding out flask package in normal environment, you should uninstall with "pip uninstall flask"
- mod_wsgi should be working only with venv python now