Debian+Django+uWsgi+nginx+mysql+celery
Posted 梁伟雄的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Debian+Django+uWsgi+nginx+mysql+celery相关的知识,希望对你有一定的参考价值。
下载系统各种依赖
nano /etc/apt/sources.list
在Debian中使用apt-get安装软件包时经常会提示让你插入netinst的光盘:
Media change: please insert the disc labeled
把下面这一行注释掉
deb cdrom:[Debian GNU/Linux 8.2.0 _Jessie_ - Official amd64 CD Binary-1 20150$
apt-get update
apt-get install python-setuptools python-dev python-pip python-virtualenv redis-server mysql-server mysql-client git -y
把项目拉到服务器
cd ~
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub
把公钥粘贴到码云上
此处应以码云上的地址为准
pip安装各种包
cd ~/serve/
EnvironmentError:mysql config not found
apt-get install libmysqld-dev
网上有人解释说使用apt-get安装的MySQL是没有mysql_config这个文件的
pip install -r requirements.txt
配置celery
cd /etc/systemd/system/
nano celery.service
内容如下:
[Unit]
Description=Celery Service
After=network.target
[Service]
Type=forking
#User=celery
#Group=celery
EnvironmentFile=-/etc/conf.d/celery
WorkingDirectory=/root/serve
ExecStart=/bin/sh -c ‘${CELERY_BIN} multi start ${CELERYD_NODES} \
-A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
--logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}‘
ExecStop=/bin/sh -c ‘${CELERY_BIN} multi stopwait ${CELERYD_NODES} \
--pidfile=${CELERYD_PID_FILE}‘
ExecReload=/bin/sh -c ‘${CELERY_BIN} multi restart ${CELERYD_NODES} \
-A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
--logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}‘
[Install]
WantedBy=multi-user.target
mkdir conf.d
cd conf.d/
nano celery
内容如下:
CELERYD_NODES="w1 w2 w3"
CELERY_BIN="/usr/local/bin/celery"
CELERY_APP="serve"
CELERYD_MULTI="multi"
CELERYD_PID_FILE="/etc/celery/%n.pid"
CELERYD_LOG_FILE="/etc/celery/%n%I.log"
CELERYD_LOG_LEVEL=“INFO”
启动celery
systemctl daemon-reload
systemctl start celery
ps -ef|grep celery
systemctl enable celery
systemctl stop celery
安装配置uwsgi
pip install uwsgi
cd /root/
nano serve.ini
内容如下:
[Install]
WantedBy=multi-user.target
[email protected]:/etc/apt# nano /etc/conf.d/celery
[email protected]:/etc/apt# cat /root/serve.ini
[uwsgi]
chdir=/root/serve
socket=/var/run/serve.sock
chmod-socket=666
module=serve.wsgi:application
master=True
pidfile=/tmp/serve.pid
vacuum=True
max-requests=5000
processes = 4
daemonize=/var/log/uwsgi/serve.log
测试uwsgi是否正常
uwsgi -i /root/serve.ini
ps -ef |grep uwsgi
pkill -9 uwsgi
安装配置nginx
apt-get install nginx
cd /etc/nginx/sites-enabled/
nano court
注意要把server_name的ip地址改了
内容如下:
server {
listen 80;
server_name 172.16.146.133;
client_max_body_size 10m;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript;
location / {
uwsgi_pass unix:///var/run/serve.sock;
include uwsgi_params;
uwsgi_param UWSGI_SCHEME $scheme;
uwsgi_param SERVER_SOFTWARE nginx/$nginx_version;
}
location /static/ {
alias /root/serve/static/;
index index.html index.htm;
}
location /static/admin/ {
alias /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin$
index index.html index.htm;
}
charset utf-8;
}
systemctl restart nginx
ps -ef|grep nginx
卸载nginx(if necessary)
dpkg --get-selections|grep nginx
apt-get --purge remove nginx
apt-get --purge remove nginx-common
apt-get --purge remove nginx-core
find / -name "*nginx*"|rm -fr
数据库相关操作
service mysql start
mysql -u root -p
create database serve;
create user ‘serve‘@‘localhost‘ identified by ‘serve‘;
grant all privileges ON serve.* to ‘serve‘@‘localhost‘;
flush privileges;
exit;
在django生成数据库相关表
cd ~/serve/
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
启动服务
systemctl start celery
uwsgi -i /root/serve.ini
可能出现的问题
输入ip地址之后,可能样式会丢失,网上解决办法:
chmod -R 755 /root
这个方法个人觉得是有危险的,但是是最快解决问题的,没办法,谁叫你把项目放在root的家目录下
以上是关于Debian+Django+uWsgi+nginx+mysql+celery的主要内容,如果未能解决你的问题,请参考以下文章
Django + Uwsgi + Nginx 实现生产环境部署