第六步:网络验证服务器端项目上线部署
功夫不负有心人,终于部署成功啦!
前期准备:
项目名:netauth
系统:百度云服务器下的Ubuntu16.4
软件:xshell(无论如何想办法用这个跟服务器实例连上,不要用远程连接干敲,不然你会有砸电脑的冲动)
Navicat(用于上传数据库)
FileZille(通过这个软件,将项目的zip包传到服务器系统下)
apt install unzip
unzip netauth.zip
rm netauth.zip
1.安装MySQL数据库
1.安装mysql
apt-get install mysql-server -y#设置密码:(不显示)
apt-get install mysql-client
apt-get install libmysqlclient-dev -y
2.配置远程连接权限
vim /etc/mysql/mysql.conf.d/mysqld.cnf#英文输入环境下,敲 i 键,进入插入模式#上下左右键移动光标,注释掉 bind-address = 127.0.0.1#英文输入环境下,敲Esc键,输入:wq 保存退出
mysql -u root -p#输入密码(不显示),登录mysql
GRANT ALL PRIVILEGES ON*.* TO 'root'@'%' IDENTIFIED BY '123456'WITH GRANT OPTION;
flush privileges;
quit
3.远程连接数据库
#用Navicat链接服务器数据库,如果报错:
fuser -k 3306/tcp#将客户端数据库通过数据传输,传输到服务器
#看一下数据库传上来了没
mysql -u root -p
show databases;
quit
4.上传完了,别忘了把远程连接权限关了
vim /etc/mysql/mysql.conf.d/mysqld.cnf#英文输入环境下,敲 i 键,进入插入模式#上下左右键移动光标,去掉 bind-address = 127.0.0.1这行的注释#英文输入环境下,敲Esc键,输入:wq 保存退出
2.安装python3.6
add-apt-repository ppa:fkrull/deadsnakes
#回车
apt-get install python-software-properties -y
apt-get update
apt-get install python3.6-dev python3.6-venv -y
apt install python-pip -y
3.配置虚拟环境
1.安装和进入虚拟环境
python3.6 -m venv env36#在当前目录下,创建一个python3.6的虚拟环境,取名为env36,目前自带虚拟环境只支持到3.6.2
. env36/bin/activate#进入虚拟环境#退出虚拟环境 deactivate
2.配置虚拟环境
cd netauth
pip3 install-r requirments.txt#安装相关依赖包,其中requirments.txt,是在客户端时,通过pip freeze > requirments.txt获得#这个过程也许需要多等一会,比较low一点的云服务就慢一些,亲身体验,bat里,百度的最慢
3.修改settings.py
cd netauth
vim settings.py#进入settings.py文件中,点击i键,变为编辑模式#通过上下左右键移动光标
ALLOWED_HOSTS = ['公网ip']#在最底追加
STATIC_ROOT=os.path.join(BASE_DIR,"/static/")#然后敲Esc键,输入:wq 保存退出
4.测试一下项目能否运行
cd ..
python manage.py runserver0.0.0.0:8000
#浏览器访问:公网ip+8000#发现啥都有啦!
4.虚拟环境中安装uwsgi
1.安装
cd ~pip3 install uwsgi
2.检测是否安装成功
cd netauth
uwsgi--http :8000 --module netauth.wsgi#通过uwsgi启动项目,这一步必须在项目目录下操作执行#ctrl+C键关闭项目服务状态#fuser -k 8000/tcp#关闭8000端口占用
5.安装配置nginx
1.安装nginx
cd ..
apt install nginx -y
2.配置uwsgi与nginx连接
#在项目目录下执行,注意后面这个点儿别落下
cd netauth
cp/etc/nginx/uwsgi_params .#回到~目录下
cd ..
3.配置uwsgi.ini
vim uwsgi.ini#进入uwsgi.ini,然后在英文输入下敲i键,进入插入模式,将以下内容填充进去(通过敲i键进入插入模式这一步不能省略,不然粘贴进去的内容会部分丢失)
[uwsgi]
chdir=/root/netauth
module=netauth.wsgi:application
home=/root/env36
master=true
processes=10socket=:8001chmod-socket = 666vacuum=true#然后在英文输入下,Esc键,然后输入冒号wq+回车,保存退出。
4.配置nginx
#cd 到项目目录下
vim netauth_nginx.conf#新建进入了nginx.conf文件#在英文输入法下,敲i键,进入可插入模式#粘贴以下内容#mysite_nginx.conf
#the upstream component nginx needs to connect to
upstream django {#server unix:/root/netauth/netauth.sock; # for a file socket
server 127.0.0.1:8001; #for a web port socket (we'll use this first)
}#configuration of the server
server {#the port your site will be served on
listen 8000;#the domain name it will serve for
server_name .example.com; #substitute your machine's IP address or FQDN
charset utf-8;#max upload size
client_max_body_size 75M; #adjust to taste
#Django media
location /media {
alias/root/netauth/media; #your Django project's media files - amend as required
}
location/static {
alias/root/netauth/static; #your Django project's static files - amend as required
}#Finally, send all non-media requests to the Django server.
location /{
uwsgi_pass django;
include/root/netauth/uwsgi_params; #the uwsgi_params file you installed
}
}
5.建立软链接
sudo ln -s /root/netauth/netauth_nginx.conf /etc/nginx/sites-enabled/
6.收集静态文件
#cd 到项目目录下执行
python manage.py collectstatic#yes
6.最后测试
#修改settings.py中
DEBUG =False
cd~service nginx restart#重启nginx#启动uwsgi
uwsgi --ini uwsgi.ini#浏览器访问http://180.76.143.11:8000