django部署
程序员文章站
2022-06-11 11:38:09
...
安装环境
centos7.4+新建一个非root用户,权限不够的地方添加sudo。
1.安装nginx
sudo yum install nginx
# 启动nginx
sudo systemctl start nginx
# 查看nginx进程
ps aux|grep nginx
# 关闭nginx进程
sudo kill -QUIT 进程号
sudo pkill -f nginx
# 启动(centos建议使用这种方式)
/usr/sbin/nginx
2.安装MySQL
# 下载mysql源安装包
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
# 安装mysql源
sudo yum localinstall mysql57-community-release-el7-8.noarch.rpm
# 检查mysql源是否安装成功
yum repolist enabled | grep "mysql.*-community.*"
# 安装
sudo yum install mysql-community-server
# 启动MySQL
sudo systemctl start mysqld
# 查看MySQL的启动状态
systemctl status mysqld
# 开机启动
sudo systemctl enable mysqld
sudo systemctl daemon-reload
# 查看默认密码
grep 'temporary password' /var/log/mysqld.log
# 进入MySQL
mysql -uroot -p
set password for 'root'@'localhost'=password('MyNewPass4!');
# 修改编码
show variables like '%character%';
# 修改/etc/my.cnf配置文件
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
# 重启MySQL
sudo systemctl restart mysqld
# 添加远程登录用户
GRANT ALL PRIVILEGES ON *.* TO 'yangxin'@'%' IDENTIFIED BY 'Yangxin0917!' WITH GRANT OPTION;
# 关闭strict mode
# 修改/etc/my.conf
sql-mode= NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
3.安装gcc、安装Python3及相关依赖
# 安装gcc
sudo yum install gcc-c++
# 安装zlib相关依赖包
sudo yum -y install zlib*
# 安装 openssl-devel
sudo yum install openssl-devel
# 源码安装Python3
wget 源码 或 直接上传
./configure --with-ssl --prefix=/usr/local/python3
make
sudo make install
安装完Python后,将Python3下bin目录添加到环境变量中
# 修改.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
创建软连接
ln -s /usr/local/bin/python3.6 /usr/bin/python3
source ~/.bash_profile
4.安装virtualenv
sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper
# 找不到pip就用全路径
sudo /usr/local/python3/bin/pip3 install virtualenv
sudo /usr/local/python3/bin/pip3 install virtualenvwrapper
# 修改.bash_profile
# 添加Python3 bin目录到path下
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export WORKON_HOME=$HOME/virtualenvs
VIRTUALENVWRAPPER_PYTHON=/usr/local/python3/bin/python3
export VIRTUALENVWRAPPER_PYTHON
source ~/.bash_profile
sudo find / -name virtualenvwrapper.sh
source /usr/local/python3/bin/virtualenvwrapper.sh
# 创建虚拟环境
mkvirtualenv test1
# 生成Python库列表
pip freeze > requirements.txt
# 服务器端安装
pip install -r requirements.txt
如果安装太慢,可以添加豆瓣源
pip install -i https://pypi.douban.com/simple 安装库名称==2.0.3
# 示例
pip install -i https://pypi.douban.com/simple django==2.0.3
5.安装uwsgi
pip install uwsgi
# 以uwsgi方式启动
uwsgi -i uwsgi.ini
将本地数据库传输到服务器端。
create database basename;
6.项目简单测试
settins.py ALLOWED_HOSTS
中添加IP地址
python manage.py runserver 0.0.0.0:80000
# 尝试访问网站,应该能正常显示运行
# 测试uwsgi
uwsgi --http :8000 --module XXX.wsgi
# 这步测试,静态文件应该还没加载上
7.配置nginx
项目下新建/conf/nginx/my_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
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
}
}
创建软链接
sudo ln -s /XXX/conf/nginx/uc_nginx.conf /etc/nginx/conf.d/
8.拉取所有的静态文件
#settings.py中,添加
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
#运行命令
python manage.py collectstatic
运行nginx
sudo /usr/sbin/nginx
9.通过配置文件启动uwsgi
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = 项目的全路径
# Django's wsgi file
module = 项目内wsgi的位置.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 = virtualenv的全路径
logto = /tmp/mylog.log
centos存在权限问题(13: Permission denied)
需要修改/etc/nginx/nginx.conf
文件,将user nginx;
改为user root;
settins.py
中 DEBUG = False
启动uwsgi
uwsgi -i 全路径/uwsgi.ini &
关闭uwsgi
pkill -f -9 uwsgi
访问
http://你的ip地址/
其他
# 在A服务器文件传输到B:
scp -r A待传文件夹 [email protected]的IP:存放路径
# 修改MySQL密码
set password for [email protected] = password('123');
# 清除known_hosts里的旧缓存
ssh-****** -R "你的远程服务器ip地址"
# linux创建新用户
useradd jack
passwd jack
# root用户编辑/etc/sudoers文件
# root ALL=(ALL) ALL下添加一行
jack ALL=(ALL) ALL
# iterm2传输文件需在服务器端安装lrzsz
sudo yum install -y lrzsz
# nginx日志默认位置
/var/log/nginx