Stuck on apache2 default page while deploying Django website - django

I have gone through a few answers on the web and I am still struggling to get past the apache2 default page. The webapp works fine locally and without apache. I am using an AWS EC2 instance for this on an ubuntu machine.
The whole code is hosted in /home/ubuntu/sm on the machine, the venv is in /home/ubuntu/sm/venv and running on the ip addr 3.X.X.212
I've tried adding, editing, removing a few variables, to no avail. I got nothing in the error logs either. The 'gracefully restarting' entries are starting to look narcissistic given how long I've been stuck on this.
apachectl -S produces the following response:
[Tue Nov 05 05:16:52.198045 2019] [alias:warn] [pid 13664:tid 139764718218176] AH00671: The Alias directive in /etc/apache2/sites-enabled/sm.conf at line 33 will probably never match because it overlaps an earlier Alias.
VirtualHost configuration:
*:80 ip-172-*-*-29.ap-southeast-2.compute.internal (/etc/apache2/sites-enabled/sm.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used
"sm.conf" is the config file used for this, as below:
ServerAdmin webmaster#localhost
LogLevel info
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /robots.txt /home/ubuntu/sm/website/static/robots.txt
Alias /favicon.ico /home/ubuntu/sm/website/static/favicon.ico
Alias /static/ /home/ubuntu/sm/website/static
Alias /static/ /home/ubuntu/sm/media
WSGIDaemonProcess website.net processes=2 threads=15 python-home=/home/ubuntu/sm/venv/lib/python3.6 python-path=/home/ubuntu/sm/
WSGIProcessGroup website.net
WSGIScriptAlias /sm /home/ubuntu/sm/sm/wsgi.py process-group=website.net
<Directory /home/ubuntu/sm/>
AllowOverride all
Require all granted
Options FollowSymlinks
</Directory>
<Directory /home/ubuntu/sm/sm>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /home/ubuntu/sm/website/static/>
Require all granted
</Directory>
<Directory /home/ubuntu/sm/media>
Require all granted
</Directory>
Comments, concerns, suggestions, critique of shoddy code, all appreciated.

Related

issues with serving 2 django applications with apache in ubuntu server using wsgi

i'm trying to host a second domain in my ubuntu vps (domain2), i read online that is possible to host 2 domains in one vps (with one ip adress) yet i'm having some issues with it.
i have this configuration in apache2 ubuntu under /etc/apache2/sites-enabled/000-default-le-ssl.conf:
Define wsgi_daemon1 "domain1"
Define wsgi_daemon2 "domain2"
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/my_linux_user/myproject/static
<Directory /home/my_linux_user/myproject/static>
Require all granted
</Directory>
<Directory /home/my_linux_user/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<IfDefine !wsgi_init>
WSGIDaemonProcess ${wsgi_daemon1} python-path=/home/my_linux_user/myproject python-home=/home/my_linux_user/myproject/myprojectenv
WSGIProcessGroup ${wsgi_daemon1}
WSGIScriptAlias / /home/my_linux_user/myproject/myproject/wsgi.py
Define wsgi_init 1
</IfDefine>
ServerName domain1.tn
SSLCertificateFile /etc/letsencrypt/live/domain1.tn/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain1.tn/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error_project2.log
CustomLog ${APACHE_LOG_DIR}/access_project2.log combined
Alias /static /home/my_linux_user/project2/static
<Directory /home/my_linux_user/project2/static>
Require all granted
</Directory>
<Directory /home/my_linux_user/project2/django_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<IfDefine !wsgi_init>
WSGIDaemonProcess ${wsgi_daemon2} python-path=/home/my_linux_user/project2 python-home=/home/my_linux_user/project2/project2ENV
WSGIProcessGroup ${wsgi_daemon2}
WSGIScriptAlias / /home/my_linux_user/project2/django_project/wsgi.py
Define wsgi_init 1
</IfDefine>
ServerName domain2
SSLCertificateFile /etc/letsencrypt/live/domain2/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain2/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
the first domain is served correctly but the second domain (domain2) is not, it actually still pointing to the default apache2 index page
i tried
restarting apache2 with:
apachectl restart
and with
apachectl graceful
checking the DNS configuration, the domaain in up and working
when i tried to just point the domain with a seperate vhost and a simple web page it works!, yet with wsgi it doesn't
i tried to see what are the enabled sites with
apache2ctl -S
and i something like this:
*:443 is a NameVirtualHost
default server domain1.tn (/etc/apache2/sites-enabled/000-default-le-ssl.conf:5)
port 443 namevhost domain1.tn (/etc/apache2/sites-enabled/000-default-le-ssl.conf:5)
port 443 namevhost domain2.com (/etc/apache2/sites-enabled/000-default-le-ssl.conf:106)
port 443 namevhost mail.domain1.tn
(/etc/apache2/sites-enabled/mail.domain1.tn.conf:3)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex proxy: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex mpm-accept: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
Define: MODSEC_2.5
Define: MODSEC_2.9
Define: wsgi_daemon1=domain1
Define: wsgi_daemon2=domain2
Define: wsgi_init=1
User: name="www-data" id=33
Group: name="www-data" id=33
so my question does it take some time for changes to take effect (it was not the case when i hosted the first domain (domain1)), if not i will appreciate any help :)
ps: i'm replacing the actual domain names

Forbidden 403 Apache error on an Indico installation

I installed Indico http://indico-software.org/ that is an open source tool for event organization, archival and collaboration on Ubuntu with this instructions https://indico.syntek.org/category/1/material/0/0.pdf, installation looks ok but when I try to open localhost/indico I get a forbidden 403 error.
I added this lines to the /etc/apache2/apach2.conf
WSGIScriptAlias / /opt/indico/htdocs/
WSGIPythonHome /usr/local/pythonenv/BASELINE
WSGIPythonPath
/usr/local/pythonenv/indico12/lib/python2.7/sitepackages
WSGIPythonEggs /opt/indico/tmp/egg-cache
<Directory /opt/indico/htdocs>
Options Indexes FollowSymLinks
AllowOverride None
<Files indico.wsgi>
Require all granted
</Files>
</Directory>
This is my virtulhost file on /etc/apache2/sites-available/a2indico.conf
<VirtualHost *:80>
ErrorLog /var/log/apache2/error.log
LogLevel warn
Alias /indico/images "/opt/indico/htdocs/images"
Alias /indico/css "/opt/indico/htdocs/css"
Alias /indico/js "/opt/indico/htdocs/js"
Alias /indico/ihelp "/opt/indico/htdocs/ihelp"
WSGIDaemonProcess WSGIDAEMON processes=32 threads=1 inactivity-
timeout=3600 maximumrequests=10000 python-
path=/usr/local/pythonenv/indico12/lib/python2.7/site-packages python-
eggs=/opt/indico/tmp/egg-cache
WSGIScriptAlias /indico "/opt/indico/htdocs/indico.wsgi"
<Directory "/opt/indico">
WSGIProcessGroup WSGIDAEMON
WSGIApplicationGroup %{GLOBAL}
AllowOverride None
Options FollowSymLinks
Require all granted
</Directory>
</VirtualHost>
The chown for the /etc/apach2 and /opt/indico directorys are for www-data user.
Thank you very much for your help.
Plz give me some advise. =)
Your probably not landing in that newly created virtualhost, it has no ServerName or ServerAlias. apachectl -S will show you the other virtualhosts you have. Even after specifying a ServerName or ServerAlias here, you may need to modify your default (000-default) virtualhost as it may implicitly use your local systems name as a ServerNAme (ubuntu leaves it blank which can hide a later vhost!)

Unable to connect to WSGI daemon process 'sampleapp'

I tried to install django in ubuntu server and i had 503 error in error.log
my apache2.conf is:
<VirtualHost x.xxx.xxx.xxx:8080>
ServerAlias www.samplesite.com
ServerAdmin info#samplesite.com
DocumentRoot /var/www/sampleapp/
ScriptAlias /cgi-bin/ /var/www/sampleapp/cgi-bin/
Alias /vstats/ /var/www/sampleapp/stats/
Alias /error/ /var/www/sampleapp/document_error
#SuexecUserGroup admin admin
CustomLog /var/log/apache2/domains/sampleapp.ir.bytes bytes
CustomLog /var/log/apache2/domains/sampleapp.ir.log combined
ErrorLog /var/log/apache2/domains/sampleapp.ir.error.log
WSGIProcessGroup sampleapp
WSGIScriptAlias /sampleapp /var/www/sampleapp/sampleapp/wsgi.py
WSGIDaemonProcess sampleapp python-path=/var/www/sampleapp:/var/www /sampleapp/envme/lib/python3.4/site-packages
<Directory /var/www/sampleapp/static/>
Require all granted
</Directory>
<Directory /var/www/sampleapp/media/>
Require all granted
</Directory>
<Directory /var/www/sampleapp/sampleapp/>
<Files wsgi.py>
require all granted
</Files>
</Directory>
<Directory /var/www/sampleapp/>
AllowOverride All
Options +Includes +Indexes +ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
and i have this error in my error.log:
(13)Permission denied: [client x.xxx.xx.xxx:59504] mod_wsgi (pid=26905): Unable to connect to WSGI daemon process 'sampleapp' on '/var/run/apache2/wsgi.26899.0.1.sock' as user with uid=1005.
I lost 2 days trying to solve it,
You can refer this url,
http://code.google.com/p/modwsgi/wiki/ConfigurationIssues#Location_Of_UNIX_Sockets
Adding this,
WSGISocketPrefix run/wsgi
will solve the error. The solution is to change where the socket files are kept to a location where Apache user can read them.

Can't get Apache to serve django admin static files

I'm trying to deploy Django to apache but can't get it to serve my static admin files. It seems to be looking for them under /var/www/static and I can't seem to be able to change that.
The admin site seem to be working except for styling. I get a title and a log in form. My django app is working too. It's the static files for the admin that aren't served.
Using Django 1.4.1.
The files are under /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static and linked to from /home/dutt/vaccapp/backend/static/admin.
The apache error log says this
[Sun Sep 30 10:57:20 2012] [error] [client 192.168.1.10] File does not exist: /var/www/home, referer: http://dathui.example.com/vaccapp/admin/
[Sun Sep 30 10:57:20 2012] [error] [client 192.168.1.10] File does not exist: /var/www/home, referer: http://dathui.example.com/vaccapp/admin/
But I'm not sure how to change it.
In my django site config I have
<VirtualHost *:80>
ServerAdmin me#host.com
ServerRoot "/home/dutt/vaccapp"
DocumentRoot "/home/dutt/vaccapp"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/dutt/vaccapp/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
Alias /static/ "/home/dutt/vaccapp/backend/static/"
<Directory "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
ServerRoot is not set in apache2.conf.
From my settings.py
STATIC_ROOT = '/home/dutt/vaccapp/backend/'
STATIC_URL = '/static/'
Nothing added to STATICFILES_DIRS.
This is added to my apache2.conf
WSGIScriptAlias /vaccapp /home/dutt/vaccapp/backend/wsgi.py
WSGIPythonPath /home/dutt/vaccapp
<Directory /home/dutt/vaccapp>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
ADMIN_MEDIA_PREFIX is set by default to /static/admin/ # Deprecated in Django 1.4 (now using STATIC_URL + 'admin/'. The result is the same.
Here's the fixes to the apache config:
Alias /static/admin "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static"
Alias /static "/home/dutt/vaccapp/backend/static"
<Directory "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
And the WsgiScriptAlias had to be moved from the main apache config into the VirtualHost.
After a long discussion we found the problem was that Django did not install the admin static properly ... they were symlinked to eachother (very weird). A Django reinstall fixed it and it worked fine now.
My low reputation forces me to write a whole answer to add a small detail to Igor's answer.
I simply added the apache config part to my configuration, but it was not sufficient. I had to change:
"/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static"
to
"/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin"
both in the first Alias and in the <Directory> directive.
Just like Blazor I had to use a small variation of the original. My setup includes graphite-web, which is using django admin. I'm just posting it for reference.
My apache's virtual host:
<VirtualHost *:80>
ServerName graphite.myhost.com
Redirect permanent / https://graphite.myhost.com/
</VirtualHost>
<VirtualHost *:443>
ServerName graphite.myhost.com
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/graphite.cert
SSLCertificateKeyFile /etc/apache2/ssl/ssl_graphite.key
SSLStrictSNIVHostCheck on
WSGIDaemonProcess _graphite processes=5 threads=5 display-name='%{GROUP}' inactivity-timeout=120 user=_graphite group=_graphite
WSGIProcessGroup _graphite
WSGIImportScript /usr/share/graphite-web/graphite.wsgi process-group=_graphite application-group=%{GLOBAL}
WSGIScriptAlias / /usr/share/graphite-web/graphite.wsgi
AliasMatch ^/admin/(.*)static/admin(.*)$ /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/$2
<Directory "/usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/">
Order allow,deny
Allow from all
</Directory>
Alias /content/ /usr/share/graphite-web/static/
<Location "/content/">
SetHandler None
</Location>
<Location "/">
Order allow,deny
allow from all
AuthType Basic
AuthName "Restricted Zone"
AuthBasicProvider wsgi
WSGIAuthUserScript /var/www/django_auth.wsgi
Require valid-user
</Location>
ErrorLog ${APACHE_LOG_DIR}/graphite-web_error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/graphite-web_access.log combined
</VirtualHost>
I also added STATIC_URL = 'static/' just to make sure I didn't have any issues with the regex.
Try using next
python manage.py collectstatic
The staticfiles app - Django documentation
I got similar problem when running Django + Apache, the Django admin site misses all styling. This is how I solved it:
apache conf: httpd-app.conf
Alias /static "/...path/to/your/django.../site-packages/django/contrib/admin/static"
<Directory "/...path/to/your/django.../site-packages/django/contrib/admin/static">
Require all granted
</Directory>
The /...path/to/your/django.../ above, you can find it by pip show django

Two Django projects running simultaneously and mod_wsgi acting werid

I'm trying to run two Django projects simultaneously. I happened to be using mod_wsgi, and found the site is acting weird. Perhaps there would be a workaround, but I'd like to know what I'm missing and how to solve the problem.
In the apache configuration
# Setup the Python environment
# As root owns basically everything on a Amazon AMI and root
# cannot be used. Create a folder under /var/run/wsgi
# with the owner as ec2-user and group ec2-user.
WSGISocketPrefix /var/run/wsgi
# Call your daemon process a name
WSGIDaemonProcess pydaemon processes=1 threads=5
# Call your daemon process group a name
WSGIProcessGroup pydaemon
# Point to where the handler file is. This will be different
# If you are using some other framework.
WSGIScriptAlias /test /var/www/html/test/wsgi.py
WSGIScriptAlias /proto /var/www/html/proto/wsgi.py
After Apache restarts, if I connect to '/proto', then the proto site is shown. However, then I connect to '/test', without restarting Apache, the proto site is still shown, and I cannot access to the test site.
Now I restart Apache, this time I go to '/test' first. The test site comes up! However, if I go to '/proto' it still shows the test site, not the proto site.
What could make this happen? I added SESSION_COOKIE_PATH differently for each application just in case, but the problem still exists.
[UPDATED]
I also tried as the following, to give different WSGI application group names, but without luck.
Alias /cuedit /var/local/test/wsgi.py
<Location /test>
SetHandler wsgi-script
Options +ExecCGI
WSGIApplicationGroup test
</Location>
Alias /proto /var/local/proto/wsgi.py
<Location /proto>
SetHandler wsgi-script
Options +ExecCGI
WSGIApplicationGroup proto
</Location>
[UPDATED]
I changed from the daemon mode to the embedded mode. I guess the problem was two instances shared the same mod_wsgi daemon process so their namespace collide.
I would expect they should be handled correctly, but in the daemon mode I couldn't get it right.
Use this as a workaround:
WSGIDaemonProcess pydaemon-1 processes=1 threads=5
WSGIDaemonProcess pydaemon-2 processes=1 threads=5
WSGIScriptAlias /test /var/www/html/test/wsgi.py
<Location /test>
WSGIProcessGroup pydaemon-1
WSGIApplicationGroup %{GLOBAL}
</Location>
WSGIScriptAlias /proto /var/www/html/proto/wsgi.py
<Location /proto>
WSGIProcessGroup pydaemon-2
WSGIApplicationGroup %{GLOBAL}
</Location>
This will force each application into separate daemon process group and no way they should be able to interfere with each other.
If that still doesn't work, you have problems with your WSGI script file somehow.
I also have 2 Django projects however each one is running on a different port (httpd config), it looks something like this:
<VirtualHost *:80>
ServerAdmin xx
ServerName xx
ServerAlias xx
ErrorLog /path/to/first/project/logs/error.log
CustomLog /path/to/first/project/logs/access.log combined
Alias /static/ /path/to/first/project/sitestatic
WSGIDaemonProcess app processes=1 threads=15 display-name=%{GROUP}
WSGIProcessGroup app
WSGIScriptAlias / /path/to/first/project/django.wsgi
<Directory /path/to/first/project/apache>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:8080>
ServerAdmin xx
ServerName xx
ServerAlias xx
ErrorLog /path/to/second/project/logs/error.log
CustomLog /path/to/second/project/logs/access.log combined
WSGIDaemonProcess app1 processes=1 threads=15 display-name=%{GROUP}
WSGIProcessGroup app1
WSGIScriptAlias / /path/to/second/project/apache/django.wsgi
<Directory /path/to/second/project/apache>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
The problem might be related to Apache sharing the Python sub interpreter between WSGI applications. Try adding this to the Apache configuration to avoid sharing:
WSGIApplicationGroup %{GLOBAL}
Check this blog post for in-depth explanation and additional tips (check the comments too).
Can't comment on the answer given by Graham, so adding one of my own.
The problem for me really was the Python Interpreter, but I also had to add the python-path for each interpreter. Here follows an example configuration:
WSGIDaemonProcess py_app1 processes=1 threads=5 python-path=/path/to/app1
WSGIScriptAlias /app1 /path/to/app1/wsgi.py
<Directory /path/to/app1>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
<Location /app1>
WSGIProcessGroup py_app1
WSGIApplicationGroup %{GLOBAL}
</Location>
WSGIDaemonProcess py_app2 processes=1 threads=5 python-path=/path/to/app2
WSGIScriptAlias /app2 /path/to/app2/wsgi.py
<Directory /path/to/app2>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
<Location /app2>
WSGIProcessGroup py_app2
WSGIApplicationGroup %{GLOBAL}
</Location>