I am new to django and this is my first project in django. So far, I have developed a django app. It is working fluently in my local machine but I’m unable to deploy it online.
I have seen many tutorials on internet for deploying the app on server. But none of them seems to be working to me. May be I’m doing something wrong over here.
https://www.youtube.com/watch?v=hBMVVruB9Vs https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/modwsgi/ https://www.digitalocean.com/community/tutorials/how-to-deploy-a-local-django-app-to-a-vps https://www.digitalocean.com/community/tutorials/how-to-run-django-with-mod_wsgi-and-apache-with-a-virtualenv-python-environment-on-a-debian-vps
I followed these tutorials and tried to deploy the app on the server but I always gets 403 forbidden error. I tried to remove this error and referred other stackoverflow answer as well but no success..
403 Forbidden error with Django and mod_wsgi
Apache mod_wsgi error: Forbidden You don't have permission to access / on this server
Django on apache wtih mod_wsgi (Linux) - 403 Forbidden
Installing Django with mod_wsgi
Here is the structure that i created for my django project. I have two apps in my django project and I’m connecting my one app connect with DNS xyz.com and my second app connect with its subdomain abc.xyz.com.
Structure
project_folder
|
|->app1
| |->urls.py
| |->template_folder
| |->static_folder
|->app2
| |->urls.py
| |->template_folder
| |->static_folder
|->project_name
|->urls.py
|->wsgi.py
project_name/urls.py
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^app1/', include('app1.urls')),
url(r'^app2/', include('app2.urls')),
]
app1/urls.py
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^example1/', views.example1),
url(r'^example2/', views.example2),
)
app2/urls.py
urlpatterns = patterns('',
url(r'^example3/', views.example3),
url(r'^example4/', views.example4),
)
Basically what I’m trying to do is that my app1 runs on xyz.com and my app2 runs on abc.xyz.com (sub domain). So that all app in the project having same user login. If a user login from one app it will be logged in the second app also.
On my local machine the app runs as
http://localhost:8000/app1
http://localhost:8000/app1/example1
http://localhost:8000/app1/example2
http://localhost:8000/app2/example3
http://localhost:8000/app2/example4
Here is my apache2 conf file that i create on my server
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName xyz.com
DocumentRoot /home/user_name/project_folder/
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
AddHandler mod_python .py
PythonHandler mod_python.publisher | .py
PythonDebug On
</Directory>
<Directory /home/user_name/project_folder/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /home/user_name/project_folder/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /home/user_name/project_folder/access.log combined
</VirtualHost>
Hi @Daniel Roseman was right with the comment. I went thru the document over again and tried with the way it is mentioned in the pages. And I finally resolved the issue.
This is how I resolved it..
app1.conf
<VirtualHost *:80>
ServerName xyz.com
WSGIScriptAlias / /var/www/html/project_folder/project_name/wsgi.py
WSGIDaemonProcess xyz.com python-path=/var/www/html/project_folder:/usr/local/lib/python2.7/site-packages
WSGIProcessGroup xyz.com
<Directory /var/www/html/project_folder/project_name>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enable the app1.conf file using command sudo a2ensite app1.conf.
For domain and subdomain i used django package django-subdomains
setting.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites', # For include sites
'app1',
'ap2',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'subdomains.middleware.SubdomainURLRoutingMiddleware', # Subdomain package
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
SITE_ID = 1
ROOT_URLCONF = 'app1.urls'
SUBDOMAIN_URLCONFS = {
None: 'app1.urls',
'www': 'app1.urls',
'abc': 'app2.urls',
}
After all settings done run command python manage.py migrate. It will create django_site table in database with one entry. Change example.com to xyz.com.
For subdomain create new app2.conf file and change xyz.com to abc.xyz.com.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With