2019-01-11 nginx+uwsgi+python+django spirit 论坛项目

1. 通过uwsgi启动, 测试uwsgi+django是否正常:

    

root@3a560ed12c50:/code/mysite# ll
total 408
drwxr-xr-x 4 root root 4096 Jan 11 06:58 ./
drwxr-xr-x 1 root root 4096 Jan 10 10:25 ../
-rw-r--r-- 1 root root 25 Jan 10 10:25 __init__.py
-rw-r--r-- 1 root root 389120 Jan 11 06:58 db.sqlite3
-rw-r--r-- 1 root root 0 Jan 10 10:45 django.log
-rw-r--r-- 1 root root 254 Jan 10 10:25 manage.py
drwxr-xr-x 4 root root 4096 Jan 10 10:45 mysite/
drwxr-xr-x 4 root root 4096 Jan 10 10:45 static/
root@3a560ed12c50:/code/mysite# uwsgi --http :80 --module mysite.wsgi     # 这里的mysite.wsgi 指向mysite app下的wsgi.py文件

*** Starting uWSGI 2.0.17.1 (64bit) on [Fri Jan 11 07:04:16 2019] ***
compiled with version: 7.3.0 on 03 January 2019 07:21:48
os: Linux-4.15.0-43-generic #46~16.04.1-Ubuntu SMP Fri Dec 7 13:31:08 UTC 2018
nodename: 3a560ed12c50
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /code/mysite
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** WARNING: you are running uWSGI without its master process manager ***
your memory page size is 4096 bytes
detected max file descriptor number: 1048576
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :80 fd 4
spawned uWSGI http 1 (pid: 267)
uwsgi socket 0 bound to TCP address 127.0.0.1:46245 (port auto-assigned) fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x557757c8ab50
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72904 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x557757c8ab50 pid: 266 (default app)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 266, cores: 1)

打开浏览器,输入127.0.0.1:

因为网站是部署在docker 镜像上的, 所以从宿主机访问加上了80的对应映射端口8090,同时,可以看到,这种方式静态文件都无法找到(不知是否是我的方法漏了步骤?)

 

 

 2.  nginx + uwsgi + django 启动:

      /etc/nginx/sites-enabled/default

      

root@3a560ed12c50:/code/mysite/mysite# cat /etc/nginx/sites-enabled/default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name _;

# location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
# }

# location /uwsgi {
# include /etc/nginx/uwsgi_params;
# uwsgi_pass 127.0.0.1:8001;
# }

location / {
root /code/mysite;
uwsgi_pass 127.0.0.1:8001;
include /etc/nginx/uwsgi_params;
}

location /static {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /code/mysite/static/;
}


# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}

 a. 启动 nginx:  

     service nginx start  或者 service nginx reload + service nginx restart

b. 启动uwsgi:

    uwsgi --socket :8001 --module mysite.wsgi

 

然后通过浏览器访问 

 

这个时候是正常的,静态文件是可以访问的,样式都是对的。

 

 

改进:

1. 每次通过传递参数启动uwsgi比较麻烦,所以更好的方式是用配置文件的方式,配置文件可以用ini和xml两种格式

root@3a560ed12c50:/code/mysite# pwd
/code/mysite
root@3a560ed12c50:/code/mysite# ll
total 424
drwxr-xr-x 4 root root 4096 Jan 11 10:09 ./
drwxr-xr-x 1 root root 4096 Jan 10 10:25 ../
-rw-r----- 1 root root 3503 Jan 11 10:09 UWSGI.log
-rw-r--r-- 1 root root 25 Jan 10 10:25 __init__.py
-rw-r--r-- 1 root root 389120 Jan 11 10:09 db.sqlite3
-rw-r--r-- 1 root root 0 Jan 10 10:45 django.log
-rw-r--r-- 1 root root 254 Jan 10 10:25 manage.py
drwxr-xr-x 4 root root 4096 Jan 10 10:45 mysite/
-rw-rw-rw- 1 root root 4 Jan 11 10:03 pid.uwsgi
drwxr-xr-x 4 root root 4096 Jan 10 10:45 static/
-rw-r--r-- 1 root root 524 Jan 11 09:59 uwsgi.ini
-rw-r--r-- 1 root root 178 Jan 11 10:01 uwsgi.xml
root@3a560ed12c50:/code/mysite#

    a.  uwsgi.ini

root@3a560ed12c50:/code/mysite# cat uwsgi.ini
[uwsgi]
socket = :8001

# the base directory (project full path)
chdir = /code/mysite

# Django wsgi file
module = mysite.wsgi

# process-related settings
# master
master = true

# maximum number of worker processes
processes = 4

# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
# pidfile for record run pid
pidfile =pid.uwsgi
# run process background and save log to daemonize
daemonize = UWSGI.log
root@3a560ed12c50:/code/mysite#

    b.  uwsgi.xml

root@3a560ed12c50:/code/mysite# cat uwsgi.xml
<uwsgi>
<socket>127.0.0.1:8001</socket>
<chdir>/code/mysite</chdir>
<module>mysite.wsgi</module>
<processes>4</processes>
<daemonize>uwsgi_xml.log</daemonize>
</uwsgi>
root@3a560ed12c50:/code/mysite#

 

启动:     uwsgi --ini uwsgi.ini  

# 因为配置了后台运行,所以用ps查看uwsgi进程,可以看到有5个uwsgi进程,一个是master,4个worker

root@3a560ed12c50:/code/mysite# uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
root@3a560ed12c50:/code/mysite# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18592 2468 pts/0 Ss Jan10 0:00 bash
root 112 0.0 0.0 18596 2880 pts/1 Ss+ Jan10 0:00 bash
root 454 0.0 0.0 140624 1520 ? Ss 07:55 0:00 nginx: master process /usr/sbin/nginx
www-data 456 0.0 0.1 140944 4712 ? S 07:55 0:00 nginx: worker process
root 466 7.8 1.3 105560 38324 ? S 10:03 0:00 uwsgi --ini uwsgi.ini
root 468 0.0 1.0 105560 31580 ? S 10:03 0:00 uwsgi --ini uwsgi.ini
root 469 0.0 1.0 105560 31580 ? S 10:03 0:00 uwsgi --ini uwsgi.ini
root 470 0.0 1.0 105560 31580 ? S 10:03 0:00 uwsgi --ini uwsgi.ini
root 471 0.0 1.0 105560 31580 ? S 10:03 0:00 uwsgi --ini uwsgi.ini
root 472 0.0 0.0 34400 2804 pts/0 R+ 10:03 0:00 ps aux
root@3a560ed12c50:/code/mysite#

如果是xml启动:

uwsgi -x uwsgi.xml

root@efc817923cbf:/code/mysite# uwsgi -x uwsgi.xml
[uWSGI] parsing config file uwsgi.xml
root@efc817923cbf:/code/mysite# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 18508 3412 pts/0 Ss 10:49 0:00 bash
root 22 18.3 1.3 105520 38652 ? S 10:51 0:00 uwsgi -x uwsgi.xml
root 24 0.0 1.0 105520 31584 ? S 10:51 0:00 uwsgi -x uwsgi.xml
root 25 0.0 1.0 105520 31584 ? S 10:51 0:00 uwsgi -x uwsgi.xml
root 26 0.0 1.0 105520 31584 ? S 10:51 0:00 uwsgi -x uwsgi.xml
root 27 0.0 0.0 34400 2812 pts/0 R+ 10:51 0:00 ps aux
root@efc817923cbf:/code/mysite#

注意,这里只有4个uwsgi进程,因为没有指定master为true, 如果需要,可以添加:

<master>true</master>

 

posted @ 2019-01-11 15:14  alxe_yu  阅读(241)  评论(0)    收藏  举报