Deploy django app on ubuntu apache2 - django

I want to run my Django app created in virtualenv on ubuntu with python3. Folder structure in virtualenv folder:
-bin
-include
-lib
-myapp
-share
pip-selfcheck.json
The myapp folder contains my application with apache folder configured as specified in this tutorial: https://www.sitepoint.com/deploying-a-django-app-with-mod_wsgi-on-ubuntu-14-04/
I have all apps installed I need in my virtualenv, after 'sudo service apache2 restart' I see only Apache2 Ubuntu Default Page.
File /etc/apache2/sites-enabled/000-default.conf is like in the tutorial:
<VirtualHost *:80>
WSGIScriptAlias / /home/myuser/mysite/apache/wsgi.py
<Directory "/home/myuser/mysite/apache/">
Require all granted
</Directory>
</VirtualHost>
Of course with correct paths pointing to my project location in 'venv' folder.
No idea where to move on, what to check, thanks for suggestions.
EDIT:
I realy dont get this, I edited the mentioned file, had a try, nothing happened so I edited it back and after restart it it worked.

The official Django documentation on using mod_wsgi can be found at:
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/
It may be better to consult that for how to set things up specifically for Django, you are missing various required configuration elements.
As to why you are still getting the default page, it could be because you added that configuration as an extra thing to the end of the default sites file, rather than inserting the contents of the VirtualHost you have into the existing VirtualHost in that file. If you added it after, it will be ignored as it will still use the existing VirtualHost as it comes first and you haven't set up correctly named based hosting.
Also be aware that where other people say you should use something else, every solution will have a learning curve if this is all new. No solution is simple if you want to integrate into your existing host system. So jumping around looking at different solutions can be a great waste of time. Select one which you think you like and stick with it. The idea that one is superior to others is in general nonsense as their performance is similar.
I would actually suggest you skip even trying to integrate it into your host system to begin with if you are just playing. Use a WSGI server that you can run from the command line, even if it is just the bundled development server with Django. Options which are simple to run from the command line are:
mod_wsgi-express
gunicorn

So after editing the file, restarting apache, editing the file back it works (/etc/apache2/sites-enabled/000-default.conf). Really dont get it, several restarts yesterday did not help...

I have Ubuntu 20.04 and Apache2. I followed the mod_wsgi installation instructions linked to here but forgot to enable the module in /etc/apache2/apache2.conf (or to include a file from apache2.conf that does so). I added that and then the WSGIScriptAlias / /home/myuser/mysite/apache/wsgi.py directive in my 000-default.conf did not cause an error when I restarted the server with systemctl restart apache2

Related

Need some help getting Apache 2.2.5 to register environmental variables (CentOS 6.10)

I've tried every solution I've found in the last two days on this and other sites. None work.
I'm trying to set two OS level environment variables/commands for the apache user account on centos 6.10. Apache version is 2.2.5. See final paragraph for why I'm having to do this. Alternative solutions are also welcome.
Specifically these commands/variables:
. /opt/rh/python27/enable
export set CLOUDSDK_PYTHON=/opt/rh/python27/root/usr/bin/python2
I have:
Put them in /etc/sysconfig/httpd and /etc/init.d/httpd
Tried various version of these commands
Switched apache to use bash, created .bashrc in /var/www/, gave apache ownership and set permissions correctly and put them in there. I realize this is bad practice, its a test server and I was out of other ideas.
I can't get them to persist beyond a single exec statement in php (where I still have to call both listed commands) or a single su -s /bin/bash apache -c "[command]" execution.
Does anyone know how to get these to persist for the apache user?
Why am I doing this?
I am doing this because Google Cloud Cloud SDK has PHP code that doesn't work so I'm left with either using CURL or passing exec statements, both of which require this environmental variable nonsense for the executing user. To run Google Cloud SDK commands you have to have version 2.7 of python installed, which you can't upgrade to on centos 6.10 without breaking the OS, unless you install it concurrently somewhere else per: http://jhurani.com/linux/2018/07/30/GCSDK-on-centos6.html doing that means I have to set whatever user account is executing code to use the new version of python. Which is apache in this case. Works fine on my normal user account, but I can't get the apache account to respect these changes beyond a single execution, even though everything I read says that it should.
Any help would be appreciated. Thanks for you time and any insight you'd care to share.
Could you try to export the variables in /etc/sysconfig/httpd
export mydocroot=/var/www/html
Then configure the Virtual Host
<VirtualHost *:80>
DocumentRoot ${mydocroot}
</VirtualHost>
Finally restart the service
systemctl restart httpd
In addition, I found that you can also do it through the .httaccess file with SetEnv assuming that it's enabled on the website.
SetEnv SPECIAL_PATH /foo/bin
That's all you need to add the environment variable into the .htaccess file

Serve Django from root domain with Apache, and also use /var/www/html

I'm new to Django and Apache, so apologies if my terminology is a bit off.
I have a Django app that I'm serving with Apache using mod_wsgi. I used this guide, and just switched to Apache from the Django dev server. In my 000-default.conf file I have this line
WSGIScriptAlias / /home/ubuntu/Projects/myapp/wsgi.py
Everything works fine and the homepage of my app is example.com. However, I find now that there are images (a few tilesets) that I was previously accessing at example.com/tiles which are now not accessible, because they are actually stored at /var/www/html/tiles.
I understand why the paths aren't working, but I'm wondering if there's a way I can keep running the django site from example.com while also serving the tiles from a different directory.
I think if you add a preceding configuration directive:
Alias /tiles /var/www/html/tiles
... it should fix it.
Please note that order of Apache configuration directives may matter. More information about Apache and Alias.

Running django with mod_python on a shared apache server

I'm in a university and I'm provided with a public_html folder where I can put my CGI scripts. For eg. when I put PHP scripts and visit them from my browser, it works correctly and the PHP is properly interpreted.
I wish to run Django apps in this environment and I know that the university runs an apache server and had mod_php and mod_python installed, although I think I'm not allowed to modify the httpd.conf etc.
All the tutorials that I've read about Django on mod_python ask me to modify the httpd.conf, is there any way I can get my Django site running by making non-sudo changes only?
You can try to put your directives in a .htaccess file in your directory. Dependent on the server config, it might work.

Django site on Webfaction - how the flow of information works, and integrating with Pycharm remote dev

I have been learning Django in the development mode for a two months and I am up to speed with most basic aspects of python + django now. However, I was using the built-in runserver till this time
Now, I have got a Webfaction hosting account and wanted to know the following
1) Webfaction sets up the project with a certain Django version (say 1.6.4) and Python version (say 2.7) initially
The project directory (for say project MYAPP) is /<>/webapps/MYAPP
When the site is running in production mode, how does the apache server know which Python version, and which site-packages versions to use with the MYAPP source code to render the site?
I can see that the MYAPP folder has a lib/python2.7 folder, however when I am connected to SSH terminal, and do a "which python", i see :
which python
/usr/local/bin/python
so, do I take that this is the Python executable that is being used for rendering the website instead of the one in webapps/MYSITE/lib/python2.7 folder? How does the information/data flow about which programs to use during rendering the site with apache mod_wsgi work?
2) I was using a virtualenv in the development mode during testing. How do I use this on webfaction in production mode?
3) I am using Pycharm IDE. It worked well for the development mode. I can see that it has a remote interpreter configuration and a Deployment setting/option.
The python path that the remote interpreter settings tool detects automatically is the python executable at /usr/local/bin/python
Is this fine, or should I be pointing it to the more local python2.7 in the webapps/lib folder?
Thanks a lot of the answers and pls let me know if you need any supplemental info
Note to the OP: This should really be three separate questions.
1) For WebFaction, your Django app will use the Python version (and libraries, etc.) defined in:
~/webapps/<appname>/apache2/conf/httpd.conf
Specifically, you'll want to look at what is defined for WSGIPythonPath (which should mostly match up with WSGIDaemonProcess unless you modify the config and are doing something strange).
Note that which python just tells you what the default python is in your shell. This has nothing to do with the config file for the webapp.
2) You may want to expand on this as to exactly what your use case is and why the default Django webapp created by WebFaction doesn't fit your needs. But the short answer is:
Create a virtualenv on your WebFaction account.
Install Django, etc. into the virtualenv.
Edit the httpd.conf file I mentioned above to use your virtualenv instead.
I've done this with both a Django webapp made via the WebFaction control panel and via a custom mod_wsgi webapp. So it does work. Just make sure to use the right Python version when making your virtualenv.
3) I don't use PyCharm so I can't answer this (one reason why this question should be split up).

What are the 'correct' permission setting for django site running on apache2 server

I am very new to apache and django, so execuse me if this question is simple. I am trying to deploy an existing site to an apache server. For the time being, the site is still in development so I am only deploying it as a virtualhost on my local machine.
I am using Django's WSGI module in the deployment. In my site's config file, I have the following aliases:
Alias /media/ /home/tester/Desktop/siteRootDir/media
Alias /content/ /home/tester/Desktop/siteRootDir/content
WSGIScriptAlias /c /home/tester/Desktop/siteRootDir/deploy/site.wsgi
When I run apache and go to localhost/c I was getting the (13)PermissionDenied error in the apache log. To get around that error, I (admitedly stupidly) ran
chmod -R 777 /home/tester/Desktop/siteRootDir
I know that is not the way to deal with the issue, but I just wanted the site to work so I can continue its development.
So my question is, what are the correct permission settings to the siteRootDir directory and its sub-directories such that the site will run and I do not expose unnecessary files in the directory.
Also, I realize that this is not an ideal set up and I will likely run into problems when I deploy the site in production. Can anyone please suggest a better organizational approach to this?
Thanks!
The tightest permissions possible would be 0600 for files and 0700 for dir's and as a owner the user owning the apache processes. This user differs per OS and flavor (e.g. for OSX it's www, for Debian/Ubuntu it's www-data).
This would probably too tight for a development server. At least would you like to be able to modify all your files through your IDE of text editor, so either you should add ACLs for yourself (i.e. the user that edits the Django files, templates and static files).
Also, in a production server you want the apache user to be able to write to directories that hold web uploaded content. That would be somewhere in your static files section (or on a different dedicated static files server).