My environment
conda 4.7.10, 3.6.7 Python, Django 3.0.3
Installation uWSGI
pip install uwsgi
If there is a problem, consider the following solution: Reference Links
apt-get install python3-dev
apt-get install gcc-4.7 ##卸载你的gcc版本,安装为4.7:
rm /usr/bin/gcc
ln -s /usr/bin/gcc-4.7 /usr/bin/gcc
Finally, re-run the install commandpip install uwsgi
UWSGI test is working properly
- Test.py create a new file:
touch test.py
If you want to delete the run afterrm 文件名
enter the following:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2
- Run uWSGI:
uwsgi --http :8080 --wsgi-file test.py
may be error:
uwsgi: error loading the while the Libraries Shared: libpcre.so.1: CAN not Open Shared Object File: No SUCH File or Directory
Solution: Reference Links
find / -name libpcre.so.* ##找到所有的系统中libpcre
ln -s /root/anaconda3/lib/libpcre.so.1 /lib ##创建libpcre.so.1软链到/lib下
which uwsgi ##测试一下是否好用了
Here is a pit, if your server is a cloud server, for example, I was Ali cloud, we must pay attention to running the port settings and port security groups corresponding to the cloud server console, or it may not work correctly with http access port.
Browser type
http://example.com:8000
appears Hello World instructions to run successfully.
The following represents the normal path: at The Web Client <-> uWSGI <-> Python
ps.Ubuntu kill command specifies the process:kill -9 $(lsof -i tcp:8080 -t)
From simple test.py to Django project
- Create a Django project and ensure that it runs correctly: how to create a reference here
. Ps remember the cloud server Django project settings.py put inALLOWED_HOSTS = ['']
insteadALLOWED_HOSTS = ['*']
, allow other normal access address. UWSGI use to run:
uwsgi --http :8000 --module mysite.wsgi
If you can run the following description of the normal route normal: the web client <-> uWSGI <-> Django
usually we do not allow your browser to communicate directly with uWSGI, that is the job of the web server.Install Nginx
sudo apt-get install nginx
sudo /etc/init.d/nginx start ##启动nginx服务
Browser access through port 80, you should get a message from Nginx: "Welcome to nginx!" .
This shows the normal path: the web client <-> the web server
Nginx configuration of static routes
- New Django directory named in the
uwsgi_params
file, the file contents from here Copy - New Django directory named in the
mysite_nginx.conf
document, says:
upstream django {
server 0.0.0.0:8081; #web socket
}
server {
listen 8080; #nginx端口
server_name xx.xx.xx.xx; #IP地址
charset utf-8;
client_max_body_size 75M;
location /media {
alias /path/to/your/mysite/media; #media文件路径
}
location /static {
alias /path/to/your/mysite/static; #static文件路径
}
location / {
uwsgi_pass django;
include /path/to/your/mysite/uwsgi_params; #uwsgi_parms文件路径
}
}
- The above documents linked to the / etc / nginx / sites-enabled in order to identify Nginx
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
- Modify Django's settings.py file, add the following statement:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
- Run the following command:
python manage.py collectstatic
- Restart Nginx
sudo /etc/init.d/nginx restart
Test
test pictures test.jpg into the media folder accessxx.xx.xx.xx:8080/media/test.jpg
if they can access, indicating nginx provided the correct file servicesDynamic configuration request Nginx
Nginx to lettest.py
the application tell "hello world" it.
uwsgi --socket :8081 --wsgi-file test.py
socket :8081
: Use uWSGI protocol, port 8081, the same time, Nginx is already configured to communicate with the uWSGI in the port, while the external port 8000. Access:xx.xx.xx.xx:8080/
emergence of "hello world" explained the following path normal: the web client <-> the web server <-> the socket <-> uWSGI <-> PythonInstead of using a Unix socket port
We are currently using a simple TCP socket, replace Unix socket used with less overhead.- Edit
mysite_nginx.conf
the file
to the first sentenceserver 0.0.0.0:8081; #web socket
readserver unix:///path/to/your/mysite/mysite.sock;
here / path / to / your / ..... into your own path, mysite.sock is automatically generated, do not ignore it. - Restart Nginx:
sudo /etc/init.d/nginx restart
- Again run uWSGI:
uwsgi --socket mysite.sock --wsgi-file test.py
In the browser, go to:
xx.xx.xx.xx:8000/
here may appear 502 Bad Gateway, because Nginx no authority to enter the directory, it can not access the socket file, one solution is to change the identity of a user running Nginx, the /etc/nginx/nginx.conf the first row of user www-data; www-data change permissions high enough users, restart Nginx. I directly into the root, you can run properly.Use uWSGI and Nginx running Django application
Enter the following command to run Django application
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664