前几天跟某位大佬聊天的时候,我说我为了能把本地网站发布到网上,使用Openvpn,云服务器开启Openvpn主机,本地连接过去,然后云服务器nginx代理发布到网上。大佬说,你这样挺麻烦的,你试试ssh打洞。
今天在家实在想找点事做(主要是心情不爽),就折腾起树莓派,结果给我折腾成功了,以下是方案总结:
- 方案为:树莓派安装wordpress,通过ssh打洞,云主机nginx代理到公网。
- 在树莓派上执行以下命令,安装wordpress
yum install httpd mariadb-server -y
3. 初始化Mariadb以及添加数据库,安装wordpress不再赘述。可以自行百度。这里提供mariadb的一个初始化教程,以便后续遗忘。
https://www.cnblogs.com/dxxblog/p/10243057.html
4. 本地服务器生成ssh key
ssh-keygen
5. 拷贝本地ssh key到远程云主机,目的:免密登录。-p参数:如果远程主机ssh端口默认不是22,需要该参数,本人不是22端口
ssh-copy-id -p 端口号 username@host
6. 安装autossh。一般centos直接输入命令 yum install autossh -y即可。博主是Centos 7 arm架构的树莓派,yum里没有autossh,采用以下办法编译安装:
#wget https://armv7.dev.centos.org/repodir/epel-pass-1/autossh/1.4g-1.el7/armv7hl/autossh-1.4g-1.el7.armv7hl.rpm
可以使用自建mirror啦>>
wget https://mirror.fastspeedgo.xyz/raspberryMirror/autossh-1.4g-1.el7.armv7hl.rpm
yum install autossh-1.4g-1.el7.armv7hl.rpm
7. Systemctl中添加服务
vi /lib/systemd/system/autossh.service
填入以下内容,请注意,User=,填入使用 ssh-keygen的用户,它影响ExecStart那行中的’
/root/.ssh/id_rsa ‘,是否是‘ /root/.ssh/id_rsa ’得看你是使用什么用户生成,如果不太明白,请百度”ssh-keygen “。
另,ExecStart这行, -M 5678主要是用于监控ssh是否需要重连,具体参考
https://www.cnblogs.com/itech/p/4572275.html
[Unit]
Description=Auto SSH Tunnel
After=network-online.target
[Service]
User=root
Type=simple
ExecStart=/usr/bin/autossh -M 5678 -NR 远程服务器端口:localhost:本地端口 -i /root/.ssh/id_rsa username@host -p 端口号 >> /dev/null 2>&1
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -TERM $MAINPID
KillMode=process
Restart=no
[Install]
WantedBy=multi-user.target
WantedBy=graphical.target
8. 设置autossh开机自启,启动服务
systemctl enable autossh
systemctl start autossh
(注意,此时默认远程服务器监听127.0.0.1地址,如需要监听0.0.0.0,请更改/etc/ssh/sshd_config中的GatewayPorts为yes。参考
http://www.mamicode.com/info-detail-1030313.html )
9 在云服务器上测试是否成功打通:(端口号是第七步里命令行里的远程服务器端口 )
wget http://localhost:端口号
10. 快速配置nginx代理。不多赘述,这里贴个conf文件样例。
server {
listen 80;
listen [::]:80;
server_name blog.mytlu.cn;
# enforce https
return 301 https://blog.mytlu.cn;
}
server
{
listen 443 ssl http2;
#listen [::]:443 ssl http2;
server_name blog.mytlu.cn;
index index.html index.htm index.php default.html default.htm default.php;
access_log /var/log/nginx/sample;
ssl_certificate /*.crt;
ssl_certificate_key /*.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
ssl_session_cache builtin:1000 shared:SSL:10m;
proxy_redirect http:// $scheme://;
port_in_redirect on;
location ~ ^/ {
proxy_buffering off;
proxy_pass http://localhost:端口号;
proxy_http_version 1.1;
proxy_set_header Host $host:$server_port;
# proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_header User-Agent;
#proxy_pass_header X-Forwarded-Proto;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 10240M;
client_body_timeout 5m;
}
}
10. 部分参考网站链接:
https://www.cnblogs.com/itech/p/4572275.html
