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!
Related
I had started a fresh linode running ubuntu 19.04 and the first time I used the directions at:
https://www.rosehosting.com/blog/how-to-install-mezzanine-cms-on-ubuntu-18-04/
To install Mezzanine CMS it worked just fine, I could run the runserver command and see the django website. Eventually it started giving me a problem after trying 50 ways to deploy the site using apache and mod_wsgi.
I gave up and rebuilt the server and then still couldn't see the new install at the IP when I ran run server. I figured maybe it was because I accidentally installed some things using "python" and others with "python3" so I rebuilt the server.
This third time I followed the direction perfectly, the only difference is I didn't install a mysql server just kept the default SQLlite server and created a DB and Django Superuser.
I have added my ip as a host in settings.py and local_settings.py
I have already ran makemigrations and migrate
I did check to see if maybe the IP had changed when I rebuilt, it hadn't
My local environment on my laptop works fine, just not the linode
Any suggestions on anything I'm missing?
Deployment Guide
Step 1 (Installation)
Install apache2 mod_wsgi
Install virtualenv
Install virtualenv
Install Nginx for asynchronous event-driven approach to handle multiple client requests
Install mysql
sudo apt-get update
sudo apt-get install python3-pip python3-dev apache2 libapache2-mod-wsgi-py3
sudo apt-get install virtualenv ufw
sudo apt-get install nginx
sudo apt-get install mysql-server libmysqlclient-dev
sudo mysql_secure_installation
Step 2 (Apache & Hostname & User)
Set hostname and add it into /etc/hosts with hostname and your Server IP
Create new user and give add to the group sudo for safety
sudo adduser username
sudo usermod -aG sudo
Enable SSH authentication for login & Edit default port of ssh in /etc/ssh/sshd_config. https://askubuntu.com/questions/1074034/not-able-to-change-ssh-port-on-ubuntu-18-04-1-lts
Edit /etc/apache2/site-availabledefault-000.conf for your new Django configuration
Step 3 (Firewall)
sudo ufw allow 8000
sudo ufw allow http
sudo ufw allow ssh
sudo ufw default allow outgoing
sudo ufw default deny incoming
Enable all other required port numbers
ssh sudo ufw enable
Step 4 (Django configuration)
Chown static and media forlders and edit it's permission recursively
Add allowed host in settings.py
Checkout deployment checklist in django official website and do it.
Step 5 (Please checkout)
For apache configuration please visit https://pythonprogramming.net/deploying-to-server-django-tutorial/
Edit the path given in your apache configuration (path for WSGI Script,python-path, python-home ) if any errors found like Internal server error, miss configuration etc
For reference of python-home path please refer Get virtualenv's bin folder path from script
You can also add python-path to WSGIDaemonProcess
Additionally, you can visit puttygen for public and private key generation to login through SSH
For this particular problem turned out I just needed to suddenly bind dev server to 0.0.0.0
the command to do so was
python manage.py runserver 0.0.0.0:8000
Rinshans answers is definetley the details for deployment, I've followed those steps just kept making some mistake in the config and wsgi scripts. I'm going to try deploying with Gunicorn or use the Fabric self-deployment tools built in to Mezzanine CMS, just haven't done so yet.
I created a droplet(cloud server) on DigitalOcean and with no-ip.com I gave it the hostname - project.ddns.net.By ssh(ing) into the droplet I installed pip and virtualenv.
Inside /var/www/ I created a virtualenv and cloned the repository from github of my project.The directory struture is -
project_revamped (root of the project)
->requirements
->base.txt
->dev.txt
->project (django project)
->static
->media
->apps (folder which contains apps)
->manage.py
->project
->urls.py
->settings
->base.py
->dev.py
I installed apache2 and mod_wsgi using -
sudo apt-get install apache2
sudo apt-get install libapache2-mod-wsgi
I then installed mysql,created a database and installed all requirements
pip install -r base.txt
I created a virtualhost project.conf on the path -
/etc/apache2/sites-available/project.conf
the content of file is this -
<VirtualHost *:80>
ServerAdmin example#gmail.com
ServerName project.ddns.net
ServerName www.project.ddns.net
WSGIScriptAlias / /var/www/project_revamped/project/project/wsgi.py
<Directory /var/www/project_revamped/project/project>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Then I gave this command to activate this conf file -
a2ensite project.conf
The content of my wsgi.py in my django project is -
import os
import sys
import site
#Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/var/www/.virtualenvs/projectenv/local/lib/python2.7/site-packages')
#Add the app's directory to the python path
sys.path.append('/var/www/project_revamped/project')
sys.path.append('/var/www/project_revamped/project/project')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings.dev'
#Activate your virtualenv
activate_env = os.path.expanduser('/var/www/.virtualenvs/projectenv/bin/activate_this.py' )
execfile(activate_env, dict(__file__=activate_env))
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
After changing the files I finally gave the commands -
service apache2 reload
service apache2 restart
However after doing these things right the corresponding ip says there is some problem the server and sends 500 error.I guess the problem is somewhere in my configuration because apache server was responding working fine.After I include django project with it the problem starts.
Can anybody please help me here in the configuration? I am stuck in this for past 2 days and every different article on the internet tells the different story.
Have a look at the official documentation. I think you're missing the WSGIPythonPath-directive.
As #BurhanKhalid stated, this linked tutorial is complete and tested and should nearly exactly match your setup.
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 am trying to serve a Django application with uWSGI from Docker. I am using supervisord to start the process for me at the end of the Dockerfile. When I run the image, it says that the uWSGI process starts and succeeds, but I'm unable to view the application at the URL I thought would display it. Perhaps I do not have things set up/configured correctly?
I am not having supervisord start nginx right now because I am currently serving static files via Amazon S3, and want to first focus on getting the wsgi up and running.
I am successful in running the application using uwsgi locally by doing uwsgi --init uwsgi.ini:local, but I am having trouble moving it into docker.
Here is my Dockerfile
FROM ubuntu:14.04
# Get most recent apt-get
RUN apt-get -y update
# Install python and other tools
RUN apt-get install -y tar git curl nano wget dialog net-tools build-essential
RUN apt-get install -y python3 python3-dev python-distribute
RUN apt-get install -y nginx supervisor
# Get Python3 version of pip
RUN apt-get -y install python3-setuptools
RUN easy_install3 pip
RUN pip install uwsgi
RUN apt-get install -y python-software-properties
# Install GEOS
RUN apt-get -y install binutils libproj-dev gdal-bin
# Install node.js
RUN apt-get install -y nodejs npm
# Install postgresql dependencies
RUN apt-get update && \
apt-get install -y postgresql libpq-dev && \
rm -rf /var/lib/apt/lists
ADD . /home/docker/code
# Setup config files
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
RUN rm /etc/nginx/sites-enabled/default
RUN ln -s /home/docker/code/nginx-app.conf /etc/nginx/sites-enabled/
RUN ln -s /home/docker/code/supervisor-app.conf /etc/supervisor/conf.d/
RUN pip install -r /home/docker/code/app/requirements.txt
EXPOSE 8080
CMD ["supervisord", "-c", "/home/docker/code/supervisor-app.conf", "-n"]
And here is my uwsgi.ini
[uwsgi]
# this config will be loaded if nothing specific is specified
# load base config from below
ini = :base
# %d is the dir this configuration file is in
socket = %dmy_app.sock
master = true
processes = 4
[dev]
ini = :base
# socket (uwsgi) is not the same as http, nor http-socket
socket = :8001
[local]
ini = :base
http = :8000
# set the virtual env to use
home=/Users/my_user/.virtualenvs/my_env
[base]
# chdir to the folder of this config file, plus app/website
chdir = %dmy_app/
# load the module from wsgi.py, it is a python path from
# the directory above.
module=my_app.wsgi:application
# allow anyone to connect to the socket. This is very permissive
chmod-socket=666
http = :8080
And here is my supervisor-app.conf file
[program:app-uwsgi]
command = /usr/local/bin/uwsgi --ini /home/docker/code/uwsgi.ini
From a MAC using boot2docker, I am trying to access the application at $(boot2docker ip):8080
Ultimately I want to upload this container to AWS Elastic Beanstalk, with not only a uWSGI process running, but a celery worker running as well.
When I run my container, I can see from the logs that both supervisor and uwsgi successfully start. I was able to get things running on my local machine both using uwsgi by itself and uwsgi through supervisor, but for some reason when I containerize the thing I can't find it anywhere.
Here is what is logged when I boot up the docker container
2014-12-25 15:08:03,950 CRIT Supervisor running as root (no user in config file)
2014-12-25 15:08:03,953 INFO supervisord started with pid 1
2014-12-25 15:08:04,957 INFO spawned: 'uwsgi' with pid 9
2014-12-25 15:08:05,970 INFO success: uwsgi entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
How are you starting the docker container?
I don't see any CMD or ENTRYPOINT script, so I'm unclear as to how anything is getting started.
In general, I would advise avoiding things like supervisord unless absolutely necessary, just start uWSGI in the foreground from a CMD line. Try adding the following as the last line in the Dockerfile:
CMD ["/usr/local/bin/uwsgi", "--ini", "/home/docker/code/uwsgi.ini"]
and then just run with docker run -p 8000:8000 image_name. You should get some reply from uWSGI. If that works, I recommend you move the other services (postgres, node, to separate containers). There are official images for Node, Python and Postgres which should save you some time.
Remember, Docker containers only run as long as their main process (which must be in the foreground).