微信服务器配置(nginx+python+supervisord, etc:java)

来源:互联网 发布:java cors 跨域 编辑:程序博客网 时间:2024/06/05 14:20

认证需要端口

微信服务器配置需要开放80或者443端口,也就是Http://和https://

一般url格式为,http://domain_name/project_name

(提示:端口号可不加)


验证

验证流程:    微信发送一些请求,并对服务器内的设置的东西进行反馈验证,若验证成功,则可以开启认证。

(微信接入指南内有详细验证流程)


服务器配置--ip

服务器之类的,这里只提到阿里云。开启阿里云后,若你开启了安全防护,可能需要自己在安全组策略中开放端口,不然微信或者网络上是无法访问到你的服务器的。(ssh也是一样的)

开放时,若不怕有攻击的话,大可以直接设置为0.0.0.0。(若是希望准确点的话,首先用微信中的调试工具,对服务器进行发送token验证,确认下发送token的微信服务器ip,并加入服务器的安全组策略中)

(提示:微信公众平台现有一个ip白名单,所以我们同样需要将我们的ip以及服务器ip加入其中,若是用了vpn什么的,也是需要的)


服务器环境搭建

是否设置新用户等看自己喜欢。


python环境

yum install python python-pip nginx gunicornpip install virutualenv gunicorn(python环境的最好用pip或者easy_install)pip freeze > weixin_envrionment.txt 保存(pip install -r xxx.txt调用)


框架可用Django,或者FLASK、Tornado

同样需要安装。(pip)


配置

虚拟环境 virtualenv project_name (source ./bin/activate启动、 deactivate取消 )

(查看文件目录树,可用tree,tree -L 2 展示2级文件格式

编写.py web文件。

编写完后,开始写.conf.          首先是nginx.conf(/etc/nginx/nginx.conf)

location /weixin {                proxy_pass http://127.0.0.1:8000;        }  


代理8000,当然默认的端口开放是80或者443的。 

server {        listen       80 default_server;        listen       [::]:80 default_server;



然后使用supervisord 调用gunicorn执行程序

加入:

[program:weixin]command = /home/mirror/weixin/bin/gunicorn -w 2 -b 0.0.0.0:8000 weixin:appdirectory = /home/mirror/weixintimeout = 60*60user = rootautostart = trueautorestart = trueredirect_stderr = truestdout_logfile = /home/mirror/logs/supervisor.log


(日志文件要记得创立,便于观察错误原因。)

-w 是 worker、-b 是address    xxx(.py程序名称):app(是主程序)


【启动方法】

supervisord -c /etc/supervisord.confsupervisorctl (观察应用)supervisorctl restart reload stop ····


(杀死进程时使用   netstat -anp| grep super 、kill -s 9 pid)

   

其他Java配置方法:spring+tomcat(虽然其实直接运行就可以了)

编写主控制器类WeiXinController

import com.qiao.weixin.algorithms.encrypt.Hash;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import java.util.Arrays;/** * @author qiao * email tyrantqiao@icloud.com * 2017/8/24 11:26 */@Controllerpublic class WeiXinController {    @RequestMapping(value = "/weixin", method = RequestMethod.GET)    public String confirmWeiXin(@RequestParam String signature,                                @RequestParam String echostr,                                @RequestParam String timestamp,                                @RequestParam String nonce) {        final String token = "QiaoLion";        String[] confirmList = new String[]{token, timestamp, nonce};        Arrays.sort(confirmList);        StringBuffer confirmBuffer = new StringBuffer();        for (int i = 0; i < confirmList.length; i++) {            confirmBuffer.append(confirmList[i]);        }        if (signature.equals(Hash.getSha1(confirmBuffer.toString()))) {            System.out.println(echostr);            return echostr;        }        return "";    }    @RequestMapping(value = "/",method = RequestMethod.GET)    public String home(){        return "home";    }    //若使用thymeleaf模版,注意circul path error,注意返回值别与你的模版名字一样,不然会error}

war

打包成war文件发布给tomcat的话,在启动类继承SpringBootServletInitializer,然后.pom文件打包成war,加入以下代码:

<dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-tomcat</artifactId>            <scope>provided</scope></dependency>
ps:providedmaven doc

compile

This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

provided

This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

(简单来说就是compile编译到运行都需要这些文件dependencies, 而provided只在编译时测试时使用,到了具体运行时,就交给运行的容器来负责。避免了包冲突之类的)





原创粉丝点击