服务器环境:
系统:Centos7.4 64bit
其他:Python3.6+virtualenv+MySQL5.7+Django2+uwsgi+Nginx+Supervisor+Memcached+redis
更改yum源镜像(非必需,阿里云服务器已更改好)
cd /etc/yum.repos.d
mv CentOS-Base.repo CentOS-Base.repo.bak
wget http://mirrors.aliyun.com/repo/Centos-7.repo
mv Centos-7.repo CentOS-Base.repo
yum clean all && yum makecache && yum update
pip3的源镜像修改(阿里云已生成好)
mkdir ~/.pip
vim ~/.pip/pip.conf
[global]
index-url = https://pypi.doubanio.com/simple/
[install]
trusted-host = pypi.doubanio.com
timeout = 6000
简化安装版本(阿里云)
安装python3
yum install epel-release -y
yum install https://centos7.iuscommunity.org/ius-release.rpm -y
yum install python36u -y
yum install python36u-devel -y
pip3 install --upgrade pip (建议关闭窗口或重启一下)
pip3 --version
安装`virtualenv
pip3 install virtualenv
pip3 install virtualenvwrapper
whereis virtualenvwrapper.sh
vim ~/.bashrc (尾部添加)
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
source ~/.bashrc
安装Git
yum install -y git
安装mysql
卸载原有数据库rpm -qa | grep mariadb
yum remove mariadb-libs-5.5.56-2.el7.x86_64
cd /usr/local/src
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server
systemctl start mysqld
systemctl status mysqld
grep "password" /var/log/mysqld.log
mysql -u root -p
重置密码:
set global validate_password_policy=0;
set global validate_password_length=1;
alter user 'root'@'localhost' identified by 'django12345';
flush privileges;
CREATE USER 'root'@'%' IDENTIFIED BY "django12345";
grant all privileges on *.* to root @"%" identified by "django12345" WITH GRANT OPTION;
flush privileges;
systemctl restart mysqld
systemctl status mysqld
vim /etc/my.cnf
# 修改字符编码为utf8
character_set_server = utf8
init_connect = 'SET NAMES utf8'
# 如果不需要密码策略,禁用密码策略
validate_password = off
解决安装mysqlclient报错:
yum install gcc mariadb-devel
安装memcached 和redis
yum install libevent libevent-devel
yum install memcached
systemctl start memcached
systemctl enable memcached
systemctl status memcached
yum install redis
systemctl start redis
systemctl enable redis
ps -ef | grep redis
本地生成需使用的pip环境文件:(cmd 在项目目录下)
pip3 freeze > requirements.txt
修改setting文件
访问域名或IP 以及数据库的信息
(本地Git命令)
全局设置:
git config --global user.name "xxxx"
git config --global user.email "xxxxx@xx.com"
创建git仓库
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/xxx/xxxx.git
git push -u origin master
进入虚拟环境:创建虚拟环境和文件夹:mkvirtualenv djangocs mkdir djangocs1
git init
git remote add origin https://gitee.com/xxx/xxx.git
git pull origin master
输入Gitee用户和密码:(不能输错,检查输入法)
pip3 install -r requirements.txt
python3 manage.py migrate
# 服务器manage.py文件的第一行代码应修改为:
#!/root/.virtualenvs/djangocs/bin/python3.6
python3 manage.py runserver 0.0.0.0:80
收集静态文件:
python3 manage.py collectstatic
退出虚拟环境 需要全局安装:
pip3 install uwsgi
cd ~
cd .virtualenvs/djangocs/
pwd
/root/.virtualenvs/djangocs
cd /srv/djangocs1/
uwsgi --http :80 --module djangocs1.wsgi --virtualenv=/root/.virtualenvs/djangocs
在项目目录下创建djangocs1_uwsgi.ini
vim djangocs1_uwsgi.ini 配置文件
[uwsgi]
#使用nginx连接时使用sock方式同机速度更快
socket=/srv/djangocs1/djangocs1.sock
#socket=172.16.0.4:9000
#不用nginx直接当做web服务器使用
#http=0.0.0.0:80
# Django相关1的配置
# 必须全部为绝对路径
# 项目的路径
chdir=/srv/djangocs1
#wsgi文件路径,在项目底下
wsgi-file=/srv/djangocs1/wsgi.py
# Django的wsgi文件
module= djangocs1.wsgi:application
# Python虚拟环境的路径
home=/root/.virtualenvs/djangocs
# 进程相关的设置
# 主进程
master=true
# 最大数量的工作进程
processes=4
threads=2
pidfile=/srv/djangocs1/uwsgi.pid
stats=/srv/djangocs1/uwsgi.status
# socket文件路径,绝对路径
# socket=/srv/djangocs1/djangocs1.sock
# 设置socket的权限
chmod-socket=666
# 退出的时候是否清理环境
vacuum=true
*************************************************
cd /
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum search nginx
yum install -y nginx
systemctl stop firewalld
cd /etc/nginx/conf.d
新建 djangocs1.conf (vim touch)
upstream djangocs1 {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
#server 172.16.0.4:9000; # uwsgi的端口
server unix:///srv/djangocs1/djangocs1.sock;
}
# configuration of the server
error_log /home/wwwroot/nginxerror.log;#错误日志
server {
# the port your site will be served on
listen 80;
# 端口
server_name xx.xx.xx.xx; # 服务器ip或者域名
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /srv/djangocs1/media; # 指向django的media目录
}
# Django static
location /static {
alias /srv/djangocs1/static_dist; # 指向django的static目录
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass djangocs1;
include /etc/nginx/uwsgi_params; # uwsgi服务
}
}
*******************************************************************
错误日志的路径需要先建立好 /home/wwwroot/nginxerror.log
mkdir /home/wwwroot
cd /lib/systemd/system/
vim nginx.service (阿里云已经配置好了 )
systemctl enable nginx.service
systemctl start nginx
systemctl status nginx (查看状态)
systemctl restart nginx
在项目路径下 重启 uwsgi
uwsgi --ini djangocs1_uwsgi.ini
pip3 install supervisor
在项目路径下创建
vim djangocs1_supervisor.conf
**********************************************
# upervisor的程序名字
[program:djangocs1]
# supervisor执行的命令
command = uwsgi --ini djangocs1_uwsgi.ini
# 项目的目录
directory = /srv/djangocs1
# 开始的时候等待多少秒
startsecs=0
# 停止的时候等待多少秒
stopwaitsecs=0
# 自动开始
autostart=true
# 程序挂了后自动重启
autorestart=true
# 输出的log文件
stdout_logfile=/srv/djangocs1/log/supervisord.log
# 输出的错误文件
stderr_logfile=/srv/djangocs1/log/supervisord.err
[supervisord]
# log的级别
loglevel=info
# 使用supervisorctl的配置
[supervisorctl]
# # 使用supervisorctl登录的地址和端口号
serverurl = unix:///srv/djangocs1/djangocs1_supervisorctl.sock
# # serverurl = http://127.0.0.1:9001
[unix_http_server]
file=/srv/djangocs1/djangocs1_supervisorctl.sock
[inet_http_server]
# supervisor的服务器
port = 127.0.0.1:9001
# 用户名和密码
username = djangoroot
password = django12345
# 登录supervisorctl的用户名和密码
username = djangoroot
password = django12345
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
**********************************************************************************************
在djangocs1下建立log文件夹
运行supervisord -c djangocs1_supervisor.conf
进入管理平台
supervisorctl -c djangocs1_supervisor.conf
平台管理指令:
* status # 查看状态
* start program_name #启动程序
* restart program_name #重新启动程序
* stop program_name # 关闭程序
* reload # 重新加载配置文件
* quit # 退出控制台