EC2: Deploying a django project with a apache server - python

I have set up an ec2 instance and I'm able to connect to it via ssh. I've installed apache, django, git and all the necessary dependencies.
I've managed to clone a git repository which is located in /home/ubuntu/.
I've also set up the configuration file in /etc/apache2/sites-enabled/hyper-blog.conf.
This is my hyper-blog.conf file:
<VirtualHost *:80>
ServerName ec2-52-11-240-28.us-west-2.compute.amazonaws.com
ServerAlias ec2-52-11-240-28.us-west-2.compute.amazonaws.com
ServerAdmin webmaster#localhost
DocumentRoot /home/ubuntu/hyper-blog/
AddDefaultCharset UTF-8
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
alias /static/ /home/ubuntu/hyper-blog/static/
alias /media/ /home/ubuntu/hyper-blog/media/
<Directory "/home/ubuntu/hyper-blog/static">
Require all granted
Options -Indexes
</Directory>
<Directory "/home/ubuntu/hyper-blog/media">
Require all granted
Options -Indexes
</Directory>
<Directory /home/ubuntu/hyper-blog/hyper-blog>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
#RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.hyperiondev.com%{REQUEST_URI}
WSGIDaemonProcess hyper-blog python-path=/home/ubuntu/hyper-blog:/usr/local/lib/python3.4/dist-packages
WSGIProcessGroup hyper-blog
WSGIScriptAlias / /home/ubuntu/hyper-blog/hyper-blog/wsgi.py
The problem is when I go to my public domain: ec2-52-11-240-28.us-west-2.compute.amazonaws.com it takes a while to load and eventually does not render anything.
I need to know how to get my application up and running. I've tried this documentation on digital ocean https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04,
but still doesn't work.
How can I get my application working without installing virtualenv?

Related

Django Apache server error. no module named 'psycopg2._psycopg' [Dulicate, but duplicate answers didn't work :(]

I am running ubuntu-server 20 and I am trying to run django deplyments server on it with apache2 and libapache2-mod-wsgi-py3. I was having many errors but i somehow managed to fix them but I am stuck on this error:
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2._psycopg'
psycopg2 and psycopg2-binary are installed in my env. Also I am using python version 3.9.
Here are my apache server configs (if you need them)
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ServerName darkzone
Alias /static /home/nika/HackerForum/staticfiles
<Directory /home/nika/HackerForum/staticfiles>
Require all granted
</Directory>
Alias /media /home/nika/HackerForum/media
<Directory /home/nika/HackerForum/media>
Require all granted
</Directory>
<Directory /home/nika/HackerForum/HackerForum>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/nika/HackerForum/HackerForum/wsgi.py
WSGIDaemonProcess darkzone python-path=/home/nika/HackerForum python-home=/home/nika/HackerForum/venv
WSGIProcessGroup darkzone
</VirtualHost>
WSGIPythonHome /home/nika/HackerForum/venv
WSGIPythonPath /home/nika/HackerForum

Apache with Wsgi Module changes in source file does not get affected immediately

Apache with Wsgi Module changes in django app source file does not get affected immediately but it reflects the changes only when the apache restarts.
configuration of my apache setting is
<VirtualHost *:80>
ServerName pyspacex.com
ServerAlias pyspacex.com
CustomLog logs/pyspacex-access_log common
ErrorLog logs/pyspacex-error_log
Alias /static/ /home/mrll0081/mysite/spacex/static/
<Directory /home/mrll0081/mysite/spacex/static>
Require all granted
</Directory>
WSGIDaemonProcess pyspacex python-home=/home/mrll0081/pyenv python-path=/home/mrll0081/mysite
WSGIProcessGroup pyspacex
WSGIScriptAlias /spacex /home/mrll0081/mysite/mysite/wsgi.py
<Directory /home/mrll0081/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>

how to use mod_wsgi for hosting multiple django projects under single domain?

I have multiple django projects and i want to host them under same domain
eg: example.com/one<br> example.com/two
I have searched for various solutions and found the below given link which helped me alot.
Is it possible to host multiple django projects under the same domain?
From the above reading , I get to know that I need mod_wsgi for this but I am confused that where to install this mod_wsgi - Do i need to install under every project folder (seperate for every myenv) or it should be installed only once .
Please help me in how and where to install this mod_wsgi and finally how to host multiple projects under same domain name.
Some Code Tried By Another User With Same Problem But Also Not Working
<VirtualHost *:80>
ServerAdmin admin#my_domain.com
ServerName my_domain.com
ServerAlias www.my_domain.com
DocumentRoot /var/www/my_domain.com
ErrorLog ${APACHE_LOG_DIR}/my_domain.com_error.log
CustomLog ${APACHE_LOG_DIR}/my_domain.com_access.log combined
# site_1
Alias /site_1_project/static /var/www/my_domain.com/site_1_project/static
<Directory /var/www/my_domain.com/site_1_project/static>
Require all granted
</Directory>
Alias /site_1_project/media /var/www/my_domain.com/site_1_project/media
<Directory /var/www/my_domain.com/site_1_project/media>
Require all granted
</Directory>
<Directory /var/www/my_domain.com/site_1_project/site_1_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess site_1_project python-path=/var/www/my_domain.com/site_1_project python-home=/var/www/my_domain.com/site_1_project/django_env_site_1
WSGIProcessGroup site_1_project
WSGIScriptAlias /site_1_project/ /var/www/my_domain.com/site_1_project/site_1_project/wsgi.py
# site_2
Alias /site_2_project/static /var/www/my_domain.com/site_2_project/static
<Directory /var/www/my_domain.com/site_2_project/static>
Require all granted
</Directory>
Alias /site_2_project/media /var/www/my_domain.com/site_2_project/media
<Directory /var/www/my_domain.com/site_2_project/media>
Require all granted
</Directory>
<Directory /var/www/my_domain.com/site_2_project/site_2_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess site_2_project python-path=/var/www/my_domain.com/site_2_project python-home=/var/www/my_domain.com/site_2_project/django_env_site_2
WSGIProcessGroup site_2_project
WSGIScriptAlias /site_2_project/ /var/www/my_domain.com/site_2_project/site_2_project/wsgi.py
#RewriteEngine on
#RewriteCond %{SERVER_NAME} =my_domain.com [OR]
#RewriteCond %{SERVER_NAME} =www.my_domain.com
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Installing mod_wsgi depends on what your host OS is. Check the instructions. If you're using CentOS or RedHat, I'd recommend looking at IUS Community; they provide a repository with yum installable packages for Python 3.6 and mod_wsgi. The version of mod_wsgi you install has to be compiled against the same version of Python you are running into your virtual environment.
Then you need to configure your VirtualHost properly. If you have a host at the root, it has to come last in your definition. Here's an example:
<VirtualHost *:443>
TimeOut 300
SSLEngine On
ServerName mysite.example.com
# Set to the lobal Application Group
WSGIApplicationGroup %{GLOBAL}
# Pass Authorizations through to the WSGI app for Django REST Framework Token Auth
WSGIPassAuthorization On
WSGIDaemonProcess subsite-develop-https python-home=/web/subsite-develop/venv request-timeout=300 user=apache group=apache
WSGIProcessGroup subsite-develop-https
WSGIScriptAlias /subsite /web/subsite-develop/config/wsgi.py process-group=subsite-develop-https
<Directory /web/subsite-develop/config>
Require all granted
</Directory>
Alias /subsite/static/ /web/subsite-develop/static/
<Directory /web/subsite-develop/static>
Header always set Access-Control-Allow-Origin "*"
Require all granted
</Directory>
WSGIDaemonProcess django-mysite-develop-https python-home=/web/django-mysite-develop/venv request-timeout=300 user=apache group=apache
WSGIProcessGroup django-mysite-develop-https
WSGIScriptAlias / /web/django-mysite-develop/config/wsgi.py process-group=django-mysite-develop-https
<Directory /web/django-mysite-develop/config>
Require all granted
</Directory>
Alias /static/ /web/django-mysite-develop/static/
<Directory /web/django-mysite-develop/static>
Header always set Access-Control-Allow-Origin "*"
Require all granted
</Directory>
Alias /media/ /var/media/mysite-www/
<Directory /var/media/mysite-www>
Require all granted
</Directory>
</VirtualHost>
This example will host one site at /subsite/ and another at the root, /. Notice that the root site comes last. It also means that you won't be able to use the route /subsite/ within the root project, since Apache will have diverted it to via the WSGIScriptAlias definition.
This is also for a site with TLS; you may have to switch the 443 to 80, and remove SSLEngine On if you're not using TLS. The WSGIPassAuthorization is for Django REST Framework tokens, you can probably remove it as well, but I've left it for a more complete example. This is for Apache 2.4+, when they switched to the Require all granted syntax.
IUS Community, if on RedHat/CentOS: https://ius.io/
I'll tell you how we did in our project. We have a single Django project with different routes. For example /players, /tablet. We hosted our project in two Docker containers. We have NGINX as our reverse proxy. NGINX redirects the request to the appropriate container based on the route. NGINX is exposed to the world. But, I'm not sure if it is useful for you or not.

Troubles with running django app via wsgi on ubuntu 17

I need your help.
I have an error when trying to run on wsgi my django project.
I'm using Ubuntu 17, Apache2, Django 2.0, Python 3.6
When I running from manage.py, everything is working, but when via wsgi got the next error :
AH01276: Cannot serve directory /var/cardsite/cardsite/: No matching
DirectoryIndex
(index.html,index.cgi,index.pl,index.php,index.xhtml,index.htm) found,
and server-generated directory index forbidden by Options directive
And don't know why, because I guess set everything correct. Bellow my configuration :
apache.conf
<VirtualHost *:80>
CustomLog /var/log/apache2/cardsite-access.log common
ErrorLog /var/log/apache2/cardsite-error.log
DocumentRoot /var/cardsite/cardsite/
Alias /static /var/cardsite/cardsite/static/
<Directory /var/cardsite/cardsite/static>
Require all granted
</Directory>
<Directory /var/cardsite/cardsite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
WSGIDaemonProcess cardsite python-path=/var/cardsite/ python-home=/var/venv_python36/
WSGIProcessGroup cardsite
WSGIScriptAlias / /var/cardsite/cardsite/wsgi.py
</VirtualHost>
wsgi.py
import os
import sys
PROJECT_DIR = '/var/cardsite'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cardsite.settings")
def execfile(filename):
globals = dict( __file__ = filename )
exec( open(filename).read(), globals )
activate_this = os.path.join( '/var/venv_python36/bin', 'activate_this.py' )
execfile( activate_this )
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
P.S. Permissions I have give on folder and all which inside.
P.S.S. Packages like "libapache2-mod-wsgi-py3" or "mod-wsgi" via pip3 also installed.
Thank you all for the any suggestion what's it can be
Don't set DocumentRoot to be a parent directory of your source code. If you were to take the WSGIScriptAlias out, people could download your source code. You should avoid the risk of that, even if WSGIScriptAlias currently intercepts everything under /. That DocumentRoot directory doesn't allow access may be part of the problem also.
Try:
<VirtualHost *:80>
CustomLog /var/log/apache2/cardsite-access.log common
ErrorLog /var/log/apache2/cardsite-error.log
DocumentRoot /var/cardsite/htdocs
<Directory /var/cardsite/htdocs>
Require all granted
</Directory>
Alias /static /var/cardsite/cardsite/static
<Directory /var/cardsite/cardsite/static>
Require all granted
</Directory>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
WSGIDaemonProcess cardsite python-path=/var/cardsite python-home=/var/venv_python36
WSGIProcessGroup cardsite
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /var/cardsite/cardsite/wsgi.py
<Directory /var/cardsite/cardsite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
The WSGIApplicationGroup has been added as always a good idea to have that, with only one WSGI application delegated to the daemon process group.
Make sure you create the directory:
/var/cardsite/htdocs
Finally, you are missing a ServerName directive. So if this isn't the default VirtualHost, your request may not even be getting handled by this VirtualHost.

Unable to access django projects' named server after deploying on apache virtual host

Unable to access django projects' named server after deploying on apache virtual host.
Below is the tag snippet that I have came up with.
Listen 8000
<VirtualHost *:8000>
DocumentRoot "/var/www/ecomgroup/ecom/ecomstore-8000"
ServerName fpshoppingfashionpassion.com
ServerAdmin webmaster#localhost
Alias /static /var/www/ecomgroup/ecom/ecomstore/public/static
<Directory /var/www/ecomgroup/ecom/ecomstore/public/static>
Require all granted
</Directory>
<Directory /var/www/ecomgroup/ecom/ecomstore/ecomstore/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess econroot.com python-path=/var/www/ecomgroup:/var/www/ecomgroup/venv/lib/python2.7/site-packages
WSGIProcessGroup econroot.com
WSGIScriptAlias / /var/www/ecomgroup/ecom/ecomstore/ecomstore/wsgi.py
</VirtualHost>
Please let me know if something else goes here.

Categories

Resources