Apache+Flask在云服务器搭建个人网站


持有云服务器的前提下,基于Flask开发简易个人网站并使用Apache部署,而且允许外界通过域名访问。


一、服务器


1.在Vultr官网部署一台符合需求的服务器,我仅仅为了演示demo,所以选的配置如下:




2.部署服务器之后,通过IP和账号密码连接云服务器

  • Windows使用FinalShell,以下是FinalShell的下载地址。
    链接:https://pan.baidu.com/s/1GZ7UsJKJOXOl7QKlTS1O0w
    提取码:4iug

  • MacOS直接使用Shell来连接。

    ssh root@1.2.3.4
    

3.通过服务器的IP、用户名和密码来连接服务器,本文使用IP:1.2.3.4进行演示



4.为了外界能够直接访问个人网站,需要配置防火墙规则

sudo ufw enable
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

二、域名


1.可以在各大域名网站选择自己的域名,我在Namesilo官网选了一个域名,演示用a.com代替。


2.在Namesilo官网配置好两条A记录,使域名www.a.com和a.com能够解析到服务器IP:1.2.3.4上。(温馨提示:为了网站的安全性,可以选择Cloudflare的DNS服务,但Cloudflare提供SSL/TLS加密模式一定不能选灵活,否则访问域名会出现重定向次数过多的问题)



三、配置谷歌OAuth2.0


1.参考《两种方式使用Gmail发送邮件:OAuth2.0或应用专用密码》先后创建Project、OAuth同意屏幕以及OAuth 2.0 Client ID,但Client的应用类型选择Web application


2.在Authorized JavaScript origins添加三个URI: http://localhost、http://localhost:7777和https://www.a.com,创建完毕即可获得Client ID

注: 前两个URI用于本地调试,第三个URI则填自己真实的域名。


四、配置环境


1.编辑sources列表

cp /etc/apt/sources.list /etc/apt/sources.list.bak
vim /etc/apt/sources.list

添加以下内容

deb https://deb.debian.org/debian buster main contrib non-free
deb-src https://deb.debian.org/debian buster main contrib non-free

deb https://debian.mirror.constant.com buster main contrib non-free
deb-src https://debian.mirror.constant.com buster main contrib non-free

deb https://deb.debian.org/debian-security/ buster/updates main contrib non-free
deb-src https://deb.debian.org/debian-security/ buster/updates main contrib non-free

deb https://deb.debian.org/debian buster-updates main contrib non-free
deb-src https://deb.debian.org/debian buster-updates main contrib non-free

2.服务器需要安装Python3.7、Mariadb和Git。以下是安装命令:

# 更新软件包列表
sudo apt update

# 安装Python3.7
sudo apt install python3.7 virtualenv

# 安装MySQL
sudo apt install mariadb-server

# 安装Git
sudo apt install git

3.配置MySQL

# 启动MySQL服务
sudo systemctl start mysql

# 启动安装向导
sudo mysql_secure_installation

# 使用root身份登录数据库
mysql -u root -p

# 创建数据库新用户
CREATE USER 'user'@'localhost' IDENTIFIED BY 'User.5555';

# 创建DB
CREATE DATABASE web_db;

# 授予用户权限
GRANT ALL PRIVILEGES ON web_db.* TO 'user'@'localhost';

# 刷新权限并退出数据库
FLUSH PRIVILEGES;
EXIT

4.下载源码并进入项目目录

git clone https://github.com/qinhj5/web-app.git
cd web-app

5.创建虚拟环境和安装依赖

virtualenv --python=python3.7 venv
source venv/bin/activate
pip3.7 install -r requirements.txt

6.编辑配置文件

  • client.json: 填入上面创建的Client ID
    cp config/client.json config/client.json.bak
    vim config/client.json
    
    {
      "client_id": ""
    }
    
  • script.js: 同样地修改client_id字段,填入上面创建的Client ID
    cp static/script.js static/script.js.bak
    vim static/script.js
    
  • mysql.json:填入上面MySQL数据库连接信息
    cp config/mysql.json config/mysql.json.bak
    vim config/mysql.json
    
    {
      "host": "localhost",
      "port": 3306,
      "user": "user",
      "password": "User.5555",
      "database": "web_db"
    }
    
  • secret.json:填入一个随机字符串用于Flask加密
    cp config/secret.json config/secret.json.bak
    vim config/secret.json
    
    {
      "key": ""
    }
    

7.创建数据库表格和初始化数据库记录

python3.7 cli.py --option create
python3.7 cli.py --option initialize

注:脚本会初始化十二位虚拟用户


五、启动服务


1.安装 Apache 服务器以及依赖模块

sudo apt install apache2 libapache2-mod-wsgi-py3

2.为a.com获取 Let’s Encrypt 免费证书

sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d a.com -d www.a.com

根据向导配置完成后会提示fullchain.pem(完整证书链)和 privkey.pem(私钥)的路径,通常在以下位置:
/etc/letsencrypt/live/a.com/fullchain.pem
/etc/letsencrypt/live/a.com/privkey.pem


3.移动项目到部署目录,并授权给www-data

cp -r ../web-app /var/www
sudo chown -R www-data:www-data /var/www/web-app

4.编写 Apache 虚拟主机配置文件

vim /etc/apache2/sites-available/web-app.conf

输入以下内容,其中私钥、完整证书链和域名需要修改:

<VirtualHost *:80>
    ServerName a.com
    ServerAlias www.a.com
    Redirect permanent / https://a.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName a.com
    ServerAlias www.a.com
	
    DocumentRoot /var/www/web-app
	
    SSLEngine on
    SSLProtocol all -SSLv2 -SSLv3
    SSLCertificateFile /etc/letsencrypt/live/a.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/a.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf

    WSGIDaemonProcess web-app python-home=/var/www/web-app/venv processes=2 threads=4 user=www-data group=www-data home=/var/www/web-app
    WSGIProcessGroup web-app

    WSGIScriptAlias / /var/www/web-app/app.wsgi

    <Directory /var/www/web-app>
        WSGIProcessGroup web-app
        WSGIApplicationGroup %{GLOBAL}
        Require all granted
        WSGIPassAuthorization On
        Header set Access-Control-Allow-Origin "*"
        Header set Access-Control-Allow-Methods "POST"
        Header set Access-Control-Allow-Headers "Content-Type, Authorization"
    </Directory>

    ErrorLog /var/log/apache2/web_error.log
    CustomLog /var/log/apache2/web_access.log combined
</VirtualHost>

5.关闭Apache默认的虚拟主机配置文件

sudo a2dissite 000-default.conf
sudo a2dissite 000-default-le-ssl.conf

6.开启Apache的ssl和headers模块

sudo a2enmod ssl
sudo a2enmod headers

7.启用自定义的虚拟主机配置文件并重启Apache服务

sudo a2ensite web-app.conf
sudo systemctl reload apache2

六、网站演示


此时即可通过域名https://www.a.com或者https://a.com 访问个人网站,而且能够通过Google账号登录个人网站





至此教程结束,完善的个人网站需要更多开发工作,本文只提供了一个简单的Demo。

有错误或者改进的地方请各位积极指出!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值