树莓派构建内网域名访问php/python 多个虚拟web主机 bind9 nginx flask uwsgi pfm php7 python3

0 前言

一个小型的项目,需要用php做简单访问业务,python做另一个与自动控制设备相关远程控制。

所以需要在一个树莓派上实现基于php和python的web。采用php7和python3,web服务采用nginx,python框架采用flask+uwsgi. php web网站的端口号分别为80,81,而python flask为82.

其中具体web、IP与端口配置如下表。

编号web名称配置网址IP地址端口号
1web1php7+fpmweb1.com192.168.0.2080
2web2php7+fpmweb2.com192.168.0.2081
3web3php7+fpmflask.com192.168.0.2082

为方便内网访问,采用bind9进行DNS域名解析到IP,再由nginx进行端口转发(配置bind9有些问题还需要更新)。

下面将一一详细介绍其配置过程。

树莓派 4B 版本信息如下:

pi@raspberrypi:~ $ lsb_release  -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 10 (buster)
Release:        10
Codename:       buster
pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.19.118-v7l+ #1311 SMP Mon Apr 27 14:26:42 BST 2020 armv7l GNU/Linux
pi@raspberrypi:~ $

路由器:中兴 其中设置树莓派固定IP为192.168.0.20(个人经验——在树莓派上手动指定IP不是很可靠,路由器上设置更可靠),具体如下图:
在这里插入图片描述

windows 10 专业版
ssh工具:putty(64-bit)
ftp工具:FileZilla

1 php安装

PHP5逐步更新到PHP7,现以PHP7.3为例子安装。

sudo apt-get update
sudo apt install -y -t buster php7.3-fpm php7.3-curl php7.3-gd php7.3-intl php7.3-mbstring php7.3-mysql php7.3-imap php7.3-opcache php7.3-sqlite3 php7.3-xml php7.3-xmlrpc php7.3-zip 

安装完成后,查看php版本

pi@raspberrypi:~ $ php -v
PHP 7.3.14-1~deb10u1 (cli) (built: Feb 16 2020 15:07:23) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.14, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.14-1~deb10u1, Copyright (c) 1999-2018, by Zend Technologies

2 nginx安装

sudo apt-get install nginx

安装完成后,在浏览器输入树莓派的IP地址(例如192.168.0.20),可以看到“Welcome to nginx!”。

3 python3安装

安装python3

sudo apt install python3

卸载 python2.7 和其配套连接与依赖(可选)

sudo apt remove python
sudo apt autoremove
sudo rm /usr/bin/python

新建与python3.7 依赖

sudo ln -s /usr/bin/python3.7 /usr/bin/python

查看直接调用python结果

pi@raspberrypi:~ $ python
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

若默认出现python3.7版本,表明安装成功。

4 php web设置与测试

php web方面需要建立2个单独网站,即2个虚拟主机,其端口号分别为80和81。需要进行nginx的相关设置。
nginx设置可以通过修改/etc/ngin目录下的nginx.conf文件,也可以通过修改/etc/nginx/site-available下面default文件进行配置。

个人推荐在/etc/nginx/site-available/default进行配置,具体如下:

sudo vim /etc/nginx/site-available/default
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
##网站1的设置,网站1 的端口为80,目录为web1
server {
 listen 80 default_server;# 网站1的端口
 #listen [::]:80 default_server;
 # SSL configuration
 #
 # listen 443 ssl default_server;
 # listen [::]:443 ssl default_server;
 #
 # Note: You should disable gzip for SSL traffic.
 # See: https://bugs.debian.org/773332
 #
 # Read up on ssl_ciphers to ensure a secure configuration.
 # See: https://bugs.debian.org/765782
 #
 # Self signed certs generated by the ssl-cert package
 # Don't use them in a production server!
 #
 # include snippets/snakeoil.conf;
 root /var/www/web1;#网站1的目录
 # Add index.php to the list if you are using PHP
 index index.html index.php index.htm index.nginx-debian.html;# 网站1的文件访问顺序
 server_name web1.com;#网站1域名,树莓派内部能访问,内网其它客服机需要修改hosts文件中ip与域名对应才能用网页输入域名访问
 location / {
  # First attempt to serve request as file, then
  # as directory, then fall back to displaying a 404.
  #try_files $uri $uri/ =404;
  index index.html index.htm index.php default.html default.htm default.php;# 网站1的文件访问顺序
 }
 # pass PHP scripts to FastCGI server
 #
 location ~ \.php$ {
 # include snippets/fastcgi-php.conf;
 #
 # # With php-fpm (or other unix sockets):
  fastcgi_pass unix:/run/php/php7.3-fpm.sock; #其为关键点,根据php版本填写,要是php为7.3版本,填写成php7.0-fpm将会出错。
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                 include fastcgi_params;
 # # With php-cgi (or other tcp sockets):
 # fastcgi_pass 127.0.0.1:9000;
 }
 # deny access to .htaccess files, if Apache's document root
 # concurs with nginx's one
 #
 #location ~ /\.ht {
 # deny all;
 #}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
# 虚拟主机样例
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
#  try_files $uri $uri/ =404;
# }
#}
##网站2的设置,网站2 的端口为81,目录为web2
server {
  listen 81;
# listen [::]:81;
#
  server_name web2.com; #网站2的域名,树莓派内部能访问,内网其它客服机需要修改hosts文件中ip与域名对应才能用网页输入域名访问
#
  root /var/www/web2; #网站2目录
  index index.html index.htm index.nginx-debian.html; #网站2访问文件顺序
#
  location / {
#   try_files $uri $uri/ =404;
  index index.html index.htm index.php default.html default.htm default.php;#网站2文件顺序
  }
 location ~ \.php$ {
 # include snippets/fastcgi-php.conf;
 #
 # # With php-fpm (or other unix sockets):
  fastcgi_pass unix:/run/php/php7.3-fpm.sock; #其为关键点,根据php版本填写,要是php为7.3版本,填写成php7.0-fpm将会出错。
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                 include fastcgi_params;
 # # With php-cgi (or other tcp sockets):
 # fastcgi_pass 127.0.0.1:9000;
 }
 }


按小写“i”键进行修改,主要是端口号,web目录,添加调用php-fpm。设置好后按“ESC”键,输入“:wq”推出即可。

进行nginx重启和测试web服务是否能访问

pi@raspberrypi:~ $ sudo nginx -t && sudo service nginx reload
nginx: [warn] conflicting server name "web1.com" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

有一个警报,但是不影响其运行。注意nginx配置文件一定得注意输入的字符和空格,不然很难查找错误的原因。
上传一个php文件到web根目录。

<?php
phpinfo();
?>

测试可以通过客户机I的IE浏览器进行,直接输入192.168.0.20:80和192.168.0.20:81即可访问到。

在这里插入图片描述

5 flask uwsgi 设置与测试

5.1 安装flask

sudo apt-get install nginx uwsgi uwsgi-plugin-python3

5.2 安装uwsgi

pip install uwsgi

5.3 编写入口.py文件

在web目录下新建基于flask的index.py文件(网站3目录为/var/www/flasksite)

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
     return 'Hello World'

if __name__ == '__main__':

       app.run()

注意python文件名称这里为index,后面配置需要用到

5.4 配置uwsgi

在网站3目录下新建一个uwsgi.ini文件,注意也可以配置xml等文件类型

sudo vim /var/www/falsksite
 [uwsgi]
# 外部访问地址,可以指定多种协议,现在用http便于调试,之后用socket
# http方式
socket = 127.0.0.1:8080
#加载的插件
plugins=python3
# 项目路径
pythonpath= /var/www/flasksite
#chdir 项目目录
# 虚拟环境的目录路径
#virtualenv = /rasa/zndhjqr_nlp/venv
# 主程序文件
#wsgi-file=%(pythonpath)/index.py
module = index
# flask项目创建的application
callable = app
#主线程
master = true
# 处理器数
processes = 2
# 线程数
threads = 2

根据实际情况输入相应的内容,保持退出。

5.5 设置nginx

在/etc/nginx/site-available/default文件最后添加下面内容:

server {
  listen 82;
# listen [::]:80;
#
  server_name flask.com;
#
  root /var/www/flasksite;
  index index.py index.htm index.html index.nginx-debian.html;
#
  location / {
#   try_files $uri $uri/ =404;
#  index index.py index.html index.htm index.php default.html default.htm default.php;
  include uwsgi_params;
  uwsgi_pass 127.0.0.1:8080;
  }
 }

5.6 启动uwsgi和nginx

sudo uwsgi --ini var/www/flasksite/uwsgi.ini

其会显示各种状态,含开启多少个线程。

重新开启一个SHH,重新加载nginx

pi@raspberrypi:~ $ sudo nginx -t && sudo service nginx reload
nginx: [warn] conflicting server name "web1.com" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

在浏览器中输入“192.168.0.20:82”,出现“Hello Word”表明基本安装成功了。

5.7 开机运行

若关闭ssh中uwsgi服务,则无法访问flask,所以要设置开机启动。
先在/etc/init.d文件夹下新建一个flask-uwsgi.sh文件,输入uwsgi --ini /var/www/flasksite/uwsgi.ini
将脚本文件添加进rc.local(一般的自启动程序都会放入此文件夹中)

sudo vim /etc/init.d/flask-uwsgi.sh
uwsgi --ini /var/www/flasksite/uwsgi.ini #vim下先按i 输入后再esc退出输入:wq保存退出
sudo vim  /etc/rc.local


#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
/etc/init.d/flask-uwsgi.sh start #添加的自启动的目录
exit 0

重启树莓派,约等30s后再在win10客户端IE输入192.168.0.20:83,页面还是会显示“Hello Word”,表明uwsgi和nginx自启动设置没有问题。
注意:
1)当uwsgi自动启动后,在连接树莓派HDMI口上的显示界面上无法进入终端(待解决)
2)再次用sudo reboot等重启或者关闭树莓派时,因uwsgi的问题无法实现正常关机和重启,需要先杀掉uwsgi的进程(sudo killall -9 uwsgi)后就能重启,或者写一个关机或重启程序。

6 内网DNS和端口转发设置

为了方便内网的访问,IP地址加端口号访问方式不利于记录和方便别人访问,需要利用DNS将域名转为IP地址,再利用端口转发功能实现输入域名跳转到相应的虚拟主机上。
例如我们在另一台PC电脑ip为192.168.0.2,通过web1.com或web2.com访问树莓派web虚拟主机(ip为192.168.0.20),其中在树莓派上架设DNS服务器。

6.1 软件工具

常用的dns工具有bind9和dnsmasq,曾经尝试过使用dnsmasq,配置了很久都没有实现,而利用bind9 进行了测试(本机测试成功了,但是其它内网电脑无法访问)。
端口转发还是利用nginx。

6.2 安装bind9

登录树莓派(因web服务压力非常小,所以将DNS和web虚拟机都装在一个树莓派4B中)

如果之前安装了其它DNS服务器例如dnsmasq,必须进行删除,不然其占用53端口,后续配置运行bind9后会提示53端口被占,无法运行。后续将补充如何处理配置错误和端口被占问题解决办法。

安装bind9

sudo apt-get install bind9 bind9utils dnsutils

6.3 配置bind9

修改/etc/bind/named.conf.local文件,添加配置以下信息。切记切记文字为英文半角,并且不要忘记; " 符号,否则报错,不容易发现。

sudo vim /etc/bind/named.conf.local
// 编辑named.conf.local文件
//web1
zone "web1.com" { //设置你要配置的域名
type master;
file "db.web1.com"; //这里指向了数据文件,具体位置在/var/cache/bind/db.web1.com
};
// web2
zone "web2.com" { //设置你要配置的域名
type master;
file "db.web2.com"; //这里指向了数据文件,具体位置在/var/cache/bind/db.web2.com
};
// web3
zone "flask.com" { //设置你要配置的域名
type master;
file "db.flask.com"; //这里指向了数据文件,具体位置在/var/cache/bind/db.web2.com
};
需要在/var/cache/bind下创建刚刚填写的两个文件名称“db.web1.com”和db.web2.com”。

```bash
sudo cp /etc/bind/db.local /var/cache/bind/db.web1.com
sudo cp /etc/bind/db.local /var/cache/bind/db.web2.com
sudo cp /etc/bind/db.local /var/cache/bind/db.flask.com

修改db.web1.com”文件,增加A类转向的虚拟机web网站的ip地址。

sudo vim /var/cache/bind/db.web1.com
;
; BIND data file for local loopback interface
;
$TTL    604800
@       IN      SOA     localhost. root.localhost. (
                              2         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
;
@       IN      NS      ns.
@       IN      A       192.168.0.20
www     IN      A       192.168.0.20
@       IN      AAAA    ::1

修改“db.web2.com”文件,增加A类转向的虚拟机web网站的ip地址。

sudo vim /var/cache/bind/db.web2.com
;
; BIND data file for local loopback interface
;
$TTL    604800
@       IN      SOA     localhost. root.localhost. (
                              2         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
;
@       IN      NS      ns.
@       IN      A       192.168.0.20
www     IN      A       192.168.0.20
@       IN      AAAA    ::1

类似的修改“db.flask.com”文件,增加A类转向的虚拟机web网站的ip地址。

sudo vim /var/cache/bind/db.flask.com
;
; BIND data file for local loopback interface
;
$TTL    604800
@       IN      SOA     localhost. root.localhost. (
                              2         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
;
@       IN      NS      ns.
@       IN      A       192.168.0.20
www     IN      A       192.168.0.20
@       IN      AAAA    ::1

类似的修改“db.flask.com

如果需要与外网访问,提高访问速度,可以/etc/bind/named.conf.options文件

在这里插入代码片
options {
    directory "/var/cache/bind";
    
    // If there is a firewall between you and nameservers you want
    // to talk to, you may need to fix the firewall to allow multiple
    // ports to talk.  See http://www.kb.cert.org/vuls/id/800113
    // If your ISP provided one or more IP addresses for stable 
    // nameservers, you probably want to use them as forwarders.  
    // Uncomment the following block, and insert the addresses replacing 
    // the all-0's placeholder.
    
     forwarders {
        202.101.172.35;//根据所接入的市级宽带种类填写DNS服务器,例如杭州电信为202.101.172.35
        202.101.172.47;//根据所接入的市级宽带种类填写DNS服务器,例如杭州电信为202.101.172.47
     };
     
    //========================================================================
    // If BIND logs error messages about the root key being expired,
    // you will need to update your keys.  See https://www.isc.org/bind-keys
    //========================================================================
    
    dnssec-validation auto;
    
    auth-nxdomain no;    # conform to RFC1035
    
    listen-on-v6 { any; };
    
};

重启bind9

pi@raspberrypi:~ $ sudo service bind9 restart
pi@raspberrypi:~ $ sudo systemctl status bind9.service
● bind9.service - BIND Domain Name Server
   Loaded: loaded (/lib/systemd/system/bind9.service; enabled; vendor preset: enabled)
   Active: **active (running)** since Sat 2020-10-10 01:53:47 CST; 1min 3s ago
     Docs: man:named(8)
 Main PID: 15005 (named)
    Tasks: 7 (limit: 4915)
   Memory: 17.0M
   CGroup: /system.slice/bind9.service
           └─15005 /usr/sbin/named -u bind
Oct 10 01:54:45 raspberrypi named[15005]: validating elinkintf.sccl.cn/A: bad cache hit (cn/DS)
Oct 10 01:54:45 raspberrypi named[15005]: broken trust chain resolving 'elinkintf.sccl.cn/A/IN': 202.101.172.35#53
Oct 10 01:54:47 raspberrypi named[15005]: validating elinkintf.sccl.cn/A: bad cache hit (cn/DS)
Oct 10 01:54:47 raspberrypi named[15005]: broken trust chain resolving 'elinkintf.sccl.cn/A/IN': 202.101.172.35#53
Oct 10 01:54:50 raspberrypi named[15005]: validating elinkintf.sccl.cn/A: bad cache hit (cn/DS)
Oct 10 01:54:50 raspberrypi named[15005]: broken trust chain resolving 'elinkintf.sccl.cn/A/IN': 202.101.172.35#53
Oct 10 01:54:50 raspberrypi named[15005]: validating 3.debian.pool.ntp.org/A: bad cache hit (org/DS)
Oct 10 01:54:50 raspberrypi named[15005]: validating 3.debian.pool.ntp.org/AAAA: bad cache hit (org/DS)
Oct 10 01:54:50 raspberrypi named[15005]: broken trust chain resolving '3.debian.pool.ntp.org/A/IN': 202.101.172.35#53
Oct 10 01:54:50 raspberrypi named[15005]: broken trust chain resolving '3.debian.pool.ntp.org/AAAA/IN': 202.101.172.35#53

当出现active running 绿色字体表明bind9配置没有问题,在192.168.0.2的win10 PC的IE浏览器上输入web1.com,可以打开默认192.168.0.20:80网站。表面配置成功。
在这里插入图片描述

6.4 bind9 配置错误

————————下面是部分配置错误53端口被占——————————————————————

重启bind9后出现53端口被占,具体报错如下

pi@raspberrypi:~ $ sudo systemctl status bind9.service
● bind9.service - BIND Domain Name Server
   Loaded: loaded (/lib/systemd/system/bind9.service; enabled; vendor preset: en
   Active: failed (Result: exit-code) since Sat 2020-10-10 01:17:19 CST; 8min ag
     Docs: man:named(8)
  Process: 536 ExecStart=/usr/sbin/named $OPTIONS (code=exited, status=1/FAILURE
Oct 10 01:17:19 raspberrypi named[555]: listening on IPv4 interface lo, 127.0.0.1#53
Oct 10 01:17:19 raspberrypi named[555]: binding TCP socket: address in use 53
Oct 10 01:17:19 raspberrypi named[555]: listening on IPv4 interface eth0, 192.168.0.20#53
Oct 10 01:17:19 raspberrypi named[555]: binding TCP socket: address in use 53
Oct 10 01:17:19 raspberrypi named[555]: unable to listen on any configured inter
Oct 10 01:17:19 raspberrypi named[555]: loading configuration: failure
Oct 10 01:17:19 raspberrypi named[555]: exiting (due to fatal error)
Oct 10 01:17:19 raspberrypi systemd[1]: bind9.service: Control process exited, c
Oct 10 01:17:19 raspberrypi systemd[1]: bind9.service: Failed with result 'exit-
Oct 10 01:17:19 raspberrypi systemd[1]: Failed to start BIND Domain Name Server.

先查询53端口被什么程序占领,解除占领或者卸载占领端口的软件,输入 sudo netstat -apn | grep 53

pi@raspberrypi:~ $ sudo netstat -apn | grep 53
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN      538/uwsgi
tcp        0      0 0.0.0.0:53              0.0.0.0:*               LISTEN      574/dnsmasq
tcp6       0      0 :::53                   :::*                    LISTEN      574/dnsmasq
tcp6       0      0 :::21                   :::*                    LISTEN      539/vsftpd
udp   140800      0 0.0.0.0:53              0.0.0.0:*                           574/dnsmasq
udp        0      0 0.0.0.0:53354           0.0.0.0:*                           574/dnsmasq
udp        0      0 192.168.0.20:36977      192.168.0.100:53        ESTABLISHED 292/systemd-timesyn
udp        0      0 192.168.0.255:137       0.0.0.0:*                           531/nmbd
udp        0      0 192.168.0.20:137        0.0.0.0:*                           531/nmbd
udp        0      0 0.0.0.0:137             0.0.0.0:*                           531/nmbd
udp        0      0 192.168.0.255:138       0.0.0.0:*                           531/nmbd
udp        0      0 192.168.0.20:138        0.0.0.0:*                           531/nmbd
udp        0      0 0.0.0.0:138             0.0.0.0:*                           531/nmbd
udp        0      0 0.0.0.0:35536           0.0.0.0:*                           574/dnsmasq
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           322/avahi-daemon: r
udp        0      0 0.0.0.0:65397           0.0.0.0:*                           574/dnsmasq
udp6       0      0 :::53                   :::*                                574/dnsmasq
udp6       0      0 :::5353                 :::*                                322/avahi-daemon: r

可以看出53端口是被以前所安装的dnsmasq占领,因dnsmasq不在使用,所需需要将其完成卸载

sudo apt-get purge dnsmasq dnsmasq-base

再次执行bind9 重启,检查是否有问题。

——————————————bing9重启中报named.conf中第11行中include前缺少“;”——————————

说明在配置文件中
/etc/bind9/named.default-zones;
/etc/bind9/named.conf.options;
/etc/bind9/named.conf.local(域名和数据文件的配置)
缺少 } ;"" 或者输入了中文等字符,需要对这三个文件做认真检查。

有时候认真检查后还是提示这个错误,可以尝试重启树莓派后,在运行bind9。(曾遇到过一次这种情况)

其它错误参考《BIND 常见错误信息》

6.5 配置nginx

因只用了一台树莓派4B,即DNS域名服务器和web主机的ip都是同一个,所以不需要去修改bind9的每个域名的ip设置,直接配置nginx即可。若不同需要修改/var/cache/bind/db.web1.com和/var/cache/bind/db.web2.com中ip为DNS服务器IP,可以参考《linux搭建dns服务器以及配置nginx端口转发新手向》一文。

打开nginx配件文件,在每个sever下的location添加proxy_pass http://192.168.0.20:端口号;

sudo  vim /etc/nginx/sites-available/default
# Default server configuration
##网站1的设置,网站1 的端口为80,目录为web1
server {
 listen 80 default_server;# 网站1的端口
 root /var/www/web1;#网站1的目录
 # Add index.php to the list if you are using PHP
 index index.html index.php index.htm index.nginx-debian.html;# 网站1的文件访问顺序
 server_name web1.com;#网站1域名,树莓派内部能访问,内网其它客服机需要修改hosts文件中ip与域名对应才能用网页输入域名访问
 location / {
    index index.html index.htm index.php default.html default.htm default.php;# 网站1的文件访问顺序
    proxy_pass http://192.168.0.20:80;#端口转向
 }
 # pass PHP scripts to FastCGI server
 #
 location ~ \.php$ {
 # include snippets/fastcgi-php.conf;
 #
 # # With php-fpm (or other unix sockets):
  fastcgi_pass unix:/run/php/php7.3-fpm.sock; #其为关键点,根据php版本填写,要是php为7.3版本,填写成php7.0-fpm将会出错。
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                 include fastcgi_params;
 # # With php-cgi (or other tcp sockets):
 # fastcgi_pass 127.0.0.1:9000;
 }
 # deny access to .htaccess files, if Apache's document root
 # concurs with nginx's one
 #
 #location ~ /\.ht {
 # deny all;
 #}
}
##网站2的设置,网站2 的端口为81,目录为web2
server {
  listen 81;
# listen [::]:81;
#
  server_name web2.com; #网站2的域名,树莓派内部能访问,内网其它客服机需要修改hosts文件中ip与域名对应才能用网页输入域名访问
#
  root /var/www/web2; #网站2目录
  index index.html index.htm index.nginx-debian.html; #网站2访问文件顺序
#
  location / {
#   try_files $uri $uri/ =404;
  index index.html index.htm index.php default.html default.htm default.php;#网站2文件顺序
   proxy_pass http://192.168.0.20:81;#端口转向
  }
 location ~ \.php$ {
 # include snippets/fastcgi-php.conf;
 #
 # # With php-fpm (or other unix sockets):
  fastcgi_pass unix:/run/php/php7.3-fpm.sock; #其为关键点,根据php版本填写,要是php为7.3版本,填写成php7.0-fpm将会出错。
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                 include fastcgi_params;
 # # With php-cgi (or other tcp sockets):
 # fastcgi_pass 127.0.0.1:9000;
 }
 }
##网站3的设置,网站3 的端口为82,目录为/var/www/flasksite
server {
  listen 82;
# listen [::]:80;
#
  server_name flask.com;
#
  root /var/www/flasksite;
  index index.py index.htm index.html index.nginx-debian.html;
#
  location / {
#   try_files $uri $uri/ =404;
#  index index.py index.html index.htm index.php default.html default.htm default.php;
  include uwsgi_params;
  uwsgi_pass 127.0.0.1:8080;
   proxy_pass http://192.168.0.20:82;#端口转向
  }
 }

重启nginx,没有出现错误,并正常运行,在192.168.0.2的pc win10的IE上输入web1.com和web2.com就能直接访问了。

pi@raspberrypi:~ $ sudo service nginx restart
 sudo systemctl status nginx.service

在这里插入图片描述

7 参考文献

1) 《将树莓派内置的 Python2.7 升级成 Python3》
2)《用uWSGI和Nginx部署Flask项目的方法示例
3)《树莓派开机程序自启动的2种方案!
4)《linux搭建dns服务器以及配置nginx端口转发新手向
5) 《BIND 常见错误信息》

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值