CentOS7部署Django,nginx,uwsgi,redis
Posted 梁伟雄的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7部署Django,nginx,uwsgi,redis相关的知识,希望对你有一定的参考价值。
前期准备
把所有的软件都传到这个tools文件夹
cd ~
mkdir tools
cd tools/
mkdir /application
安装nginx
yum install pcre pcre-devel
yum install -y openssl openssl-devel
tar xf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.12.2/ --with-http_stub_status_module —with-http_ssl_module
make && make install
ln -s /application/nginx-1.12.2 /application/nginx
检查语法
/application/nginx/sbin/nginx -t
启动nginx
/application/nginx/sbin/nginx
curl 127.0.0.1
如果看到welcome to nginx说明nginx安装成功
修改nginx配置文件
cd /application/nginx/conf
vi nginx.conf
修改配置文件内容如下:
worker_processes 4;
#pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# 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/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
#include /etc/nginx/conf.d/*.conf;
include /application/nginx/extra/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachephpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
接着:
cd /application/nginx
mkdir extra
cd extra/
vi 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.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# 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;
#
# 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;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
# fastcgi_pass unix:/var/run/php5-fpm.sock;
#}
# 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;
# }
#}
接着:
vi court
内容如下:
erver {
listen 80;
server_name 172.16.146.135;
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/my_project.sock;
include uwsgi_params;
uwsgi_param UWSGI_SCHEME $scheme;
uwsgi_param SERVER_SOFTWARE nginx/$nginx_version;
}
location /static/ {
alias /root/my_project/static/;
index index.html index.htm;
}
location /static/admin/ {
alias /usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/;
index index.html index.htm;
}
charset utf-8;
}
接着:
检查语法
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload
安装mysql数据库
yum install -y libaio-devel
cd /root/tools/
tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz
mv mysql-5.5.32-linux2.6-x86_64 /application/mysql-5.5.32
ln -s /application/mysql-5.5.32 /application/mysql
cd /application/mysql
\cp support-files/my-small.cnf /etc/my.cnf
mkdir -p /application/mysql/data
groupadd mysql
useradd -s /sbin/nologin -g mysql -M mysql
chown -R mysql.mysql /application/mysql
vi /etc/hosts
在localdomain4后面添加你的主机名
不会?hostname
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 Aaron
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data —user=mysql
\cp support-files/mysql.server /etc/init.d/mysqld
sed -i ‘s#/usr/local/mysql#/application/mysql#g‘ /application/mysql/bin/mysqld_safe /etc/init.d/mysqld
/etc/init.d/mysqld start(这个是个人习惯,我习惯了centOS6.x,你也可以配置成用systemctl来启动)
/etc/init.d/mysqld stop
vi /etc/my.cnf
在[client]下面加上:
default-character-set=utf8
在[mysqld]下面加上:
character-set-server=utf8
在[mysql]下面加上:
default-character-set=utf8
有问题看日志 tail -50 Aaron.err
/etc/init.d/mysqld start
接着:
/application/mysql/bin/mysql -uroot
create database my_project;
create user ‘my_project‘@‘localhost‘ identified by ‘my_project‘;
grant all privileges on my_project.* to ‘my_project‘@‘localhost‘;
flush privileges;
exit;
配置celery
cd /usr/lib/systemd/system/
vi celery.service
内容如下:
[Unit]
Description=Celery Service
After=network.target
[Service]
Type=forking
#User=celery
#Group=celery
EnvironmentFile=-/etc/conf.d/celery
WorkingDirectory=/root/my_project
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 -p /etc/conf.d
cd /etc/conf.d
vi celery
内容如下:
CELERYD_NODES="w1 w2 w3"
CELERY_BIN="/usr/bin/celery"
CELERY_APP="my_project"
CELERYD_MULTI="multi"
CELERYD_PID_FILE="/etc/celery/%n.pid"
CELERYD_LOG_FILE="/etc/celery/%n%I.log"
CELERYD_LOG_LEVEL=“INFO"
接着:
其中CELERY_BIN=“/usr/bin/celery"这个路径要以which celery这个路径为准
systemctl start celery
systemctl stop celery
有报错,看systemctl status celery.service -l和日志
安装各种奇奇怪怪的东西
yum install pip -y
yum install python-pip -y
yum -y install epel-release
yum install python-pip -y(依赖于epel-release)
yum install -y python-devel(pip安装uwsgi需要这个)
pip install --upgrade pip
yum install mysql-devel(EnvironmentError: mysql_config not found)
把my_project项目拖进来到/root/下
cd my_project
vi settings
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.mysql‘,
‘OPTIONS‘: {
‘read_default_file‘: os.path.join(BASE_DIR, ‘mysql.conf‘),
‘unix_socket‘: ‘/tmp/mysql.sock‘,
},
}
}
添加
‘unix_socket‘: ‘/tmp/mysql.sock‘,
如上所示
接着:
pip install requirements.txt -r
安装uwsgi
vi /root/my_project.ini
内容如下:
[uwsgi]
chdir=/root/my_project
socket=/var/run/my_project.sock
chmod-socket=666
module=my_project.wsgi:application
master=True
pidfile=/tmp/my_project.pid
vacuum=True
max-requests=5000
processes = 4
daemonize=/var/log/uwsgi/my_project.log
接着:
mkdir /var/log/uwsgi/ -p
touch my_project.log
uwsgi -i /root/my_project.ini
ps -ef|grep uwsgi
pkill -9 uwsgi
安装redis
cd tools/
tar xf redis-4.0.2.tar.gz
cd redis-4.0.2
make MALLOC=jemallocmake PREFIX=/application/redis-4.0.2 install
ln -s /application/redis-4.0.2/ /application/redis
/application/redis/bin/redis-server
启动项目
cd my_project
python manage.py makemigrations
python manage.py migrate
/application/mysql/bin/mysql -uroot
python manage.py createsuperuser
getenforce
setenforce 0
systemctl stop firewalld.service
以上是关于CentOS7部署Django,nginx,uwsgi,redis的主要内容,如果未能解决你的问题,请参考以下文章
centos7下部署Django(nginx+uWSGI+Python3+Django)
centos7下采用Nginx+uwsgi来部署django
阿里云 centos7 django + uWSGI+Nginx + python3 部署攻略
Django 部署 CentOS7+Python3+Django+uWSGI+Nginx(含Nginx返回400问题处理防火墙管理)