Please help me to set my home PC as a server (Windows 10 + Python + Django + Postgre SQL + Anaconda)
There are several instructions on how to set the server on the Internet, and surprisingly, many instructions differ from each other. I have absolutely no experience in setting a server. Probably, I’m making a stupid mistake somewhere that I can’t identify for 3 days. I am lost.
I believe that most useful instructions are these (they are complete and new):
https://www.codementor.io/aswinmurugesh/deploying-a-django-application-in-windows-with-apache-and-mod_wsgi-uhl2xq09e
https://ostrokach.gitlab.io/post/apache-django-anaconda/
I followed the instructions and successfully downloaded the necessary modules, installed Wamp, made the changes as displayed in the guide and started it. What I see now: the Wamp icon glows green. When I load a localhost, the page loads endlessly but does not load.
Wamp error log shows following: [Fri Oct 11 14:50:33.823752 2019]
[core:notice] [pid 1364:tid 808] AH00094: Command line:
'c:\wamp64\bin\apache\apache2.4.39\bin\httpd.exe -d
C:/wamp64/bin/apache/apache2.4.39' [Fri Oct 11 14:50:33.840681 2019]
[mpm_winnt:notice] [pid 1364:tid 808] AH00418: Parent: Created child
process 15052 [Fri Oct 11 14:50:34.981629 2019] [mpm_winnt:notice]
[pid 15052:tid 800] AH00354: Child: Starting 64 worker threads.
Project name: turiumasina
path D:/Users/PycharmProjects/turiumasina/
path to wsgi:D:/Users/PycharmProjects/turiumasina/turiumasina/wsgi_windows.py (I renamed wsgi to wsgi_windows, settings file is in turiumasina/turiumasina/)
I copied the output generated by the mod_wsgi-express command and pasted it at the end of C:\wamp64\bin\apache\apache\conf\httpd.conf
my httpd-vhosts.conf
ServerName localhost
WSGIPassAuthorization On
ErrorLog "logs/turiumasina.error.log"
CustomLog "logs/turiumasina.access.log" combined
WSGIScriptAlias / "D:/Users/PycharmProjects/turiumasina/turiumasina/wsgi_windows.py"
<Directory "D:/Users/PycharmProjects/turiumasina">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
Alias /static "D:/Users/PycharmProjects/turiumasina/static"
<Directory "D:/Users/PycharmProjects/turiumasina/static">
Require all granted
</Directory>
wsgi_windows.py
import os
import sys
import site
from django.core.wsgi import get_wsgi_application
site.addsitedir("C:/users/.conda/envs/turiumasina/Lib/site-packages")
sys.path.append('D:/Users/PycharmProjects/turiumasina')
sys.path.append('D:/Users/PycharmProjects/turiumasina/turiumasina')
os.environ['DJANGO_SETTINGS_MODULE'] = 'turiumasina.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "turiumasina.settings")
application = get_wsgi_application()
I generated the "static" folder, wrote a line in the settings.py:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
If I enter python manage.py runserver via pycharm, I can see my site through a browser (http://127.0.0.1:8000/). the Wamp icon glows green. When I load a localhost, the page loads endlessly but does not load.
I would like my site to be visible not only to me, but also to Internet users by entering my external IP.
If you need, I can send you the whole project and project settings
I would get rid of WAMP altogether, I don't see why you need it if you're not using PHP or MySql.
If you just need to play/debug the Django app over the local network just run ./manage.py runserver 0.0.0.0:8000 (or any other port). Make sure your Windows firewall is allowing incoming connections to the port.
The connect to it you need the computer's actual IP not '0.0.0.0:8000'. Type 'ipconfig' at the command prompt to see what your actual IP is an then connect to Your_actual_IP:8000
If you need something more solid than the build-in Django server (to run in non-debug mode) then you can install Waitress (https://docs.pylonsproject.org/projects/waitress/en/latest/).
Related
I'm with a problem while deploying Django in my VPS with Centos 7.3 and WHM. It seems to work, except for a socket problem with mod_wsgi.
[Sun Jun 25 00:37:03.254774 2017] [wsgi:error] [pid 29756] (13)Permission denied: [client 66.249.83.220:35523] mod_wsgi (pid=29756): Unable to connect to WSGI daemon process 'brunamaiahair.com.br' on '/var/run/apache2/wsgi.721.27.1.sock' as user with uid=1004.
I read to insert WSGISocketPrefix as a directive, so I edited httpd.conf and put:
WSGISocketPrefix /var/run/apache2/wsgi
But I'm receiving the same error. Here is the log with the modified httpd.conf after an Apache restart:
[Sat Jun 24 21:10:56.084269 2017] [mpm_prefork:notice] [pid 721] AH00163: Apache/2.4.25 (cPanel) OpenSSL/1.0.2k mod_bwlimited/1.4 mod_wsgi/4.5.7 Python/2.7 configured -- resuming normal operations
Here is my VirtualHost configuration:
WSGIDaemonProcess brunamaiahair.com.br socket-user=#1004 python-path=/home/bmhair/public_html/django/framework:/home/bmhair/public_html/django/denv/lib/python2.7/site-packages
WSGIProcessGroup brunamaiahair.com.br
WSGIScriptAlias / /home/bmhair/public_html/django/framework/framework/wsgi.py
See socket-user option in:
http://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html
Recent CPanel installations seem to use PrivilegesMode set to SECURE so you will need to declare who should own the socket. It should be the user/uid Apache changes to when handling requests for you, rather than the default of the Apache user.
If for example the user which CPanel is setup to run your request as under Apache is bmhair, you need to add to the WSGIDaemonProcess directive in the Apache configuration the option:
socket-user=bmhair
After a restart of Apache, check in the directory where the socket file is placed, eg., /var/run/apache2 and check that socket file is owned by user bmhair.
Note that this will require that the directory /var/run/apache2 provides access to other users, ie., not just root or the user Apache runs your code as. If that is not the case, then use WSGISocketPrefix to move the socket file to another directory which is accessible to the user bmhair. Generally you should not need to even override WSGISocketPrefix as the default location used is fine. If you had set it explicitly for some reason, and didn't allow the default to be used, that could also be part of the problem.
The path /var/run/apache2 bmhair do not have access to that folder.Only root and approved users have.
So we need to show apache a path for wsgi socket.
If it is VPS:
WSGISocketPrefix /var/run/wsgi
If you are in a shared hosting:
WSGISocketPrefix ../../var/run/wsgi
Or
WSGISocketPrefix /home/bmhair/var/run/wsgi
As I can see you are in bmhair user so for you 2nd one will work.
I have an Amazon EC2 instance running Ubuntu and I'm trying to get it to display my Django site. The dev server works (i.e. if I run the dev server on the instance and go to http://ec2-XX-XXX-XX-XX.us-west-2.compute.amazonaws.com/:8000, my site works fine) but I can't get it to run properly via apache.
I've read about a thousand tutorials and so far none have been successful. Here is my most recent attempt - my Django site is stored in /home/ubuntu/Amazon/repo/my_site/.
I've tried creating a virtual host file in /etc/apache2/sites-available/ec2.conf with the following:
<VirtualHost *:80>
ServerName ec2-XX-XXX-XX-XX.us-west-2.compute.amazonaws.com
ServerAlias *.compute.amazonaws.com
WSGIScriptAlias / /home/ubuntu/Amazon/repo/my_site/my_site/apache/apache.wsgi
<Directory />
Require all granted
</Directory>
</VirtualHost>
After that, I ran sudo a2ensite ec2 to add the conf file to sites-enabled but going to ec2-XX-XXX-XX-XX.us-west-2.compute.amazonaws.com in my browser still returns to the default page.
Any guidance would be great, I'm sure I'm missing several steps, but I'm completely lost at the moment. There are a lot of things I don't understand at the moment such as:
Do I need a /var/www/html/ directory somewhere? Or does Django handle that?
Do I need to use virtual hosts at all? Or is that only for subdomains?
Do I need to register a domain name prior to getting it working? All the examples I see use something like "example.com" where I have "ec2-XX-XXX-XX-XX.us-west-2.compute.amazonaws.com". I have the domain name registered but I haven't pointed it at my amazon instance yet - I was hoping to first get it working.
Edit: Apache error.log output:
[Tue Jul 08 16:07:47.983174 2014] [mpm_event:notice] [pid 5054:tid 140062636054400] AH00491: caught SIGTERM, shutting down
[Tue Jul 08 16:07:49.046666 2014] [mpm_event:notice] [pid 5211:tid 139882510350208] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Tue Jul 08 16:07:49.046735 2014] [core:notice] [pid 5211:tid 139882510350208] AH00094: Command line: '/usr/sbin/apache2'
I am a django beginner.
I receive the following message in /var/log/apache2/error.log (apache is set to debug level)
[Thu Aug 30 16:15:11 2012] [info] mod_wsgi (pid=5616): Initializing Python.
[Thu Aug 30 16:15:11 2012] [info] mod_wsgi (pid=5616): Attach interpreter ''.
[Thu Aug 30 16:15:15 2012] [error] [client 172.24.113.130] File does not exist: /var/www/netvistra3
[Thu Aug 30 16:15:19 2012] [error] [client 172.24.113.130] File does not exist: /var/www/netvistra3
My wsgi file is located in /usr/local/django/netvistra3/apache/netvistra3.wsgi
The admin site only seems to work with django's embedded webserver.
Snippet from apache2 virtual directory site:
Alias /static/admin /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin
WSGIDaemonProcess netvistra3 user=pbensel group=staff threads=5
WSGIScriptAlias /netvistra3 /usr/local/django/netvistra/apache/netvistra3.wsgi
<Directory /usr/local/django/netvistra3>
WSGIScriptReloading On
WSGIProcessGroup netvistra3
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
I greatly appreciate any help
Django has a bit of magic when running under it's own server that helps with this. When deploying on Apache, etc you are you're responsible for setting up Apache, or whichever media server you're using, to serve the admin files. The Django docs are pretty good on the topic. Please see the following link to the Django docs.
It's a pretty common problem. So, don't feel bad. Just Google if you need any more specific help for "deploy django admin"
Btw, I use the "collectstatic" approach for my projects and server them from an s3 bucket. It works great and seems to be a pretty common practice. At least with Django 1.3.
As David said, you have to take the right steps to set up static files. You have:
Alias /static/admin /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin
but are missing corresponding:
<Directory /usr/lib/python2.7/dist-packages/django/contrib/admin/static>
Order allow, deny
Allow from all
</Directory>
Don't have that and you will find static files fail with forbidden error.
I would though also check whether you have set up settings properly to say under what URL the static media is available.
If the static files are not served properly, you will be missing all the stlying for the admin pages, which could be considered as not working properly.
Also look into collectstatic as suggested as not always a good idea to refer to static media out of the Python installation like you are doing.
I think my apache2 server is close to being set up but it's still returning a 500 error. Here is my .wsgi file:
import os, sys
#path to directory of the .wsgi file ('apache/')
wsgi_dir = os.path.abspath(os.path.dirname(__file__))
#path to project root directory (parent of 'apache/')
project_dir = os.path.dirname(wsgi_dir)
sys.path.append(project_dir)
project_settings = os.path.join(project_dir, 'settings')
os.environ['DJANGO_SETTINGS_MODULE'] = 'ecomstore.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Here is my virtualhost file:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin admin#site.com
ServerName www.site.com
ServerAlias site.com
Alias /static /home/ecomstore/static
DocumentRoot /home/ecomstore
WSGIScriptAlias / /home/ecomstore/apache/ecomstore.wsgi
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
Here is my server restart:
sudo /etc/init.d/apache2 restart
* Restarting web server apache2
[Sun Apr 08 02:47:31 2012] [warn] module wsgi_module is already loaded, skipping
... waiting ..........[Sun Apr 08 02:47:42 2012] [warn] module wsgi_module is already loaded, skipping
...done.
However, even though I get no errors with mod-wsgi configuration on the restart, I still get the 500 Internal Server error mentioned before. Is there anything I'm missing?
You would need to look at the Apache error logs from when you made the request and not when you did the restart.
One source of problems is that sys.path does not include the parent directory of the project directory which would be necessary because of what you set DJANGO_SETTINGS_MODULE to. Go read about this requirement in:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
and fix it.
Beyond that, you need to work out whether it is an Apache 500 page or a Django one. If it is a Django one, then turn on DEBUG in Django settings file and restart Apache so you get a full error shown in browser. Turn off DEBUG when you have resolved.
I am just starting to fiddle around with django. First I made a small app on my windows machine and verified it worked fine
Then, I zipped the entire project, and opened the zip on a linux machine.
The linux machine was installed with mod_wsgi and django 1.1.1, of course.
I created the following dirs:
/usr/local/bin/ROOT - contains only one file, django.wsgi
/usr/local/bin/ROOT/myapp - root dir of django app
Per the instructions here, I added to httpd.conf:
<VirtualHost *:80>
ServerName server
ServerAlias server
ServerAdmin webmaster#example.com
WSGIScriptAlias /myapp /usr/local/bin/ROOT/django.wsgi
<Directory /usr/local/bin/ROOT/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Now per the instructions here I put in /usr/local/bin/ROOT/django.wsgi:
import os
import sys
path = '/usr/local/bin/ROOT'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
If it is of any importance: there is nothing set on PYTHOHPATH before the application starts.
After that I did a graceful restart to apache, and went to server/myapp. I got a 500 error.
Lookin in the log I see:
[Sun Dec 05 12:24:17 2010] [error] [client XXXX] ImproperlyConfigured: Error importing middleware django.middleware.csrf: "No module named csrf"
What am I doing wrong? all other threads I found about this always either end up with a conclusion that it's an old version of django (but mine's 1.1.1) or that there are several apps running, but I have only one...
Help?
"django.middleware.csrf" is the package in Django 1.2.x
For Django 1.1.x CSRF settings read the appropriate docs Here
The package in 1.1.x was "django.contrib.csrf.middleware.CsrfMiddleware"
1.1.1 is an old version of Django (the current version is 1.2.3), and that's almost certainly the cause of your problem. There is no django.middleware.csrf in 1.1.1.