【CentOS-7.4】Django+Nginx+MySQL
Django,有两个主要 CMS:
(1)Django,https://www.djangoproject.com/
(2)DjangoCMS,
我们这里使用 Django
【升级】
yum update
yum upgrade
yum update
安装软件
yum install unzip
yum install tree
yum install htop
yum install tmux
【安装】nginx
# 添加 nginx 到 yum 源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
或者
vi /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
# 安装依赖
# gcc 是 linux 下的编译器
yum install gcc gcc-c++ -y
# pcre 是一个 perl 库,包括 perl 兼容的正则表达式库,nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要安装 pcre 库。
yum install pcre pcre-devel -y
# zlib 库提供了很多种压缩和解压缩方式 nginx 使用 zlib 对 http 包的内容进行 gzip
yum install zlib zlib-devel -y
# openssl 是 web 安全通信的基石,没有 openssl,可以说我们的信息都是在裸奔
yum install openssl openssl-devel -y
# 安装
yum install nginx
# 配置 nginx 开机启动
systemctl enable nginx
# 启动 nginx 服务
systemctl start nginx
# 查看 nginx 服务是否启动成功
ps -ef | grep nginx
【配置】https 访问
【配置】防火墙
# 查看防火墙【服务】状态
systemctl status firewalld
# 查看防火墙【运行】状态
firewall-cmd --state
# 开启
service firewalld start
# 重启
service firewalld restart
# 关闭
service firewalld stop
# 查询端口是否开放
firewall-cmd --query-port=8080/tcp
# 开放80端口
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=8080-8085/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp
# 查看防火墙的开放的端口
firewall-cmd --permanent --list-ports
# 重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload
如果是阿里云 ECS,则去【安全组】配置防火墙规则。
【配置】SElinux
# 临时关闭 SELinux
setenforce 0
# 临时打开 SELinux
setenforce 1
# 查看 SELinux 状态
getenforce
# 开机关闭 SELinux
# 编辑 /etc/selinux/config 文件,将 SELinux 的值设置为 disabled。
vi /etc/selinux/config
# 查看防火墙规则
firewall-cmd --list-all
【安装】MySQL-8
官方网站
https://dev.mysql.com/downloads/repo/yum/
# 下载 MySQL-8 源
wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
# 安装 yum repo 文件并更新 yum 缓存
rpm -ivh mysql80-community-release-el7-3.noarch.rpm
# 配置禁用 mysql5.7 的仓库,启用 mysql8.0 的仓库
yum install yum-utils -y
yum-config-manager --disable mysql57-community
yum-config-manager --enable mysql80-community
# 检查是否正确启用了仓库
yum repolist enabled | grep mysql
# 安装
yum install mysql-community-server
# 启动 MySQL 服务
systemctl start mysqld
# 查看 MySQL 密码
grep 'temporary password' /var/log/mysqld.log
# 输入密码
mysql -uroot -p
# 修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'UUUUXXXX';
exit
# 配置开机启动
systemctl enable mysqld
# 启动 MySQL 服务
systemctl start mysqld
【安装】 Node.js
下载
cd /usr/src && wget https://nodejs.org/dist/v12.13.1/node-v12.13.1-linux-x64.tar.xz
解压
xz -d node-v12.13.1-linux-x64.tar.xz
tar -xvf node-v12.13.1-linux-x64.tar
链接
ln -s /usr/src/node-v12.13.1-linux-x64/bin/node /usr/bin/node
ln -s /usr/src/node-v12.13.1-linux-x64/bin/npm /usr/bin/npm
验证
node -v
npm -v
安装依赖
cd /var/www/Django
更换 npm 源
npm config set registry https://registry.npm.taobao.org/
npm set sass_binary_site https://npm.taobao.org/mirrors/node-sass
查看 npm 配置
npm config get
安装
npm install --unsafe-perm
【安装】 python3
查看当前 python 版本
python
Python 2.7.5 (default, Aug 7 2019, 00:51:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
# 安装 python3
yum install python3 python3-devel
python3
Python 3.6.8 (default, Aug 7 2019, 17:28:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
【安装】 pip3
yum install python3-pip
【查看】pip 版本
pip -V
【安装】pipenv
pip install pipenv
【创建项目】
且在对应的目录下创建对于的虚拟环境
# 建立目项目录
mkdir -p /var/www/Django
# 进入项目目录
cd /var/www/Django
# 创建虚拟环境
pipenv install
Creating a virtualenv for this project…
Pipfile: /var/www/Django/Pipfile
Using /usr/bin/python3 (3.6.8) to create virtualenv…
⠼ Creating virtual environment...Already using interpreter /usr/bin/python3
Using base prefix '/usr'
No LICENSE.txt / LICENSE found in source
New python executable in /root/.local/share/virtualenvs/Django-O2aVMgS9/bin/python3
Also creating executable in /root/.local/share/virtualenvs/Django-O2aVMgS9/bin/python
Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file.
Installing setuptools, pip, wheel...
done.
✔ Successfully created virtual environment!
Virtualenv location: /root/.local/share/virtualenvs/Django-O2aVMgS9
Creating a Pipfile for this project…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (ca72e7)!
Installing dependencies from Pipfile.lock (ca72e7)…
▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
【配置】源
vi /var/www/Django/Pipfile
# [[source]]
# name = "pypi"
# url = "https://pypi.org/simple"
# verify_ssl = true
[[source]]
name = "pypi-tsinghua"
url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
verify_ssl = true
在 pipenv 虚拟机中安装 Django
cd /var/www/Django
本地安装
pipenv install -r requirements.txt
【或】网络安装
在 pipenv 虚拟机中安装 【Django】 项目其他 【whl】
pip install Django==3.0.2
Looking in indexes: https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/
Collecting Django==3.0.2
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/55/d1/8ade70e65fa157e1903fe4078305ca53b6819ab212d9fbbe5755afc8ea2e/Django-3.0.2-py3-none-any.whl (7.4 MB)
|████████████████████████████████| 7.4 MB 4.0 MB/s
Requirement already satisfied: pytz in /usr/local/lib/python3.6/site-packages (from Django==3.0.2) (2019.3)
Collecting asgiref~=3.2
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/a5/cb/5a235b605a9753ebcb2730c75e610fb51c8cab3f01230080a8229fa36adb/asgiref-3.2.3-py2.py3-none-any.whl (18 kB)
Collecting sqlparse>=0.2.2
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/ef/53/900f7d2a54557c6a37886585a91336520e5539e3ae2423ff1102daf4f3a7/sqlparse-0.3.0-py2.py3-none-any.whl (39 kB)
Installing collected packages: asgiref, sqlparse, Django
Successfully installed Django-3.0.2 asgiref-3.2.3 sqlparse-0.3.0
测试
python3
Python 3.6.8 (default, Aug 7 2019, 17:28:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
3.0.2
或
python3 -m django --version
测试成功
【用 Django 写第一个 app】
参考:
https://docs.djangoproject.com/en/3.0/intro/tutorial01/
https://docs.djangoproject.com/zh-hans/3.0/intro/tutorial01/
# 进入项目虚拟目录
cd /var/www/Django
# 在项目虚拟目录建立项目
django-admin startproject MyDjangoProject
ls
MyDjangoProject Pipfile Pipfile.lock
tree MyDjangoProject/
MyDjangoProject/
├── manage.py
└── MyDjangoProject
├── asgi.py
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
1 directory, 6 files
These files are:
- The outer
mysite/
root directory is a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like. - 这里我命名为:MyDjangoProject
manage.py
:- 一个让你用各种方式管理 Django 项目的命令行工具。你可以阅读 django-admin and manage.py 获取所有
manage.py
的细节。 - 里面一层的
mysite/
目录包含你的项目,它是一个纯 Python 包。它的名字就是当你引用它内部任何东西时需要用到的 Python 包名。 (比如mysite.urls
). mysite/__init__.py
:- 一个空文件,告诉 Python 这个目录应该被认为是一个 Python 包。如果你是 Python 初学者,阅读官方文档中的 更多关于包的知识。
mysite/settings.py
:- Django 项目的配置文件。如果你想知道这个文件是如何工作的,请查看 Django 配置 了解细节。
mysite/urls.py
:- Django 项目的 URL 声明,就像你网站的“目录”。阅读 URL调度器 文档来获取更多关于 URL 的内容。
mysite/asgi.py
:- An entry-point for ASGI-compatible web servers to serve your project. See How to deploy with ASGI for more details.
mysite/wsgi.py
:- 作为你的项目的运行在 WSGI 兼容的Web服务器上的入口。阅读 如何使用 WSGI 进行部署 了解更多细节。
【迁移项目】
生成 requirement.txt 文件
pipenv lock -r --dev > requirements.txt
上传项目压缩包至服务器目录
/var/www/Django
解压
unzip DjangoProject.zip
配置