Django+nginx+uwsgi+ubuntu18.04 website deployment

Django website deployment

  • Principles You
    have to talk about principles in everything. As shown in the figure, it is the whole principle. For details, you can watch Bilibili up's lecture, which is really good! ! !
网站用户
网站服务器
socket
uwsgi
Django

Picture source and network

Picture from (https://www.bilibili.com/video/BV1rx411272x)

  • Overview
购买服务器和域名
域名备案
开始部署
远程连接
环境配置
代码上传
完成部署
后期运维
  • Preparation Tool
    xFTP
    Xshell

  • Server purchase The
    editor used here is Alibaba Cloud's student server (after all, it is a big factory, the community is perfect, and the quality is no problem), the price is beautiful, and it is easy to use (Alibaba Cloud's cloud wing plan is still more conscientious). Introduce the server purchase process. Log in to Alibaba Cloud first , perform real-name authentication, and then perform student authentication .
    Insert picture description here
    After certification, you can participate in the Cloud Wing Project . After you enter, you will see this look.
    Insert picture description here
    Then select a server type to place an order and checkout. (
    You will have a place on the Internet from the moment you place an order) After that, you will receive a lot of mail from Ali. After you have your own server, click on the console... to Insert picture description here
    enter the cloud server ECS , in the instance you can see your public and private IP addresses. Insert picture description here
    Click Configure Rules.
    Insert picture description here
    Set your own security group rules. According to me, there should be no big problem. At this point, the purchase and setup of the server has ended.

  • Domain name purchase
    First of all, we have to clarify what a domain name is and the difference between it and url and ip address. Put a link below what is a domain name.

    明白了什么是域名之后,我们就开始购买域名。进入万网挑选自己的域名,点击购买,之后解析域名,绑定到自己服务器上。(这样说好像不太专业)由于小编已经解析号域名,没法给大家解释了,就放个链接给大家看一下域名解析
    域名解析好之后,我们需要申请一个免费的ssl证书(这个证书的作用后面再详细解释),ssl证书申请入口
    Insert picture description here
    购买之后,下载并保存相应服务器类型的证书
    Insert picture description here
    小编的系统的Nginx的,所以就下载了nginx的证书。
    下面是最最最重要的域名备案,我们国家所有的网站域名都必须进行备案,这个时间需要20多天,所以购买完域名之后一定尽快备案。备案入口
    备案完成之后便完成了购买云服务器和域名的全过程。

  • 远程连接

    • 方法一
      进入ECS控制台,点击实例->远程连接

Insert picture description here
之后会看到登录界面,用户名默认为root,密码为自己设置的密码。(如果没有设置密码,在更多->密码/密匙可自行设置)
Insert picture description here
点击确定就可以进入服务器系统。

  • 方法二
    利用远程连接工具xshell进行连接。首先打开fshell,点击文件->新建,出现下面这个页面。(主机一栏为你的公网ip地址
    Insert picture description here
    点击确定之后会让你输入你自己设置的密码(也就是登录阿里云远程连接的密码)。之后便可连接。进入服务器之后我们需要对我们现有的环境进行更新。
  • 更新python环境
    首先更新一下我们ubuntu的安装工具:
 sudo apt-get  update

Insert picture description here
由于ubuntu自带python2.7和3.6(默认python环境是2.7),如果不满足我们生产的需要,我们可以自行更新。

查看python默认环境
	python -V

安装python3.x环境

	sudo apt install python3.8 //安装3.8版本的python

修改ubuntu默认环境

	sudo rm /usr/bin/python //删除原有的软连接
	sudo ln -s /usr/bin/python3.5 /usr/bin/python    //建立新链接

此时再次查看python默认版本

	python -V

Insert picture description here

  • 安装Nginx
	sudo apt install nginx

安装完成后,用你的电脑浏览器访问你服务器的公网ip地址,看到如下界面,说明安装成功。
Insert picture description here
这也说明nginx部分已经和浏览器之间可以正常工作啦!!!(内心是不是很激动,其实我们还差的远呢)

  • 安装虚拟环境
    我们为什么要使用虚拟环境,我们的服务器上不可能就我们一个project,用虚拟环境隔开,防止混乱嘛。
    虚拟环境和Django的关系
    Insert picture description here
    安装虚拟环境
	sudo apt-get pip install virtualenv

下面我们就建立虚拟环境,,ubuntu的默认安装路径是/usr,我们换一个路径

	cd  /var/www/
	virtualenv your_env_name //建立虚拟环境
	source your_env_name/bin/activate//激活虚拟环境
	python -V//查看虚拟环境内的python环境是否和外部一样

到此我们已经搭建了虚拟环境并且激活了它。

  • 项目上传
    服务器的文件夹目录(使用xftp即可看到,登陆方法和xshell一样)
    Insert picture description here
  • 安装项目所需要的库
    在本地环境下,进入到你项目所在的目录,执行命令导出我们项目用到的包
	pip freeze > requiremnets.txt

此时在你本地的项目目录下,就会多出一个requirements.txt文件。记事本打开就能看到我们所用的库。
Insert picture description here
然后将此文件上传到服务器项目目录下(即和manage.py在同一个目录)
执行命令即可安装所用项目用到的包

	pip install -r requirements.txt

此时运行项目

		python manage.py runserver 0.0.0.0:8000//端口为8000

在浏览器上输入你的公网ip+:8000(端口号),即可访问您的项目。(如果运行失败,请检查自己的8000端口是否放行或仔细检查自己项目是否有错)

  • 安装uwsigi
    概念:Web服务器面向外部世界。它可以直接从文件系统提供文件(HTML,图像,CSS等)。但是,它无法直接与Django应用程序对话;它需要一些东西来运行应用程序,从Web客户端(例如浏览器)获取请求并返回响应。Web服务器网关接口WSGI可以完成此任务。WSGI是Python标准。uWSGI是WSGI实现。在本教程中,我们将设置uWSGI,以便它创建Unix套接字,并通过uwsgi协议将响应提供给Web服务器。最后,我们完整的组件堆栈如下所示:
网站用户
网站服务器
socket
uwsgi
Django

安装uwsgi

	sudo apt-get install python-dev//这一步很重要
	sudo pip3 install uwsgi

执行uwsgi命令

	uwsgi --http :8000 --module xxx.wsgi//xxx为你含wsgi.py的目录名称

此时浏览器输入你的公网ip+:8000即可访问

  • 配置nginx.conf
    输入
	cd /etc/nginx
	vim nginx.conf #vim打开nginx配置文件

你会看到
Insert picture description here
(可能你的和我的不一样,按我这个改就可以了,server_name哪里改成自己的域名,还有的同学可能不会vim,请自行百度)

  • 配置uwsgi.ini
	cd /var/www/
	vim uwsgi.ini

Copy these following codes into

  • Add uwsgi_params
	cd /var/www/
	vim uwsgi_params

Copy these following codes into

  • Install uwsgi outside the virtual environment
	cd ..
	deactivate #退出虚拟环境
	pip install uwsgi
  • Set boot up
  1. Copy command (set startup parameters)
	vim /lib/systemd/system/rc.local.service 

2. Copy the code (press ESC for the following code and then: wq enter)

	[Unit]
	Description=/etc/rc.local Compatibility
	Documentation=man:systemd-rc-local-generator(8)
	ConditionFileIsExecutable=/etc/rc.local
	After=syslog.target network.target remote-fs.target nss-lookup.target
	 
	[Service]
	Type=forking
	ExecStart=/etc/rc.local start
	TimeoutSec=0
	RemainAfterExit=no
	GuessMainPID=no
	 
	#这一段原文件没有,需要自己添加
	[Install]
	WantedBy=multi-user.target
	Alias=rc-local.service
 

3. Copy the command (set up the soft connection, start up and go back to /etc/... this directory to find the file)

	ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/rc.local.service
  1. Copy command (there is no rc.local file itself, and then God said that if you want to have it, there will be)
	vim /etc/rc.local
  1. Copy command (add execute permission)
	sudo chmod +x /etc/rc.local

6. Copy the code (write commands to this script for scripts, services or other operations to be started)

	#!/bin/bash //这点很重要,一定不要漏掉
	echo "hello" > /etc/test.log
	/etc/init.d/webserver start
	exit 0

7. Just restart.

(Article reference: https://blog.csdn.net/idiot_qi/article/details/86527052)

(To be continued...)

Guess you like

Origin blog.csdn.net/m0_43456002/article/details/105059322