09搭建BBS博客系统Mysql

搭建BBS博客系统Mysql

一、安装mysql数据库

1、安装

[root@db01 ~]# yum install mariadb* -y

2、启动

[root@db01 ~]# systemctl start mariadb

3、测试链接

[root@db01 ~]# mysql

  Welcome to the MariaDB monitor. Commands end with ; or \g.
  Your MariaDB connection id is 3
  Server version: 5.5.68-MariaDB MariaDB Server

  Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  MariaDB [(none)]>

4、创建远程链接用户

MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

5、刷新权限

MariaDB [(none)]> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

6、创建一个数据库

[root@db01 ~]# mysql -uroot -h172.16.1.51
MariaDB [(none)]> create database django;
Query OK, 1 row affected (0.00 sec)

7、查看数据库

MariaDB [(none)]> show databases;

  +--------------------+
  | Database           |
  +--------------------+
  | information_schema |
  | django             |
  | mysql             |
  | performance_schema |
  | test               |
  +--------------------+
  5 rows in set (0.00 sec)

注意:删除数据库不要随意执行

MariaDB [(none)]> drop database django;
Query OK, 5 rows affected (0.00 sec)

二、配置Django + 数据库

1、上传bbs代码至 [root@web01 0pt] 内

2、修改配置

django 链接 数据库的配置


[root@web01 bbs]# pwd
/opt/bbs
[root@web01 bbs]# vim BBS/settings.py
ALLOWED_HOSTS = ['*']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django',
'HOST': '172.16.1.51',
'PORT': 3306,
'USER': 'root',
'PASSWORD': '123456',
'CHARSET': 'utf8'
}
}

3、数据库迁移

[root@web01 bbs]# pwd
/opt/bbs

# 安装对应数据库操作包
[root@web01 bbs]# pip3 install pymysql -i https://pypi.douban.com/simple/
[root@web01 bbs]# pip3 install --upgrade pip
[root@web01 bbs]# pip3 install pillow -i https://pypi.douban.com/simple/
[root@web01 bbs]# pip3 install bs4


# 创建数据库迁移文件
[root@web01 bbs]# python3 manage.py makemigrations

  Migrations for 'app01':
    app01/migrations/0001_initial.py
       - Create model UserInfo
       - Create model Article
       - Create model Blog
       - Create model UpAndDown
       - Create model Tag
       - Create model Comment
       - Create model Category
       - Create model Article2Tag
       - Add field blog to article
       - Add field category to article
       - Add field tags to article
       - Add field blog to userinfo
       - Add field groups to userinfo
       - Add field user_permissions to userinfo


# 使用数据库迁移文件迁移数据库
[root@web01 bbs]# python3 manage.py migrate

4、测试

[root@web01 bbs]# python3 manage.py runserver 0.0.0.0:8000

浏览器输入:192.168.15.7:8000

# 添加一个默认的页面(没有默认的界面时执行该操作)
[root@web01 bbs]# pwd
/opt/bbs
[root@web01 bbs]# vim BBS/urls.py
url('', views.home),

三、Nginx代理Django

1、配置uwsgi

[root@web02 BBS18_day01]# vim myuwsgi.ini 
[uwsgi]
# 端口号
socket = :8000
# 指定项目的目录
chdir = /opt/bbs
# wsgi文件路径
wsgi-file = BBS/wsgi.py
# 模块wsgi路径
module = BBS.wsgi
# 是否开启master进程
master = true
# 工作进程的最大数目
processes = 4
# 结束后是否清理文件
vacuum = true


#执行下面命令,无报错后执行后台运行
[root@web01 bbs]# uwsgi --ini myuwsgi.ini
[root@web01 bbs]# uwsgi -d --ini myuwsgi.ini

#查看uwsgi是否开启进程
ps -ef | grep uwsgi

2、配置Nginx + uwsgi

[root@web01 bbs]# cd /etc/nginx/conf.d
[root@web01 conf.d]# cat bbs.conf
# 配置一个网站
server {
# 监听的端口
listen 80;
# 配置域名
server_name bbs.test.com;
# 配置路径
location / {
# 加载Nginx代理uwsgi的配置项
include uwsgi_params;
# 指定uwsgi的访问地址
uwsgi_pass 127.0.0.1:8000;
# 连接uwsgi的超时时间
uwsgi_read_timeout 2;
# 自定义uwsgi代理项目的路径及配置项
uwsgi_param UWSGI_SCRIPT BBS.wsgi;
# 指定python项目的路径
uwsgi_param UWSGI_CHDIR /opt/bbs;
# 索引文件
index index.html index.htm;
# 客户端上传文件的最大值
client_max_body_size 35m;
}
}


#重启nginx
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx

#windows配置域名
192.168.15.7 bbs.test.com

四、Nginx多站点

1、创建数据库
MariaDB [(none)]> create database bbs;

2、上传代码
3、修改配置
4、数据库迁移
5、测试
6、配置uwsgi
7、重启nginx

 

 

 

 

posted @ 2021-11-06 20:29  vonmo  阅读(44)  评论(0编辑  收藏  举报