Problem while deploying a Django app with Apache2 on Ubuntu - python

I've been trying to deploy a Django app on Ubuntu 18.04 using Apache2 for the past couple of days now, and it still doesn't work.
My apache2 config is:
<VirtualHost *:1337>
<Directory /var/www/LGSM_webpanel/LGSM_webpanel>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/LGSM-error.log
CustomLog ${APACHE_LOG_DIR}/LGSM-access.log combined
WSGIScriptAlias / /var/www/LGSM_webpanel/LGSM_webpanel/wsgi.py
WSGIDaemonProcess LGSM_webpanel python-path=/var/www/LGSM_webpanel
WSGIprocessGroup LGSM_webpanel
DocumentRoot /var/www
</VirtualHost>
However, for some reason, when I access http://localipofthemachine:1337 I just get the google chrome error Connection refused.
Now, the server works when I use manage.py runserver, and I can fully access it, but when I use apache, I just get this error.
The apache logs are also empty by the way.
Thanks in advance for your help

First of all, you need to add Listen 1337 in the virtual host configuration, i.e.
Listen 1337
<VirtualHost *:1337>
# your configuration
# etc
</VirtualHost>
Second, make sure that the apache user www-data can access your webapp files by running chown www-data:www-data -R /var/www/LGSM_webpanel.

Related

Django : stop access to my web app from being accessed on port 8000 and access only using the IP

I have deployed my Django web app on a digital ocean droplet. The python server is running on port 8000.
python manage.py runserver 0.0.0.0:8000
I have configured apache web server to serve the requests.
/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /etc/myproject
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
#Serving static files
Alias /static/ /etc/myproject/static/
<Directory /etc/myproject/static>
Require all granted
</Directory>
<Directory /etc/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/etc/myproject python-home=/etc/myprojectenv
WSGIProcessGroup myproject
WSGIScriptAlias / /etc/myproject/myproject/wsgi.py
</VirtualHost>
Now when I am accessing my web app using the IP address X.X.X.X, I am able to see my app, however the app is also accessible on port 8000, X.X.X.X:8000.
I want to prevent my app from being accessed on any other port except being accessed ousing IP. How can I do that?
Okay I was missing a point here that I don't need to run Django server as well when I have configured Apache web server to serve my requests. All request are served by the mod_wsgi Apache module. The mod_wsgi package provides an Apache module that implements a WSGI compliant interface for hosting Python based web applications on top of the Apache web server.

Deploying Django app : Forbidden You don't have permission to access this resource

I was deploying my django app on my VPS using the apache web server.
It works totally fine on http://192.46.209.82:8000/ but when I try to access the same using my IP address, 192.46.209.82. I get
Forbidden
You don't have permission to access this resource.
Apache/2.4.41 (Ubuntu) Server at 192.46.209.82 Port 80
Here is my conf file
nano /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster#example.com
DocumentRoot /root/django/myproject
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /root/django/myproject/static
<Directory /root/django/myproject/static>
Require all granted
</Directory>
<Directory /root/django/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/root/django/myproject python-home=/root/django/myprojectenv
WSGIProcessGroup myproject
WSGIScriptAlias / /root/django/myproject/myproject/wsgi.py
</VirtualHost>
I am using python's virtual environment, where myprojectenv is my virtual env path and the pwd is /root/django
Note : I tried the answer from the question that I was getting as suggestion to this question but that did not work for me.
EDIT : Updated permission
In my case even though I set up all the permission correctly, I still was getting error so I moved my entire project and virtual environment to /opt and it worked for me for now. But I would like to know the reason why it was not working under /home even when I gave all permissions

PGAdmin4 virtual host not working with Apache2 + WSGI

I have a problem. I'm creating a server where I will host a Django project and also PGAdmin4. I configured PGAdmin to run through apache2 and WSGI with a Python 3.8 virtual environment. It was working perfectly before I set up the Django project. at the ip/pgadmin. As soon as i manage to host the Django on the virtual host. The pgadmin started not to work.
When trying to enter the "ip/pgadmin" while the django webserver is not at the root at the WSGIScriptAlias i receive this page.
Not Found
The requested URL was not found on this server.
Apache/2.4.41 (Ubuntu) Server at 192.168.0.10 Port 80
and the following log error. The pgadmin4 is installed withing the same virtualenv that runs the django project but as they are daemons thats not a problem. So i do not see why is searching under "/var/www/acas_webserver/pgadmin/"
[Tue Mar 16 00:54:22.633470 2021] [core:info] [pid 26119:tid 140348751849216] [client 192.168.0.5:63072] AH00128: File does not exist: /var/www/acas_webserver/pgadmin/
But when i place the django project at the root of the WSGIScriptAlias i reacieve this page but no log info to know what happened.
Not Found
The requested resource was not found on this server.
I'm trying to get both of them work alone and having my django at the root of the directories of the apache2 virtual hosts + wsgi. I cannot fint whats the problem an its been countless hours without figuring it out. where are the virtual hosts configurations
#django project
<VirtualHost *:80>
ServerAdmin lemanuel.colon#upr.edu
ServerName 192.168.0.10
DocumentRoot /var/www/acas_webserver
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
LogLevel info
Alias /static /var/www/acas_webserver/static
<Directory /var/www/acas_webserver/static>
Require all granted
</Directory>
Alias /media /var/www/acas_webserver/media
<Directory /var/www/acas_webserver/media>
Require all granted
</Directory>
<Directory /var/www/acas_webserver/adapts>
<Files wsgi.py>
Require all granted
</Files>
Require all granted
</Directory>
WSGIDaemonProcess acas_app python-path=/var/www/acas_webserver python-home=/var/www/acas_webserver/my_venv
WSGIProcessGroup acas_app
WSGIScriptAlias / /var/www/acas_webserver/adapts/wsgi.py
WSGIApplicationGroup %{GLOBAL}
</VirtualHost>
#Pgadming
<VirtualHost *:80>
ServerName 192.168.0.10
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/pgadmin-error.log
CustomLog ${APACHE_LOG_DIR}/pgadmin-access.log combined
DocumentRoot /var/www/acas_webserver/my_venv/lib/python3.8/site-packages/pgadmin4/
LoadModule wsgi_module modules/mod_wsgi.so
WSGIDaemonProcess pgadmin processes=1 threads=25 python-home=/var/www/acas_webserver/my_venv
WSGIScriptAlias /pgadmin /var/www/acas_webserver/my_venv/lib/python3.8/site-packages/pgadmin4/pgAdmin4.wsgi
<Directory /var/www/acas_webserver/my_venv/lib/python3.8/site-packages/pgadmin4/>
WSGIProcessGroup pgadmin
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
</VirtualHost>
I just want to get them work to continue on with this projects and its been several days of headaches. Can someone be of help here? If anything is need im happy to provide. Plz help.
UPDATE:
The problem is all coming up once I enable the django webserver. As soon as i turn of my django webserver on Apache2, i can access my pgadmin page, but as soon as I turn on my django webserver on Apache2, the pgadmin again is unreachable.
I have looked at your configs ( e.x apache ) and seem fine.
I think, there are many possible ways to solve your problem.
The first way would be using some docker container that fits your needs. if you don't know about it just "GOOGLE: docker Django Postgres apache " and read the top links.
Search for it and there are many results already there. After that, you can run your project probably. I don't want to go deep into docker but surely would solve your problem.
Second, Check and monitor your services, ports, and permissions. If they are not in right place, maybe your server ports are busy or you should restart the services. (as you said worked fine before the Django installation, this idea has a low chance to solve the problem)
Furthermore, You should deploy your Django project in the right folder. the differences between those logs are rational. root folder probably is under a read-only mode.
you have mentioned that you have to work on this project and it's been several days of headaches. My first question is why do you focus on server deployment rather than coding? You can easily run Postgre, WSGI, and Django without any headache ! ( developing environment)
and pgadmin isn't a necessary tool. Also, if it's the problem and you should use it just ignore it and pass it to the final stage which you have done your coding phases. ( seems you stuck in the first place without coding)

Django app hangs when attempting to connect to database via Apache

I am having difficulty troubleshooting this issue. I have a Django app running on an Ubuntu 14.04 server (with Apache 2.4 and mod_wsgi for Python 3.4). It connects to SQL Server via pymssql.
In development, the app works fine. I query the database, and the database returns the expected results.
In production (under the Apache user), however, the script hangs at the exact point that a database query is made. My browser (Chrome or Firefox) shows a spinning wheel that continues to spin as long as the browser window is open.
I have the following in my apache2.conf file:
ServerName localhost
# WSGIDaemonProcess application
WSGIPythonPath /home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
# WSGIProcessGroup application
WSGIScriptAlias / /home/production_code/school/school/wsgi.py
# Python virtualenv home
WSGIPythonHome /home/production_code/python3env
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf
And the following in my sites-enabled/000-default.conf file:
<VirtualHost *:80>
ServerAdmin *****#school.edu
ServerName localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static/ /home/production_code/school/static/
<Directory /home/production_code/school/>
Require all granted
</Directory>
<Directory /home/production_code/school/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /home/production_code/school/static>
Require all granted
</Directory>
</VirtualHost>
Does anyone have any idea what might be causing this or how I might troubleshoot this? The Apache error logs and access logs are not particularly helpful in this situation, since a response to the request is never rendered. Similarly, Django debugging is also not useful here.
Instead of:
# WSGIDaemonProcess application
WSGIPythonPath /home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
# WSGIProcessGroup application
use:
WSGIDaemonProcess application python-path=/home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
WSGIProcessGroup application
WSGIApplicationGroup %{GLOBAL}
A key part of this is the WSGIApplicationGroup directive, with it being set to %{GLOBAL}.
This is to get around faulty third party extension modules for Python that don't work in sub interpreters and which can fail with a dead lock or crash.
See:
https://code.google.com/p/modwsgi/wiki/ApplicationIssues#Python_Simplified_GIL_State_API
It is also recommend you go back to using daemon mode. It is generally not a good idea to use embedded mode.
http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html

Django EC2 deployment with wsgi and Apache

I have a micro instance on EC2 with Django installed there. I've also installed mod-WSGI, PostgreSQL, etc following several tutorials.
Finally, I pulled my project from bitbucket and started Apache on my EC2. Unfortunately, the only thing I have is the default Apache page and I've already spent a day and night reading and trying to figure out what am I doing wrong.
my_project is in /home/ubuntu dir. In it's folder I have wsgi.py file:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
This is my .conf file stored in /etc/apache2/httpd.conf:
Listen 80
NameVirtualHost *:80
WSGIPythonPath /home/ubuntu/my_project/
<VirtualHost *:80>
DocumentRoot /home/ubuntu/my_project
ServerName www.ec2-54-***-104-**.us-west-2.compute.amazonaws.com
ServerAlias ec2-54-***-104-**.us-west-2.compute.amazonaws.com
ErrorLog /home/ubuntu/my_project/apache/error_log
CustomLog /home/ubuntu/my_project/access_log_common
WSGIScriptAlias / /home/ubuntu/my_project/wsgi.py
Alias /static/ /home/ubuntu/my_project/static/
<Directory /home/ubuntu/my_project/static>
Require all granted
</Directory>
<Directory /home/ubuntu/my_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
When I'm trying to
/etc/init.d/apache2 stop
and instead:
python manage.py runserver 0.0.0.0:80
It works fine and I'm able to connect to my app from a browser with IP as URL.
It gives me understanding that the setting.py file is fine (am I right?) and the problem is or in wsgi.py or httpd.conf.
When I stop my development server and start Apache again, I get the default Apache page. Can you help me please to find what's wrong with my files?
Permissions for apache:
ubuntu#ip-172-**-**-69:~$ ls -ld my_project/
drwxr-xr-x 10 www-data www-data 4096 May 26 19:51 project/
You normally shouldn't use an IP address for ServerName, it has to be the hostname (FQDN) that the site is accessed as.
You are missing a WSGIDaemonProcess directive to correspond to your use of WSGIProcessGroup.
It is bad security practice to set DocumentRoot to be where your source code for your application is.
Finally, stuff under a users home directory is not usually accessible to the user that Apache runs as.
Suggest go back and review the mod_wsgi deployment documentation on the Django site again for a start.

Categories

Resources