I am digging arround from some days and i can not figure out how to deploy a Django Project.
I have a Amazon AWS EC2 machine running.
I started the project on my laptop, then i configured and installed Virtualenv on AWS EC2 and all the requeriments.
This machine has Apache2 and libapache2-mod-wsgi and mysql installed.
When i do the command python manage.py runserver there are NO errors! I did the syncdb and everything is ok.
But a can not see the page in my browser.
Apache config:
File: /etc/apache2/sites-enabled/test_site.com.conf
WSGIScriptAlias / /home/ubuntu/server/workspace/proj/proj/wsgi.py
WSGIPythonPath /home/ubuntu/server/workspace/proj/
<Directory /home/ubuntu/server/workspace/proj/proj>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
Alias /static/ /home/ubuntu/server/workspace/proj/static/
<Directory /home/ubuntu/server/workspace/proj/static>
Require all granted
</Directory>
How can i Deploy my Django?
Thanks,
Ricardo
I found a way to solve my problem.
For new comers, with a similar problem, take a look at this page Deploy django in Apacha2 server and mod wsgi
You can use this script that I wrote:
Safely deploy your Django app in less 1 minute!
Instructions
Installing the DeployDjango script
$ wget https://raw.githubusercontent.com/yask123/DeployDjango/master/deploydjango.sh && chmod +x deploydjango.sh
From your Django App’s root directory (Where manage.py file exists).
$ sudo ./deploydjango.sh project_name
Done!
Visit http://ip-address-of-your-instance to see your web app live!
Related
I am trying to deploy a Django Application using Apache on an Ubuntu Server, I am having a weird error (500 internal error server error) when using port :80.
I made some test using port :8000 running the command:
python3 manage.py runserver 0.0.0.0:8000
And the application is working without any problem there, but when I try to access the application using port 80 it does work.
My apache config file looks like this:
<VirtualHost *:80>
Alias /static /home/ubuntu/gh_system/static
<Directory /home/ubuntu/gh_system/static>
Require all granted
</Directory>
<Directory /home/ubuntu/gh_system/HRSYSTEM>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess HRSYSTEM python-home=/home/ubuntu/gh_system/env python-path=/home/ubuntu/gh_system>
WSGIProcessGroup HRSYSTEM
WSGIScriptAlias / /home/ubuntu/gh_system/HRSYSTEM/wsgi.py
I reviewed the apached2 errors logs, and I am getting a: No module named 'django', my wsgi files is:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "HRSYSTEM.settings")
application = get_wsgi_application()
After some research, testing and reviewing the apache logs with:
sudo tail -100 /var/log/apache2/error.log
I found that the problem was related to this line:
WSGIDaemonProcess HRSYSTEM ***python-home=/home/ubuntu/gh_system/env*** python-path=/home/ubuntu/gh_system>
Basically, apache was trying to read the packages inside of the virtual environment but I did install them using the command:
sudo pip3 install -r requirements.txt
Apparently, using the "sudo" command was installing the packages outside the virtual environment, it is so weird; but without "sudo" the console logs the error permission denied, so I give chmod 700 permission to the virtual environment and install again the packages without the "sudo", and then it works!
Some of the things I learned about the errors:
Problem: Not module 'encodings' --> this problem occurs when apache can not access your packages, usually for a bad path specified in the config file.
Problem: Not module 'x module, could be django' means you need to install the package using pip3.
Hope it helps someone! and just to point out, something I have been learning lately, is: when you spend too much time trying to solve a problem, and there is not much information about it on internet, it means that something really, really basic is failing in your project; for example, installing the packages outside the virtual environment without even realizing it, just like I did.
I have a deployed Django app on a Centos 7 server. I ran into problems when git pulling from my repository. It doesn't recognize python-home at the location where my virtualenv is anymore.
When i print(sys.prefix), it shows '/usr', but when i activate my virualenv (source /path/to/virtualenv/bin/activate) it shows the correct path that is assigned to python-home in my httpd .conf file
.conf file
Alias /static /var/www/ek/static
<Directory /var/www/ek/static>
Require all granted
</Directory>
<Directory /var/www/ek/new>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess ek python-path=/var/www/ek python-home= /var/www/kpi_env
WSGIProcessGroup ek
WSGIScriptAlias / /var/www/ek/new/wsgi.py
I'm not exactly sure what the problem is, when im not in my virtualenv, should the prefix be the path to the virtualenv? Should print(sys.prefix) only show var/www/kpi_env when im in the virtualenv?
Whatever it is, it doesn't recognize python-home=/var/www/kpi_env as anything anymore.
When i change it to python-home = /usr it works but the application is giving me errors that i've never run into before on my server or on my workstation. This error being Permission denied is Server running on host 12.0.0.1 and accepting tcp con on port 5432 This is referring to postgresql, and i'm sure that it is running and accepting tcp connection on port 5432. And that my settings.py reflect the right configurations. I havent changed any configurations with changes with git pull and havent touched configs for postgresql at all, so i can only think that its because of the python library it is using, because thats the only thing that has changed.
How do i make my httpd server recognize python-home= /var/www/kpi_envagain ?
This doesn't exactly answer what i asked but this was the workaround i did:
I ran this command to allow my httpd server to accept the connection to my postgresql database,
setsebool -P httpd_can_network_connect_db 1
-P will do this permanently and 1 will set it to on.
And in this, i used python-home = /usr since i couldnt get my virtualenv to work anymore
I have a django web application which I need to serve from an apache server on CentOS 6.8. In order to get this, I´m using mod_wsgi.
I have my project located in /path/to/myproject.
Right there I have the following directories and files:
- app1/
- db.sqlite3
- myproject/
- myprojectenv/
- manage.py
- static/
I have added to my setting.py this directive: STATIC_ROOT = os.path.join(BASE_DIR, "static/") and I´ve tried to run the django server and I can see my app view correctly.
So, after that I´ve tried to configure apache server in order to send a petition from my browser to my app, but apache rises a 403 Forbidden error.
The steps that I followed are:
- Add to httpd.conf the following information:
<VirtualHost *:80>
Alias /static /path/to/myproject/static
Alias /app1 /path/to/myproject/app1
<Directory /path/to/myproject/static>
Order allow,deny
Allow from all
</Directory>
<Directory /path/to/myproject/app1>
Order allow,deny
Allow from all
</Directory>
<Directory /path/to/myproject/myproject>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
WSGIDaemonProcess myproject user=myuser group=mygroup python-path=/path/to/myproject:/path/to/myproject/myprojectenv/lib/python2.7/site-packages
WSGIProcessGroup myproject
WSGIScriptAlias / /path/to/myproject/myproject/wsgi.py
</VirtualHost>
Then, I gave rights to apache user, in order to allow apache to have access to my django project:
- sudo usermod -a -G myuser apache
- chmod 710 /home/myuser
- chmod 664 ~/myproject/db.sqlite3
- sudo chown :apache ~/myproject/db.sqlite3
- sudo chown :apache ~/myproject
Finally, I restarted the htttpd service and tried to send a petition to my app, but I got a "403 Forbidden error".
After that, I tried to copy my django project to the apache directory /var/www/ and doing this, I finnaly could see my django files but just like files, apache couldn´t run my app and show my view correctly.
I think that the problem could be about the permissions of apache user but I don´t know what else I can do.
What am I doing wrong?
What is the actual error in the Apache error log, not the 403 error in the browser?
Most likely the problem is due to your home directory being 710. This means an other user such as the Apache user cannot see into your home directory. It needs to be able to do that as the Apache user to map the URL to the WSGI application.
Either move your whole project outside of your home directory, or create a separate WSGI script file called myproject.wsgi under /var/www/myproject or similar, which has in it:
from myproject.wsgi import application
Also change:
WSGIScriptAlias / /path/to/myproject/myproject/wsgi.py
<Directory /path/to/myproject/myproject>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
to:
WSGIScriptAlias / /var/www/myproject/myproject.wsgi
<Directory /var/www/myproject>
<Files myproject.wsgi>
Order allow,deny
Allow from all
</Files>
</Directory>
The Apache user only needs to be able to see the WSGI script file when doing URL mapping. The application code will actually run in your case under daemon mode with user myuser which presumably can read stuff from your home directory.
You didn't need to change ownership of your directory/files to Apache user, again because the code will run as myuser.
I'm trying to deploy this repo to Elastic Beanstalk: https://github.com/jordanmessina/test_beanstalk/tree/5e9a919d991adf22bce1a9b8f6c1515e83b1babb with this configuration: 64bit Amazon Linux 2014.09 v1.0.9 running Python 2.7
I'm getting the following error on deployment:
ERROR: [Instance: i-2eb19dc2 Module: AWSEBAutoScalingGroup ConfigSet: null] Command failed on instance. Return code: 1 Output: [CMD-AppDeploy/AppDeployStage0/EbExtensionPostBuild] command failed with error code 1: Error occurred during build: Command 01_syncdb failed.
When I go to the webpage after deployment, all I see is: Index of /
Is there something wrong with my configuration file?
UPDATE
I made some changes and managed to get the app running and 500ing (progress).
I've poked around on the instances the code is deployed to. I've narrowed the issue down to the mod_wsgi pythonpath that's setup. Here's the configuration:
LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome /opt/python/run/baselinenv
WSGISocketPrefix run/wsgi
WSGIRestrictEmbedded On
<VirtualHost *:80>
Alias /static/ /opt/python/current/app/static/
<Directory /opt/python/current/app/static/>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /opt/python/current/app/webapp/beanstalk/wsgi.py
<Directory /opt/python/current/app/>
Require all granted
</Directory>
WSGIDaemonProcess wsgi processes=1 threads=15 display-name=%{GROUP} \
python-path=/opt/python/current/app:/opt/python/run/venv/lib/python2.7/site-packages user=wsgi group=wsgi \
home=/opt/python/current/app
WSGIProcessGroup wsgi
</VirtualHost>
I also found the logs for the httpd process (standard /var/log/httpd/error_log for future reference) and was tailing them to see what the issue was. The problem was an ImportError with beanstalk.settings (because it's not on the path).
Now I'm trying to think of a sane way to keep my app structure (webapp directory in the root containing my django app) and update the python path for mod_wsgi so this thing works...I'm very frustrated with the opinionated app structure that Elastic Beanstalk is trying to push on me. Any suggestions would be great!
You can't have the DB locally on elastic beanstalk as you are attempting. EB is meant just as an application container, what you have there will try and setup SQLite on the system, which beanstalk isn't gonna let you do.
You need to configure Django to point to some DB off the beanstalk, there is no DB on that instance and your Django config doesn't seem to have any DB configured on it as you seem to just be trying to use the included SQLite DB, which I'd imagine they don't let you install that on beanstalk, so why not try configuring an another DB or using no DB at all, if possible.
I'd spin up a small RDS instance and configure my Django app to tie into that. That should allow you to sync the DB and get started. It also might be a matter of SQLite not being on the system, so you'd have to configure EB to install that if it's even possible.
First time I setup Django and Apache, and I'm having a tough time. It's seems easy by following tutorials, but it doesn't seem to work.
Basically, the steps I followed (on Debian):
Install Django (and some libraries)
Install Apache2
Install mod_wsgi
Then I've put my Django app in /root/, so the path is something like :
root/
.Projet/
.myprojet/
.site/
. .#here the models.py, views.py, etc of my site
.myprojet/
. .#here the settings.py, wsgi.py
. .static/
. .#static files in folders
.templates
. .#my templates
.database.sql
.manage.py
Then in /etc/apache2 I created the file httpd.conf, with this inside :
WSGIScriptAlias / /root/Projet/myprojet/myprojet/wsgi.py
WSGIPythonPath /root/Projet/myprojet
<Directory /root/Projet/myprojet/myprojet>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
I do a2enmod wsgi (it tells me "Module wsgi already enabled").
Then service apache2 restart (successful, but tells me I have no VirtualHosts).
When I go to my website (using the IP adress), just the Apache "It works" display.
Thank you for your help ^^
You shouldn't put your Apache configuration file in /etc/apache2/. The fact that you mention a2enmod implies that you are on a Debian-based distro like Ubuntu. For those systems, you need to give that file a name related to the app - call it django if you like - and put it in /etc/apache2/sites-available. Then you can run a2ensite django to link it to sites-enabled, restart Apache, and it should work.