apt-get update // 更新 apt sudo apt-get install git // 安装git git clone git://github.com/yyuu/pyenv.git ~/.pyenv // 下载 pyenv echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo 'eval "$(pyenv init -)"' >> ~/.bashrc exec $SHELL -l // 查看可安装版本 pyenv install --list // 安装python依赖 sudo apt-get install libc6-dev gcc sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm // 安装python pyenv install 3.5.2
注:安装pyenv 后要重新打开会话 或者 source ~/.bash_profile
useradd mvly // 创建用户
passwd mvly // 设置密码
1、pyenv global 3.6.4 // 全局切换python版本 2、pyenv local 3.6.4 // 当前文件夹下python版本
virtualenv:
git clone git://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv // 安装pyenv-virtualenv插件
pyenv virtualenv 3.6.4 ident // 基于3.6.4 创建环境
然后 直接在某个项目下。pyenv local ident 可切换到对应的环境
pip install uwsgi 切换环境之后,在当前 virtualenv下安装uwsgi
安装:Nginx
apt-get install nginx 然后: /etc/init.d/nginx start # 启动 /etc/init.d/nginx stop # 停止 /etc/init.d/nginx restart # 重启 安装uwsgi apt-get install python-dev
在django目录下创建: # test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return ["Hello World"] # python2 #return [b"Hello World"] # python3 执行以下命令: uwsgi --http :8001 --plugin python --wsgi-file test.py curl "http://localhost:8001" 测试是否发布成功
安装mysql
apt-get install mysql-server
mysql -uroot -p 测试是否安装成功
现在设置mysql允许远程访问,首先编辑文件/etc/mysql/mysql.conf.d/mysqld.cnf: 真实生产环境就算了。 sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf 注释掉bind-address = 127.0.0.1: service mysql restart // 重启服务 ------- 远程测试连接
安装 mysqlclient 报 OSError: mysql_config not found
则 安装依赖: apt-get install libmysqlclient-dev python3-dev
python manager.py runserver 查看能不能运行
Nginx+uwsgi+Django
myweb/
├── manage.py
├── myweb/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── myweb_uwsgi.ini
创建myweb_uwsgi.int
# myweb_uwsgi.ini file [uwsgi] # Django-related settings socket = :8000 # the base directory (full path) chdir = /home/fnngj/pydj/myweb # Django s wsgi file module = myweb.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
// 运行uwsgi 查看是否有错误
uwsgi --ini myweb_uwsgi.ini
再接下来要做的就是修改nginx.conf配置文件。配置nginx: /etc/nginx/sites-enabled/default
…… server { listen 8099; server_name 127.0.0.1 charset UTF-8; access_log /var/log/nginx/myweb_access.log; error_log /var/log/nginx/myweb_error.log; client_max_body_size 75M; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; uwsgi_read_timeout 2; } location /static { expires 30d; autoindex on; add_header Cache-Control private; alias /home/fnngj/pydj/myweb/static/; } }
include 必须指定为uwsgi_params;而uwsgi_pass指的本机IP的端口号与myweb_uwsgi.ini配置中的文件中的必须一致。 ……
重启nginx ; service nginx stop; service nginx start;
然后,访问:http://127.0.0.1:8099/
记得关闭安全组策略哦。