Nginx + uwsgi + mysql + python (ubuntu16.04)
http://www.cnblogs.com/alex3714/p/6538374.html
nginx:
1、端口转发(lb)
2、静态文件代理
uwsgi(处理python运行环境,类似于java的tomcat)
mysql相关配置:
1、安装mysql: apt install mysql-server
2、修改mysql配置文件: mysqld.cnf bind-address = 0.0.0.0
3、创建database并授权: create database mysite; grant all privileges on mysite.* to 'mysite'@'%' identified by 'mysite_pass'; flush privileges;
4、生成数据表
python环境配置:
1、从开发环境导出pip list: pip freeze > requirements.txt
2、将requirements.txt的内容复制到linux环境中
3、安装python3 和 python3-pip
4、安装python依赖包: pip3 install -r requirements.txt
小技巧:使用豆瓣python源安装个别比较大的库 pip2 install -i https://pypi.douban.com/simple pillow==3.4.1
uwsgi配置:
1、安装uwsgi: pip3 install uwsgi
2、测试uwsgi: uwsgi --http :8000 --module MySite.wsgi
3、注意在测试的时候需要将django settings.py中的 ALLOWED_HOSTS = [] 修改为 ['*']
4、创建uwsgi配置文件 mysite_uwsgi.ini:
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/bobby/Projects/MxOnline
# Django's wsgi file
module = MxOnline.wsgi
# the virtualenv (full path)
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = 127.0.0.1:8000
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
virtualenv = /home/bobby/.virtualenvs/mxonline #虚拟目录看情况而定
nginx配置:
1、安装nginx: apt-get install nginx
2、在django项目目录下创建conf目录及nginx配置文件uc_nginx.conf:
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name 你的ip地址 ; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias 你的目录/Mxonline/media; # 指向django的media目录
}
location /static {
alias 你的目录/Mxonline/static; # 指向django的static目录
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include uwsgi_params; # the uwsgi_params file you installed
}
}
3、将该配置文件链接到nginx的启动配置文件中
sudo ln -s 你的目录/Mysite/conf/nginx/uc_nginx.conf /etc/nginx/conf.d/
4、重启nginx: systemctl restart nginx.service
5、修改django settings.py配置文件,添加STATIC_ROOT,注意 STATICFILES_DIRS跟STATIC_ROOT不能同时生效,需要将其注释
django.contrib.staticfiles提供一个便捷的管理命令用于将静态文件收集到一个目录中,这样你就可以轻松地供给这些文件:
-
将 STATIC_ROOT设置为你提供文件的目录,例子如下
STATIC_ROOT = "/var/www/example.com/static/"
-
运行collectstatic管理命令
$ python manage.py collectstatic
这会将所有你在所有static目录下的所有的文件都复制到 STATIC_ROOT 目录中