手把手叫你搭建小程序服务端

本例服务端使用的是php,搭建lnmp环境和配置https请查看我的上一篇文章,ubuntu 18.04.5 LTS 安装lnmp

1.下载php版本的腾讯官方小程序demo,wafer2-quickstart-php,解压之后的目录结构,
在这里插入图片描述
其中server目录是要部署到自己申请的腾讯云服务器上的,打开server目录,做如下配置,
在这里插入图片描述
登录自己的小程序,在开发设置里面查看appId和appSecret,
在这里插入图片描述
useQcloudLogin请设置为false,

2.登录自己的服务器,创建存储小程序数据的MySQL库,库名为cAuth,表结构请参考文件cAuth.sql,DB用户名和密码请自行设置,在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
创建结果,
在这里插入图片描述

  1. 将server目录打包成server.zip,上传到服务器web目录,我的目录是/var/www/html,解压,然后将目录名修改为weapp(起一个有意义的名字即可)
    在这里插入图片描述
    在这里插入图片描述

  2. 配置SDK文件,
    在这里插入图片描述
    这个路径可以自己定,我的路径是/etc/qcloud/sdk.config,内容根据指引设定,
    在这里插入图片描述
    下面是我的设置,仅供参考,
    在这里插入图片描述

  3. 修改nginx配置,使小程序demo服务端weapp能够被正常访问,找到nginx配置文件,
    在这里插入图片描述
    下面是我的配置,仅供参考,
    在这里插入图片描述
    nginx配置

##
# 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
#
server {
        listen 80 default_server;
        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/html;

        # Add index.php to the list if you are using PHP
        index index.html index.php index.htm index.nginx-debian.html;

        server_name www.lgcxc.cn;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        #       # 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;
#       }
#}


server {
    listen              443 ssl;
    server_name         www.lgcxc.cn;

    ssl_certificate     /etc/nginx/www.lgcxc.cn/Nginx/1_www.lgcxc.cn_bundle.crt;

    ssl_certificate_key /etc/nginx/www.lgcxc.cn/Nginx/2_www.lgcxc.cn.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    location /weapp/ {
        rewrite  ^/weapp/(.*)$  /$1  break;
        proxy_set_header   Host      $http_host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass   http://127.0.0.1:8082;
    }
}

 server {
    listen 8082;
    server_name 127.0.0.1;
    root  /var/www/html/weapp;
    index  index.html index.php;
    if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php?s=$1  last;
        break;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }
}

访问项目weapp,查看服务端部署是否正常,
在这里插入图片描述
测试登录服务,看看相应了什么,
在这里插入图片描述
这个错误码,表明我们服务端配置正确,接下来要使用客户端联调。

  1. 使用微信开发者工具打开小程序demo客户端,修改config.js配置,下面是我的配置,仅供参考,
    在这里插入图片描述
    然后清理缓存,重新编译,
    在这里插入图片描述
    点击测试登录接口,微信授权允许,
    在这里插入图片描述
    测试结果,如果出现了你的头像,请求返回了你的登录信息,表明全链路配置正确,
    在这里插入图片描述

查看DB存储的登录用户信息,
在这里插入图片描述
至此,就可以愉快的开发你的小程序了。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值