I am following the tutorial for Deploying a Django Application on AWS.
Source I am using http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html
I get the following error:
[Tue Feb 23 21:05:22.883666 2016] [core:error] [pid 3394] (13)Permission denied: [client 172.31.46.155:37871] AH00035: access to / denied (filesystem path '/opt/python/current/app/mysite/wsgi.py') because search permissions are missing on a component of the path
My web page shows:
Forbidden
You don't have permission to access / on this server.
Here is my file path:
virtualenvFolder
- requirements.txt
mysite
-db.sqlite3.db
-manage.py
mysite
-__init__.py
-__init__.pyc
-settings.py
-settings.pyc
-urls.py
-urls.pyc
-wsgi.py
-wsgi.pyc
.ebextensions
-django.config
.elasticbeanstalk
-config.yml
requirements.txt:
Django==1.9.2
MySQL-python==1.2.5
django.config:
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: mysite/wsgi.py
config.yml:
branch-defaults:
default:
environment: django-env
group_suffix: null
global:
application_name: mysite
default_ec2_keyname: my-keypair
default_platform: python2.7
default_region: us-west-2
profile: eb-cli
sc: null
Note: I did not write or change config.yml it was auto generated.
This has nothing to do with Django and everything to do with Apache and file permissions. It looks like you are storing your Django application in an directory that the Apache process does not have permission to access.
Please see this question for a solution.
Related
I have developed a python (python 3.6 32bit) flask application and I need this to be deployed in a windows server with apache24 32bit.
I referred steps in https://medium.com/#madumalt/flask-app-deployment-in-windows-apache-server-mod-wsgi-82e1cfeeb2ed
When I try to launch the httpd.exe in apache24 am getting the below error
[Sun Jun 21 20:36:15.112840 2020] [mpm_winnt:notice] [pid 20600:tid 476] AH00455: Apache/2.4.43 (Win32) mod_wsgi/4.7.1 Python/3.6 configured -- resuming normal operations
[Sun Jun 21 20:36:15.112840 2020] [mpm_winnt:notice] [pid 20600:tid 476] AH00456: Apache Lounge VS16 Server built: Apr 21 2020 16:02:41
[Sun Jun 21 20:36:15.112840 2020] [core:notice] [pid 20600:tid 476] AH00094: Command line: 'httpd.exe -d C:/Apache24'
[Sun Jun 21 20:36:15.123841 2020] [mpm_winnt:notice] [pid 20600:tid 476] AH00418: Parent: Created child process 2064
Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00002dfc (most recent call first):
[Sun Jun 21 20:36:21.808509 2020] [mpm_winnt:crit] [pid 20600:tid 476] AH00419: master_main: create child process failed. Exiting.
Please find the SET configurations below,
OS=Windows_NT
Path=C:\Python36-32\Scripts\;C:\Python36-32\;C:\Program Files\Common Files\Micro
soft Shared\Microsoft Online Services;C:\Program Files (x86)\Common Files\Micros
oft Shared\Microsoft Online Services;C:\ProgramData\Oracle\Java\javapath;C:\Wind
ows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowe
rShell\v1.0\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\
PYTHONHOME=C:\Python36-32\
PYTHONPATH=C:\Python36-32\Scripts\
#1 check if you have an OLD python installation and remove it properly (including old environment vars, path ..)
#2 i would recommend you, if possible, upgrading your python installation to the last one 3.8.x +
#3 your problem is very common: your environment variables are NOT correctly set:
go to Advanced tab under System Properties and click Environment Variables
Under System Variables create those variables:
APACHE_HOME = C:\wamp\bin\apache\apache2.4.23 (i'm using WAMPSERVER)
MOD_WSGI_APACHE_ROOTDIR = %APACHE_HOME% (since you are using mod_wsgi, check official doc on pypi)
PYTHON_HOME = C:\Python37 (it depends on your python installation)
go to User variables and add/append variables to PATH like so:
PATH = %APACHE_HOME%\bin;%MOD_WSGI_APACHE_ROOTDIR%;%PYTHON_HOME%;%PYTHON_HOME%\Scripts;
#4 open new console and check your python installation:
python --version
#5 create a simple flask app to make sure everything is working as expected
py -m flask run
#6 to deploy the app on Apache server, have look at this flask doc and the official mod_wsgi doc
you have to install mod_wsgi GLOBALLY, meaning you have to deactivate first your virtual environment of your current active application.
(venv) C:\myapps\flask\helloflask>deactivate (i'm using venv the standard and default python virtual environment py -m venv venv)
C:\myapps\flask\helloflask>pip install mod_wsgi
C:\myapps\flask\helloflask>pip list
#7 configure mod_wsgi in Apache Server
check if mod_wsgi is correctly installed and configured
C:\myapps\flask\helloflask>mod_wsgi-express --help
Usage: mod_wsgi-express command [params]
Commands:
module-config
module-location
mod_wsgi-express: error: Invalid command was specified.
run this command:
mod_wsgi-express module-config
the output of the command should be like the following (depending on your system and python installation):
WSGIPythonHome "c:/python37"
LoadFile "c:/python37/python37.dll"
loadmodule wsgi_module "c:/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win32.pyd"
Copy the output of the above command and paste it in C:\wamp\bin\apache\apache2.4.23\conf. To make things consistent, locate the section where Modules are listed and add it at the very end of the list.
#8 create wsgi.py under the root of your project and paste the code (it's self-explanatory)
import os
import sys
# activate virtualenv
PROJECT = "helloflask"
# i'm using py -m venv venv
# #see: https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
# #see: https://stackoverflow.com/questions/25020451/no-activate-this-py-file-in-venv-pyvenv
activate_this = os.path.join('C:/myapps/flask', PROJECT, 'venv/Scripts/activate_this.py')
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
BASE_DIR = os.path.join(os.path.dirname(__file__))
if BASE_DIR not in sys.path:
sys.path.append(BASE_DIR)
from helloflask import create_app
application = create_app()
#9 Configure a Virtual Host for the Flask App
<VirtualHost *:80>
ServerName helloflask.local
DocumentRoot "C:/myapps/flask/helloflask"
WSGIScriptAlias / "C:/myapps/flask/helloflask/wsgi.py"
<Directory "C:/myapps/flask/helloflask">
Require all granted
</Directory>
# app = Flask(
# __name__,
# static_url_path='/public/static',
# static_folder='static'
# )
# Alias /public/static "C:/myapps/flask/helloflask/public/static"
# <Directory "C:/myapps/flask/helloflask/public/static">
# Require all granted
# </Directory>
ErrorLog "C:/wamp/logs/helloflask.error.log"
CustomLog "C:/wamp/logs/helloflask.access.log" common
</VirtualHost>
#10 check your Apache configuration
httpd -t
if it's OK, restart your Apache server
Where should the DJANGO_SETTINGS_MODULE environment variable be set?
Multiple possible locations:
In a config file (.ebextensions) as follows:
option_settings: aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: "app.settings"
In the wsgi.py file before application is loaded
In manage.py (I also see this on different Django projects on the web)
If I don't specify it in wsgi.py, it looks like it can't be found at all. Therefore, I wonder if the environment variables set in .ebextensions are set before wsgi.py is loaded.
Any idea?
Introduce your wsgi.py in a config file in .ebextensions
django_aws_eb.config:
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: "my_site/wsgi.py"
Specifly your DJANGO_SETTING_MODULE in your `wsgi.py' file
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_site.settings")
application = get_wsgi_application()
aws beanstalk first looks for configs in .ebextension to then locate wsgi.py to then locate settings.
more info: Configure Your Django Application for Elastic Beanstalk
I've been doing Flask microblog tutorial by Miguel Grinberg and have got stuck on trying to deploy to my linux VPS.(http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux-even-on-the-raspberry-pi)
I'm getting a 500 internal server error produced by apache, not flask, and I can't figure it out. It works when running it using the interpreter, but I can't launch it with apache. I've read through so many google searches and SO questions and I'm lost. I'm relatively new to linux/python/flask but I'm willing to learn if someone can point me in the right direction.
Setup:
I'm running a fresh CentOS 6.6 install with Python 2.6.6 and Apache 2.2.15. I'm running it off sqlite. I'm using these flask modules if you're interested: http://pastebin.com/bPnH83bs
Basic App Structure: (left out things for brevity)
Located in: /home/apps/portal
I have the whole directory chown'd to: chown -R apps:apache
User 'apps' is a member of the apache group
flask\ (virtualenv)
app\
static\
templates\
__init__.py
models.py
views.py
forms.py
decorators.py
db_repository\
search.db\
tmp\
app.db
config.py
run.py
runp.py
runp-sqlite.fcgi
Apache conf file setup:
FcgidIPCDir /tmp
AddHandler fcgid-script .fcgi
<VirtualHost *:80>
DocumentRoot /home/apps/portal/app/static
Alias /static /home/apps/portal/app/static
ScriptAlias / /home/apps/portal/runp-sqlite.fcgi/
ErrorLog /var/log/httpd/error_log
CustomLog /var/log/httpd/access_log combined
</VirtualHost>
runp-sqlite.fcgi Contents:
#!flask/bin/python
from flipflop import WSGIServer
from app import app
if __name__ == '__main__':
WSGIServer(app).run()
Error from apache logs when trying to access page and getting 500 error:
[Mon Dec 15 22:21:44 2014] [warn] [client *.*.*.*] mod_fcgid: read data timeout in 40 seconds
[Mon Dec 15 22:21:44 2014] [error] [client *.*.*.*] Premature end of script headers: runp-sqlite.fcgi
Error when I run "runp-sqlite.fcgi" from the console:
[root#**** portal]# sudo -u apache ./runp-sqlite.fcgi
Traceback (most recent call last):
File "./runp-sqlite.fcgi", line 6, in <module>
WSGIServer(app).run()
File "/home/apps/portal/flask/lib/python2.6/site-packages/flipflop.py", line 938, in run
sock.getpeername()
socket.error: [Errno 88] Socket operation on non-socket
Things I've checked:
I've disabled SELinux as it was causing a different problem, didn't fix this issue.
Checked that folders were chown'd to the correct user/group.
Checked that '/etc/httpd/conf/httpd.conf' and '/etc/sysconfig/httpd' PIDFILE locations are correct
Checked that iptables is accepting traffic to ports 80 and 5000(for testing). If I remove the apache configuration I've added I am successfully serving from /var/www/html
Lots and lots of googling and playing around
Sorry for the wall of text, I just don't know what you'll need to see. If anyone can help with this I'll be really greatful. If it's something dumb, I apologise. :)
Update 1:
Changed runp-sqlite.fcgi to call virtualenv:
#!flask/bin/python
activate_this = '/home/apps/portal/flask/bin/activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
from flipflop import WSGIServer
from app import app
if __name__ == '__main__':
WSGIServer(app).run()
Now apache errors_log has a new error message:
[Fri Dec 19 13:43:03 2014] [notice] Apache/2.2.15 (Unix) DAV/2 mod_fcgid/2.3.9 PHP/5.3.3 mod_wsgi/3.2 Python/2.6.6 configured -- resuming normal operations
[Fri Dec 19 13:43:05 2014] [warn] [client 110.143.38.80] (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
[Fri Dec 19 13:43:05 2014] [error] [client 110.143.38.80] Premature end of script headers: runp-sqlite.fcgi
Do you have Flask installed in a virtuelenv? If so, the problem may be that your WSGI file isn't activating it. This causes an ImportError or something similar when Apache tries to serve the site, which results in a not-very-helpful 500 Error.
The fix is to activate the virtualenv in your WSGI file before importing the app like this:
#!/bin/python
VENV_DIR = 'your_app/your_venv'
activate_this = os.path.join(VENV_DIR, 'bin', 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
from flipflop import WSGIServer
from app import app
if __name__ == '__main__':
WSGIServer(app).run()
See also this previous SO question: Running Python from a virtualenv with Apache/mod_wsgi, on Windows
I practice set up Django under Elastic Beanstalk from there document.
But There is error.
ERROR Your WSGIPath refers to a file that does not exist.
My directory like this:
-djangoenv (where I use git)
- mysite
-manage.py
-mysite
-__init__.py
-settings.py
-urls.py
-wsgi.py
and My the .elasticbeanstalk/optionsettings.djapp file like this :
And .ebextensions/python.config like this , I don't know where to put this .try several times still not work . I try mysite/mysite/wsgi.py still not work
container_commands:
01_syncdb:
command: "django-admin.py syncdb --noinput"
leader_only: true
option_settings:
- namespace: aws:elasticbeanstalk:container:python
option_name: WSGIPath
value: mysite/wsgi.py
- option_name: DJANGO_SETTINGS_MODULE
value: mysite.settings
Please tell me how and where to set my wsgi path ??
Thank you very much!
I found that you have to restart the server for it to take these changes into consideration.
I spent ages changing and tweaking these options and nothing worked. Then when I went to the EB console and restarted the environment it worked.
In the server you are about to deploy the django application to elasticbean stalk. Run:
eb config
Then replace the application.py to mysite/wsgi.py and save the changes.
After the update, you may do:
git add.
git commit -m "some updates"
eb deploy
After successfully update the environment, you may view the changes in elasticbeanstalk, under your enviroment, go to the instance and check the setting in Configuration, then view the WSGIPath under Software Configuration.
Disclaimer: This information is valid until 4 November 2016. AWS may further change the setting.
The path specified should be relative to the .elasticbeanstalk directory.
The correct path should be mysite/mysite.wsgi.py. option_settings: is:
option_settings:
- namespace: aws:elasticbeanstalk:container:python
option_name: WSGIPath
value: mysite/mysite/wsgi.py
- option_name: DJANGO_SETTINGS_MODULE
value: mysite.settings
You have WSGIPath set to "application.py" but your WSGI file is "mysite/wsgi.py".
You should try
mysite.wsgi:application and make sure you are in the mysite first folder while deploying your application
I'm attempting to deploy my first Django app to Elastic Beanstalk. The Beanstalk created successfully via command line tools, I uploaded from my Mercurial via ZIP, that seemed to work just fine. But I'm getting a 404 when attempting to access it.
Elastic Beanstalk HTTP Error (the log is huge, I can parse more, but I only see this for an error)
[Fri Jan 03 18:08:26 2014] [error] [client 127.0.0.1] Target WSGI script not found or unable to stat: /opt/python/current/app/application.py
WSGI Settings for the Django app
wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "company.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
settings.py
WSGI_APPLICATION = 'company.wsgi.application'
This works fine locally and I'm sure I'm just not understanding something that I should be.
Please help!
Folder structure:
/opt/python/current/app/
- company
- static
- templates
- wsgi.py
- settings.py
- __init__.py
- urls.py
- webapp
- templates
- manage.py
- requirements.txt
AWS Elastic Beanstalk's default configuration sets WSGIPath to application.py, you either need to rename your file with mappings to application.py or configure the environment to point to your mappings script.
Detailed information can be found in AWS Elastic Beanstalk Python Container Options: Option Values
You can set it with the management console
Or with .ebextensions option_settings
NB: It should be relative path.