欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

阿里云搭建小程序PHP环境

程序员文章站 2022-07-10 09:43:18
...

最近想着阿里云能不能搭建小程序官方的demo,之前用过基于腾迅云的小程序解决方案,虽然很好,但夸何没有在腾迅云注册备案过域名,而曾经在阿里云上注册备案过域名.

基本环境 CentOS  7.3 

(一)安装 Nginx

yum -y install nginx

查看是否安装成功

nginx -v
如果安装成功则显示

阿里云搭建小程序PHP环境

(二)安装 PHP

Wafer 的 Demo 需要 5.6 以上版本的 PHP,添加 remi 源.

wget 'https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi.repo' -O /etc/yum.repos.d/remi.repo

安装

yum install --enablerepo=remi --enablerepo=remi-php56 php php-mbstring php-mcrypt php-mysql php-curl php-fpm

查看是否安装成功

php -v

php版本要大于5.6

阿里云搭建小程序PHP环境

(三)配置 Nginx 和 HTTPS

申请一个 SSL 证书,可以到阿里云申请免费的 SSL 证书,申请成功之后下载证书,并把压缩包中 Nginx 目录下的证书文件上传到服务器的 /data/release/nginx 目录,如果没有这个目录则新建:上传完证书以后,配置 Nginx,进入服务器的 /etc/nginx/conf.d 目录,新建一个 weapp.conf 文件,内容为以下,注意(www.xx.com改为自己的域名,1_www.xx.com_budle.crt和2_www.xx.com.key分别改为自己的证书文件)

# 重定向 http 到 https
www.xx.com
server { listen 80; server_name www.xx.com; rewrite ^(.*)$ https://$server_name$1 permanent;}server { listen 443; server_name www.xx.com; ssl on; ssl_certificate /data/release/nginx/1_www.xx.com_bundle.crt; ssl_certificate_key /data/release/nginx/2_www.xx.com.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA; ssl_session_cache shared:SSL:50m; ssl_prefer_server_ciphers on; root /data/release/php-demo; location ~ \.php$ { root /data/release/php-demo; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location /weapp/ { root /data/release/php-demo; index index.html index.htm index.php; try_files $uri $uri/ /index.php; }}

运行nginx

nginx -t

(四)安装mysql

安装mysql5.7

1、配置YUM源

# 下载mysql源安装包
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm


# 安装mysql源
yum localinstall mysql57-community-release-el7-8.noarch.rpm


检查mysql源是否安装成功
yum repolist enabled | grep "mysql.*-community.*"

阿里云搭建小程序PHP环境


2、安装MySQL
yum install mysql-community-server


3、启动MySQL服务
systemctl start mysqld


查看MySQL的启动状态
shell> systemctl status mysqld

阿里云搭建小程序PHP环境


4、开机启动
systemctl enable mysqld
systemctl daemon-reload


5、修改root本地登录密码
mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改:
grep 'temporary password' /var/log/mysqld.log

登陆并修改默认密码
mysql -u root -p

mysql>ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码!'; 

新建一个数据库名为 cAuth,排序规则为 utf8mb4_unicode_ci,小程序后台用到
mysql>CREATE DATABASE IF NOT EXISTS cAuth,排序规则为 DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci; 

阿里云搭建小程序PHP环境


(五)上传 Demo 和启动

到 wafer2-quickstart-php 仓库下载最新的 Demo 代码,修改 server/config.php

<?php
/**
 * Wafer php demo 配置文件
 */

$config = [
    'rootPath' => '',

    // 微信小程序 AppID
    'appId' => '',

    // 微信小程序 AppSecret
    'appSecret' => '',

    // 使用腾讯云代理登录  
    'useQcloudLogin' => false, //不使用腾迅云代理登录


    /**
     * 这里请填写云数据库的
     */
    'mysql' => [
        'host' => 'localhost',
        'port' => 3306,
        'user' => 'root',
        'db'   => 'cAuth',
        'pass' => '数据库密码',
        'char' => 'utf8mb4'
    ],

    'cos' => [
        /**
         * 区域
         * 上海:cn-east
         * 广州:cn-sorth
         * 北京:cn-north
         * 广州二区:cn-south-2
         * 成都:cn-southwest
         * 新加坡:sg
         * @see https://cloud.tencent.com/document/product/436/6224
         */
        'region' => 'cn-sorth',
        // Bucket 名称
        'fileBucket' => 'wafer',
        // 文件夹
        'uploadFolder' => ''
    ],

    // 微信登录态有效期
    'wxLoginExpires' => 7200,
    'wxMessageToken' => 'abcdefgh',

    // 其他配置
    'serverHost' => 'wx.wafersolution.com',
    'tunnelServerUrl' => 'http://tunnel.ws.qcloud.la',
    'tunnelSignatureKey' => '27fb7d1c161b7ca52d73cce0f1d833f9f5b5ec89',
      // 腾讯云相关配置可以查看云 API 秘钥控制台:https://console.cloud.tencent.com/capi
    'qcloudAppId' => 1200000000,// 必须是数字
    'qcloudSecretId' => '你的腾讯云 SecretId',
    'qcloudSecretKey' => '你的腾讯云 SecretKey',
    'networkTimeout' => 30000
];

接着将 server 目录下的所有文件都上传到 /data/release/weapp/php-demo 目录下:


小程序端修改配置

阿里云搭建小程序PHP环境

这样就可以了进行调试了

登录和信道都没问题了

阿里云搭建小程序PHP环境

如果有什么问题及不懂的可以于关注公众号阿里云搭建小程序PHP环境回复999加我微信