django apache :WSGIPythonHome cannot occur within <VirtualHost> section - django

i have a server with ubuntu-server that run apache2 and where i have my django project. i'm having trouble deploying the app. this is my /etc/apache2/sites-available/000-default.conf:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName HIVE.localhost
ServerAdmin webmaster#localhost
DocumentRoot /home/leo/HIVE/src
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /static /home/leo/HIVE/src/static
<Directory /home/leo/HIVE/src/static>
Require all granted
</Directory>
Alias /media /home/leo/media
<Directory /home/leo/media>
Require all granted
</Directory>
<Directory /home/leo/HIVE/src/HIVE>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIPythonHome /home/leo/HIVE/HIVE_env
WSGIPythonPath /home/leo/HIVE/src
WSGIDaemonProcess HIVE python-home=/home/leo/HIVE/HIVE_env python-path=/home/leo/HIVE/src/HIVE
WSGIProcessGroup HIVE
WSGIScriptAlias / /home/leo/HIVE/src/HIVE/wsgi.py
</VirtualHost>
i want to use wsgi to deploy it. i have tried several solutions found on the internet but i couldn't manage to do it. sadly the docs didn't help at all, neither digital ocean tutorial on this. i' stuck with the command sudo apache2ctl configtest that gives me the error in the title. i have already changed ownership of the database and the folder where it is contained.
additional info:
inside my home directory i have two folders called HIVE and media. inside HIVE a virtualenv folder called HIVE_env and a src folder. inside src there is my project, inside HIVE_env there is my env ( containing libs and the rest)
i'm new to apache and deploying so this is a lot of new info for me. thank you for your attention.

Related

How to deploy multiple django apps on apache in Windows?

I want to deploy multiple django apps on apache on Windows but only know how to deploy one.
Overriding the localhost of the Wamp Server I can deploy the app without problem but I need to deploy more and don't know how. I've sehen virtual hosts and think are good but don't know how to configurate them. Anyone know how can I do this? Thanks in advance.
hosting severel django apps with Apache is possible using virtual hosts (vhosts)
important to care about:
during config of Apache I recommend to start apache from command line as "httpd.exe" as in XAMPP or WAMP you will not see some of the initial start-up error messages in error.log files.
you can only use 1 python version even in different virt.env for each vhost as apache module mod_wsgi compilation needs to fit to it and is loaded once at startup of apache
something like this in httpd.conf (you should have this already in place because of your running single app config):
LoadFile "c:/.../python/python38/python38.dll"
LoadModule wsgi_module "c:/..../mod_wsgi.cp38-win_amd64.pyd"
for those starting from scratch:
activate virt.env.
> pip install mod_wsgi
> mod_wsgi-express module-config
will give above output (LoadFile ....) that you need to copy to httpd.conf
how to set path to virt.env and app folders:
with 1 host you would point to your virt.env by setting WSGIPythonHome and WSGIPythonPath to point to your app folders in httpd.conf:
WSGIPythonHome "d:/..../django_project/env_folder"
WSGIPythonPath "d:/..../django_project/app_name"
but: you can not place WSGIPythonHome/WSGIPythonPath inside the VirtualHost declaration in httpd-vhosts.conf .... it will cause an error message
Solution: set paths in wsgi.py dynamically and remove WSGIPythonHome/WSGIPythonPath from apache *.conf:
wsgi.py:
# replacement for WSGIPythonHome "d:/..../django_project/env_folder"
# choose one:
sys.path.append('d:/.../env_folder/lib/site-packages') # add individual virt.environment packages at the end of sys.path; global env packages have prio
sys.path.insert(0,'d:/.../env_folder/lib/site-packages') # add individual virt.environment packages at the beginning of sys.path; indiv. virt.env packages have prio over global env
# replacement WSGIPythonPath "d:/..../django_project/app_name"
sys.path.append('d:/.../django_project/app_name') # add indiv. app folder to search path
here is example for apache conf:
(why the dummy host: there is a (strange or buggy) behavior of apache ... if none of the virtual host names match the request, then automatically apache will dispatch the request to the first vhost in the config - no matter which server name is defined ther. This can lead to confusion because the total wrong app is called and an error messages will most certainly pop-up from inside django, not indicating that the error is on the Apache conf level. A dummy host with a simple index.html and an error message can make this tranparent)
httpd-vhost.conf:
<VirtualHost *:80>
ServerName Dumme_Host
DocumentRoot "d:/WEBSPACES/Dummy_Host"
<Directory d:/WEBSPACES/Dummy_Host>
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName xxxx1
WSGIScriptAlias / "d:/.... /wsgi.py" application-group=app_name1
Alias /media/ d:/.../media/
Alias /static/ d:/.../static/
<Directory d:/.../app_name1>
Require all granted
</Directory>
<Directory d:/.../media>
Require all granted
</Directory>
<Directory d:/.../static>
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName xxxx2
WSGIScriptAlias / "d:/.... /wsgi.py" application-group=app_name2
Alias /media/ d:/.../media/
Alias /static/ d:/.../static/
<Directory d:/.../app_name2>
Require all granted
</Directory>
.....
</VirtualHost>

Errno 13 Permission denied: '/home/ubuntu/Demo/website/media/image_banner/download.jpg'

I built a Web site using the Django framework, one of which was to process images uploaded by the admin and save them in a folder for media/image_banner, but encountered an error ([Errno 13] Permission denied: '/home/ubuntu/Demo/website/media/image_banner/download.jpg'. I use python manage.py runserver and it works, but it doesn't work when deployed with Apache2.
this is port 80 file code:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /home/ubuntu/Demo/website
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /static /home/ubuntu/Demo/website/static
<Directory /home/ubuntu/Demo/website/static>
Require all granted
</Directory>
<Directory /home/ubuntu/Demo/website/website>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIPassAuthorization On
WSGIDaemonProcess website python-path=/home/ubuntu/Demo/website python-home=/home/ubuntu/Demo/demo_env
WSGIProcessGroup website
WSGIScriptAlias / /home/ubuntu/Demo/website/website/wsgi.py
This is caused for the lackings of permission. You should give proper read-write permission /home/ubuntu/Demo/website
sudo chmod 777 /home/ubuntu/Demo/website -R
This command will give all the permission (read, write, execute) to the website directory including all directories and files inside the website directory. But for production, you need to change the permission for the security issue.

Apache2 shows default page when I visit the site without 'www' on an android device, but works fine with 'www'

I deployed Django website: www.alahaberry.com on Linode Ubuntu 20.04 LTS. Whenever I try to access it with my computer, it works just fine whether I add www or not. Today my friend tried to check it out from his android device and the apache2 default page appeared. This only happened on android device (or maybe all mobile). I tried with BlueStacks too the same. Without www it shows default apache2 page. Please what could be wrong. I really need your help. Thanks advance.
I used letsencrypt to get SSL on it. My alahaberry-le-ssl.conf looks like this:
<IfModule mod_ssl.c>
<VirtualHost *:443>
# The ServerName directive sets the request scheme, hostname and port th> # the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName alahaberry.com
ServerAlias www.alahaberry.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /static /home/admin/alahaberry/static
<Directory /home/admin/alahaberry/static>
Require all granted
</Directory>
Alias /media /home/admin/alahaberry/media
<Directory /home/admin/alahaberry/media>
Require all granted
</Directory>
<Directory /home/admin/alahaberry/alahaberry>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/admin/alahaberry/alahaberry/wsgi.py
WSGIDaemonProcess alahaberry_app python-path=/home/admin/alahaberry pyth> WSGIProcessGroup alahaberry_app
SSLCertificateFile /etc/letsencrypt/live/www.alahaberry.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.alahaberry.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

Google Cloud Platform LAMP setup Laravel 5.4

Looking for help on server problem. I have followed the following steps to set up LAMP on the VM
https://cloud.google.com/community/tutorials/setting-up-lamp
I put my Laravel 5.4 website onto the VM, configure the .env for the following:
APP_URL=website_external_IP
But when I access the website by inserting the website_external_IP on my browser, it returns the following image.
Sorry for being noob of server problems but please let me know what else information should I provide for you to figure out the cause of it. Thank you!
1 EDIT:
In response to John Hanley's suggested site. I further made the following edits on the apache conf:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/laravel_project.conf
sudo vim /etc/apache2/sites-available/laravel_project.conf
and there vim the laravel_project.conf as followings
NameVirtualHost *:8080
Listen 8080
<VirtualHost *:8080>
ServerAdmin admin#example.com
ServerName laravel.dev
ServerAlias www.laravel.dev
DocumentRoot /home/user/projects/laravel_project/public
<Directory /home/user/projects/laravel_project/public/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
then
sudo vim /etc/hosts
and add the following line:
127.0.0.1 laravel.dev
then disabled the original conf settings and enable for my site:
sudo a2dissite 000-default.conf
sudo a2ensite laravel_project.conf
Without having the full understanding of the new edits procedures, accessing the page with IP returns the following error:
You don't have permission to access / on this server.
Your Apache web server has directory browsing enabled. Also you do not have a default page present (or enabled) therefore you server is serving up the directory listing.
/etc/apache2/apache2.conf
Remove the word Indexes from this part of your configuration:
<Directory /var/www/>
Options Indexes FollowSymLinks
...
</Directory>

Apache WSGI vs. Django Development Server

I recently decided to deploy my Django project using Apache and mod_WSGI. So, whereas my dev server runs on localhost:8000, my "Apache WSGI" server runs on localhost.
Everything looked good until I tried to POST to my Django REST API. For whatever reason, I can post perfectly fine to the Django dev server, but I cannot post to the Apache WSGI server.
As a matter of fact, I do not even see that a POST attempt was logged when I look at my Apache logs--I can see url errors there (when I purposefully try to go to a page that doesn't exist for example) but no failed POST attempts. What can be causing this problem and how can I troubleshoot it?
Could it be that the Django REST Framework is incompatible with WSGI? I am puzzled why everything else: admin page, logins, navigation, update views, forms, etc, work perfectly fine but this one thing is not even showing up in the logs.
EDIT: This is my virtual host file:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /static /home/pi/myapp/myapp2/myapp/static
<Directory /home/pi/myapp/myapp2/myapp/static>
Require all granted
</Directory>
<Directory /home/pi/myapp/myapp2/myapp/myapp>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess myapp python-path=/home/pi/myapp/myapp2/myapp python-home=/home/pi/WebApp/p3
WSGIProcessGroup myapp
WSGIScriptAlias / /home/pi/myapp/myapp2/myapp/myapp/wsgi.py
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet