On production, I am using Django 2.1 + Nginx + uWSGI. Now, I am planning to migrate to Django 3. Django 3 requires python version 3.6+, but my current python version is 3.5.
I have installed python 3.7 on my dev server from source as make altinstall so on dev server I can run it as python37 manage.py runserver, but on production server how can I define python version to 3.7 to start server?
Here is my uwsgi.ini
#mysite_uwsgi.ini
[uwsgi]
chdir = /var/www/dostavka
module = dostavka.wsgi
master = true
processes = 10
socket = /var/www/dostavka/dostavka.sock
chmod-socket = 666
vacuum = true
Then i run it using command
uwsgi --ini /var/www/dostavka/mysite_uwsgi.ini --daemonize /var/www/uwsgi.log --uid www-data --gid www-data
I am not using virtualenv at production.
So, I have solved the problem. After make altnstall python 3.7 on production server, I have re-installed uWSGI and Django by using pip3.7 install uwsgi and pip3.7 intall django. After that uWSGI start to use new python version 3.7.
Also, if you have any other applications connected to django, you need to install them using pip3.7 intall xxxx.
Related
I had a working Django project that had built and deployed many images over the last few weeks. My django project kept working fine when ran as "python manage.py runserver" and the Docker images kept being built fine (all successful builds). However the django app now doesn't deploy. What could be the issue and where should I start to look for it? I've tried the logs but they only say "Starting Django" without actually starting the service
I use github and have gone back to previous versions of the code and none of them now work, even though the code is exactly the same. It also fails to deploy the Django server on AWS Elastic Beanstalk infrastructure which is my ultimate goal with this code.
start.sh:
#!/bin/bash
echo Starting Django
cd TN_API
exec python ../manage.py runserver 0.0.0.0:8000
Dockerfile:
FROM python:2.7.13-slim
# Set the working directory to /app
WORKDIR /TN_API
# Copy the current directory contents into the container at /app
ADD . /TN_API
# COPY startup script into known file location in container
COPY start.sh /start.sh
# Install requirements
RUN pip install -r requirements.txt
# EXPOSE port 8000 to allow communication to/from server
EXPOSE 8000
# CMD specifcies the command to execute to start the server running.
CMD ["/start.sh"]
Commands:
sudo docker run -d tn-api
sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d28115a919f9 tn-api "/start.sh" 11 seconds ago Up 8 seconds 8000/tcp festive_darwin
sudo docker logs [container id]
Starting Django
(doesn't do the whole:
Performing system checks...
System check identified no issues (0 silenced).
August 06, 2017 - 20:54:36
Django version 1.10.5, using settings 'TN_API.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.)
I changed several things and although it doesn't work locally it seems to work fine when deployed to AWS. I still don't get the feedback I used to get such as below but that's ok. I can hit the server and it works. Thank you all for your help.
Performing system checks...
System check identified no issues (0 silenced). August 06, 2017 - 20:54:36 Django version 1.10.5, using settings 'TN_API.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
It looks like the path is wrong for the manage.py script in /start.sh.
Your start.sh:
#!/bin/bash
echo Starting Django
cd TN_API
exec python ../manage.py runserver 0.0.0.0:8000
Seeing that you set WORKDIR to you project directory in the Dockerfile, the start.sh script is actually run from inside the project directory - which means it is actually doing this:
cd /TN_API # WORKDIR directive in Dockerfile
echo Starting Django # from the start.sh script
cd /TN_API/TN_API # looking for TN_API within your current pwd
exec python /TN_API/manage.py runserver 0.0.0.0:8000 # goes up a level (..) to look for manage.py
So it could be that your context for running runserver is off.
You can avoid this path jumping by rewriting your Dockerfile to include an CMD directive as follows:
FROM python:2.7.13-slim
# Set the working directory to /app
WORKDIR /TN_API
# Copy the current directory contents into the container at /app
ADD . /TN_API
# Install requirements
RUN pip install -r requirements.txt
# EXPOSE port 8000 to allow communication to/from server
EXPOSE 8000
# CMD specifcies the command to execute to start the server running.
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Here using python manage.py runserver 0.0.0.0:8000 will work since you set the WORKDIR to your project directory already. So you wouldn't need your start.sh script necessarily.
I have developed a Django 1.8 application using Python 3.3. I am trying to deploy the application on a university dedicated server with Red Hat Enterprise Linux Server release 7.2 (Maipo). (Note: RHEL does not let me install newer versions of Django and Python)
I had difficulty with installing mod_wsgi using the RHEL Apache and I ended up installing:
pip install -U mod_wsgi-httpd
pip install mod_wsgi
Everything works perfectly when I use the following setup:
python manage.py runmodwsgi --setup-only --port=80 --user=iman --group=root --server-root=/etc/mod_wsgi-express-80
However, when I want to setup HTTPS, using the following setup does not return any error message, but the website does not show up neither with http:// nor with https:// prefixes.
python manage.py runmodwsgi --setup-only --port=80 --user=iman --group=root --server-root=/etc/mod_wsgi-express-80 --https-only --https-port=443 --ssl-certificate-file=/etc/sslcert/iman.crt --ssl-certificate-key-file=/etc/sslcert/private/iman.key --ssl-ca-certificate-file=/etc/sslcert/certs/ca-bundle.crt --server-name=iman123.university.edu
Here are helpful resources that I found about mod_wsgi:
mod_wsgi docs
GitHub repository
Running HTTPS and client authentication with mod_wsgi-express
pip install mod_wsgi-express left out mod_ssl.so
Thank you so much for your time and concern and apologies in advance if I am doing something wrong.
I have intalled gunicorn,but gunicorn command not found:
# pip3.4 install gunicorn
Requirement already satisfied (use --upgrade to upgrade): gunicorn in /usr/local/python3.4/lib/python3.4/site-packages
# gunicorn
-bash: gunicorn: command not found
what is the problem,is gunicorn install path not be recognized by system?
I faced the same issue and it turns out I had to add gunicorn binary path to Linux PATH variable. You can start by echoing $PATH to see all binary path listed on the system. Then find out where gunicorn is installed. For my case I was using python virtual environment and pyenv which helps manage several python versions and dependencies separately.
(venv3.6) dave#daverig (develop)✗ % pip show gunicorn
Name: gunicorn
Version: 19.7.1
Summary: WSGI HTTP Server for UNIX
Home-page: http://gunicorn.org
Author: Benoit Chesneau
Author-email: benoitc#e-engura.com
License: MIT
Location: /home/dave/.pyenv/versions/3.6.2/envs/venv3.6/lib/python3.6/site-packages
Notice gunicorn is installed in /home/dave/.pyenv/versions/3.6.2/envs/venv3.6/lib/python3.6/site-packages and the corresponding path for the binaries for this particular python version is at /home/dave/.pyenv/versions/3.6.2/envs/venv3.6/bin. So I had to add that to Linux path via ~/.profile file like so;
export PATH=$PATH:$HOME/.pyenv/versions/3.6.2/envs/venv3.6/bin then ofcourse you want to refresh this using source ~/.profile or restart your terminal. Once I was able to do this, gunicorn binary was now available on my console;
(venv3.6) dave#daverig (develop)✗ % gunicorn --version
gunicorn (version 19.7.1)
I had the same problem on Debian.
It turns out that on Debian the documentation advises to install gunicorn via apt:
$ sudo apt install gunicorn
i just created a file named gunicorn and type these codes below which is the same as my development server , and included it into system path,such as /usr/bin
#!/usr/local/bin/python3.4
#-*- coding: utf-8 -*-
import re
import sys
from gunicorn.app.wsgiapp import run
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$','',sys.argv[0])
sys.exit(run())
in this way, it solved my problem,but still confused me,why gunicorn command not generated and included into system path automatically?and why my development server did ,both the same OS (centos 6.5 x64)
Installing gunicorn from source saved me after 2 hours trying!
pip3 install git+https://github.com/benoitc/gunicorn.git
If you installed python3 from source compiled, you should export your python3 PATH:
export PATH=$PATH:/usr/local/python3/bin
Are you use python3.4-venv?
if true
Delete env folder
Just reinstall: python3.4-venv. Ex for ubuntu:
apt install python3.4-venv
Exec: python3.4 -m venv env
source env/bin/activate
reinstall gunicorn by : pip3 install gunicorn or install from requirements.txt by pip3 install -r requirements.txt
go to terminal and change directory to environment and then type the below command.
pip install gunicorn
#Enjoy1
I am upgrading a Django installation from 1.4 to 1.7, which means I am also migrating from Python 2.6.6 to Python 2.7. The production server uses Debian 6.09, which requires python 2.6 globally, and unfortunately upgrading the OS is not a valid solution at this time.
To get around this, I installed the following stack:
pyenv with python 2.7
virtualenv
virtualenvwrapper
uwsgi
supervisor (installed from the global python 2.6, run as root)
nginx
When I run uwsgi manually from the the virtualenv, the site works great. However, when I start it with supervisor, it will only use the global python install.
As user with virtualenv:
(django1.7)user#staging:~$ echo $PATH
/home/user/.virtualenvs/django1.7/bin:/home/user/.pyenv/shims:/home/user/.pyenv/bin:/usr/local/bin:/usr/bin:/bin
My supervisor config file:
[program:app]
command = /home/user/.virtualenvs/django1.7/bin/uwsgi
--module app.wsgi
--socket 127.0.0.1:10001
--master
--harakiri 120
--max-requests 5000
--threads 6
directory=/home/user/app/
environment=PATH="/home/user/.virtualenvs/django1.7/bin:/home/user/.pyenv/shims:/home/user/.pyenv/bin:",DJANGO_SETTINGS_MODULE="app.settings",HOME="/home/user"
user=user
autostart=true
autorestart=true
redirect_stderr=true
stopsignal=QUIT
Can anyone help point out where my config is wrong?
Thanks!
uWSGI has a specific virtualenv configuration directive:
virtualenv=/home/user/.virtualenvs/django1.7
Another example:
https://github.com/miohtama/LibertyMusicStore/blob/master/conf/uwsgi.ini
More information
http://uwsgi-docs.readthedocs.org/en/latest/tutorials/dreamhost.html?highlight=virtualenv
(Looks like uWSGI documentation regarding virtualenv is bit confusing, I might need to double check this with the authors)
The configuration I posted ended up working. I was foolish and did not run supervisorctl update first to make sure it loaded in the updated configuration.
I'm trying do deploy a django project. I tried a lot of tutorials, but had no luck. I use a new clean Ubuntu 11.10. I've performed
apt-get install nginx
apt-get install uwsgi
service nginx start
I've created folder /deploy/project1 and put there manage.py and other files.
My current /deploy/project1/project1/wsgi.py contains:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project1.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
So, could you tell me how to deploy my django app for domain.com properly?
I've also installed Django via pip and easy_install
What should I add in /etc/nginx/sites-enabled/default.
Assuming that you have installed all requirement and you are using the aptitude packages then you don't need the wsgi.py. All the configuration is in the uwsgi ini/xml/yaml file. (take the format that you prefer).
Here is a minimal example for example.com file for nginx(/etc/nginx/sites-available/examplecom for ubuntu 11.10)
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/projectname.log;
location /media {
alias /vagrant/test/projectname/media/;
}
location /static {
alias /vagrant/test/projectname/static/;
}
location / {
uwsgi_pass unix:///run/uwsgi/projectname/socket;
include uwsgi_params;
}
}
Create a symbolic link to /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/examplecom /etc/nginx/sites-enabled/examplecom
or
sudo /usr/sbin/nxensite examplecom
You are done with NGINX.
Go to /etc/uwsgi/apps-available and create your ini file
sudo vim /etc/uwsgi/apps-available/projectname.ini
[uwsgi]
virtualenv=/home/vagrant/.virtualenvs/projectenv
thread=3
master=1
env = DJANGO_SETTINGS_MODULE=projectname.settings
module = django.core.handlers.wsgi:WSGIHandler()
chdir = /path/to/my/django/project
socket = /run/uwsgi/projectname/socket
logto = /var/log/uwsgi/projectname.log
Point your ini to /etc/uwsgi/apps-enabled/projectname.ini
sudo ln -s /etc/uwsgi/apps-available/projectname.ini /etc/uwsgi/apps-enabled/projectname.ini
For more information, see any of these files on your system:
/etc/uwsgi/apps-available/README
/etc/uwsgi/apps-enabled/README
/usr/share/doc/uwsgi/README.Debian.gz
/etc/default/uwsgi
You are done. You can now restart nginx & uwsgi
sudo service nginx restart
sudo service uwsgi restart
Cheers!
Do not forget that Debian's, Ubuntu's and its derivates' uwsgi package does not require installation of its Python plugin — uwsgi-plugin-python, because uWSGI does not necessarily uses only Python (there are plugins for Lua, Erlang and other languages). However, Django requires Python plugin. Install it:
sudo apt install uwsgi-plugin-python
Unlike PIP's installation, you should explicitly mention uwsgi's plugin used in the app's config by adding a plugins = python line to it (if you use Python), like this:
[uwsgi]
plugins = python
uwsgi-socket = /var/sockets/django.sock
chmod-socket = 660
chdir = /home/v/django
module = project.wsgi
Unless you do this, there will be no Python-specific options available. And Debian's/Ubuntu's uWSGI will be just quiet about it!