Skip to content

SSR和网站共享端口

wandou911 edited this page Mar 22, 2021 · 19 revisions

安装nginx

步骤 1: 添加 yum 源

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

步骤 2: 安装

yum install nginx

步骤 3: 配置 Nginx 服务

设置开机启动

$ sudo systemctl enable nginx

启动nginx服务

$ systemctl start nginx

在网页输入ip地址,回车:查看nginx是否启动成功

如果出现:Welcome to nginx!

证明启动成功!可以进行部署网站的操作

停止nginx服务

$ systemctl restart nginx

如果出现:500 Internal Privoxy Error

500 Internal Privoxy Error
Privoxy encountered an error while processing your request:

Could not load template file no-server-data or one of its included components.

Please contact your proxy administrator.

If you are the proxy administrator, please put the required file(s)in the (confdir)/templates directory.  The location of the (confdir) directory is specified in the main Privoxy config file.  (It's typically the Privoxy install directory).

则需要关闭防火墙或者开启80端口:

防火墙相关指令

1、开启关闭防火墙

systemctl start firewalld
systemctl stop firewalld
systemctl status firewalld

2、放行端口

firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=443/tcp
firewall-cmd --permanent --zone=public --add-port=22345/tcp
firewall-cmd --reload

这里红字部分是我们设置的端口,需要放行。如果有出现"FirewallD is not running"问题可以参考"CentOS7出现的”Failed to start firewalld.service”问题以及端口添加记录"解决,没有启动导致的。

3、检查端口是否开启

firewall-cmd --permanent --query-port=80/tcp
firewall-cmd --permanent --query-port=443/tcp
firewall-cmd --permanent --query-port=22345/tcp

部署网站

克隆一个模版

cd
git clone https://github.com/Wuchenwcf/Web-templates.git

cd /
mkdir www
cd www/
cp -r /root/Web-templates/349套HTML5+CSS3各行各业网站模板/001/ ./

配置Nginx

cd /etc/nginx/conf.d/

通过 ls查看配置文件,(你之前没有配置过,下面就是空的了),然后通过 vi 命令新建一个配置文件,例如: vi static-naice-me.conf (我的顶级域名是naice.me通过解析子域名 static.naice.me,所以就起了这个static-naice-me.conf 名字的文件),然后你就进入了一个 vi 编辑的环境,按下 键盘的i 键,就可以写入内容,写入以下内容

server {
    listen       80;
    server_name  cloudus02.xiaokeli.info;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    root   /www/002;
    index  index.php index.html index.htm;

}

写入内容之后,按下esc然后输入:wq!来保存你编辑的内容。

退出之后我们需要通过命令行重启 nginx服务

nginx -s reload

域名解析 域名解析是把域名指向网站空间IP,让人们通过注册的域名可以方便地访问到网站的一种服务, 下面简单说说一下域名解析的操作,你拥有了一个域名然后,进入 dnspod,没有的话账号的话, 直接注册登录,然后进到控制台

添加域名

添加 a记录

如下图:

好了,我们刚好刚刚把解析好的域名写进去我们的 nginx 的配置里面,也重启了 nginx 服务,下面就直接输入http://cloudus02.xiaokeli.info/,就可以访问到我们刚才写的静态网页,是不是有点小激动??

问题:Nginx出现403 forbidden

原因:SELinux设置为开启状态(enabled)的原因。

1 查看当前selinux的状态。

/usr/sbin/sestatus

2 将SELINUX=enforcing 修改为 SELINUX=disabled 状态。

vi /etc/selinux/config

#SELINUX=enforcing SELINUX=disabled

3重启生效。reboot。

reboot

添加Https

安装 Let’s Encrypt

yum install -y epel-release

yum install letsencrypt

这一步时,出现问题 ImportError: No module named 'requests.packages.urllib3' 查了万能的谷歌,发现是python的模块版本的问题,运行以下命令后,成功解决

pip install requests urllib3 pyOpenSSL --force --upgrade
pip install --upgrade --force-reinstall 'requests==2.6.0'
使用 Let’s Encrypt 获取证书

Let's Encrypt在验证的过程中会占用443端口,所以需要暂时关闭nginx以释放端口。

# 停止 nginx,释放端口
systemctl stop nginx

# 获取证书
letsencrypt certonly --standalone

# 同意条款之后,输入域名,两个以上的域名用空格隔开
ss.xiaoweigod.com www.ss.xiaoweigod.com 

如果报错:

Problem binding to port 80: Could not bind to IPv4 or IPv6.

则原因是 nginx 占用了80端口,输入 service nginx stop。然后再次执行证书安装命令,即可顺利安装。

安装完毕后,输入 service nginx start,重启 nginx 服务。

证书到期后更新证书

证书有效期为90天 到期前可以更新证书

方法一

1 创建shell脚本 certbot.sh

创建目录

cd /
mdkir certbot
cd certbot

创建文件 vi certbot.sh

#!/bin/bash
# 停止 nginx,释放端口
nginx -s quit

# 更新证书
certbot renew

# 启动 nginx
nginx

2 创建定时任务

crontab -e

3 添加定时任务

30 6 */60 * * sh  /root/certbot/certbot.sh

每60天更新一次证书

4 查看定时任务

crontab -l

方法二

# 停止 nginx,释放端口
systemctl stop nginx.service

# 更新证书
certbot renew

# 启动 nginx
systemctl start nginx
使用证书

nginx的配置文件 /etc/nginx/conf.d ,只要往里面添加一个433端口的server即可:

# 编辑配置文件
vi /etc/nginx/conf.d/你的域名.conf

# 添加443端口以及ssl证书路径
server {
listen 80;
server_name demo.mydomain.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
  listen 443 ssl;
  server_name demo.mydomain.com;

  charset utf-8;
  root /www/demo.mydomain.com;
  index index.html index.htm;

  access_log  /var/log/nginx/demo.mydomain.com_access.log;
  error_log  /var/log/nginx/demo.mydomain.com_error.log;

  # letsencrypt生成的文件
  ssl_certificate /etc/letsencrypt/live/demo.mydomain.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/demo.mydomain.com/privkey.pem;

  ssl_session_timeout 1d;
  ssl_session_cache shared:SSL:50m;
  ssl_session_tickets on;


  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  # 一般推荐使用的ssl_ciphers值: https://wiki.mozilla.org/Security/Server_Side_TLS
  ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128:AES256:AES:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK';
  ssl_prefer_server_ciphers on;
}

# 重启nginx
systemctl restart nginx

ps: 全局替换::%s/vivian/sky/g(等同于 :g/vivian/s//sky/g) 替换每一行中所有 vivian 为 sky

此时可以在浏览器中输入 https://域名 ,进行访问,如本站是 https://cloudus02.xiaokeli.info

配置Nginx端口转发

server {
  listen 443 ssl;

这里改为:listen 22345 ssl;

重启nginx:

nginx -s reload

配置SSR端口转发

 vi user-config.json 
 # 修改additional_ports
 # additional_ports_only:ture
 # "redirect": "*:443#127.0.0.1:22345" 这里22345和nginx保持一致,
{
    "server": "0.0.0.0",
    "server_ipv6": "::",
    "server_port": 443,
    "local_address": "127.0.0.1",
    "local_port": 1080,

    "password": "m56",
    "method": "aes-128-ctr",
    "protocol": "auth_aes128_md5",
    "protocol_param": "",
    "obfs": "tls1.2_ticket_auth_compatible",
    "obfs_param": "",
    "speed_limit_per_con": 0,
    "speed_limit_per_user": 0,

    "additional_ports" : {"443": {
          "passwd": "xiaokeli.info",
          "method": "aes-128-ctr",
          "protocol": "auth_aes128_md5",
          "protocol_param": "#",
          "obfs": "tls1.2_ticket_auth",
          "obfs_param": ""
      }
    },
    "additional_ports_only" : true, // only works under multi-user mode
    "timeout": 120,
    "udp_timeout": 60,
    "dns_ipv6": false,
    "connect_verbose_info": 1,
    "redirect": "*:443#127.0.0.1:22345",
    "fast_open": false
}

重启ssr:

cd cd /root/shadowsocksr/
sh stop.sh
python server.py

如下图说明一切正常,如果不正常 根据报错修改

测试网站是否正常

此时可以在浏览器中输入 https://域名 ,进行访问,如本站是 https://cloudus02.xiaokeli.info

静态index

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0, user-scalable=no"/>
    <title>nginx 静态网站部署</title> 
    <style>
        html,body{
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            background: #333;
            display: flex;
            justify-content: center;
            align-items: center
        }
        h1, a{
            color: #fafafa;
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>nginx静态网站部署实例<br/><a href="http://blog.naice.me/">naice blog</a></h1>
</body>
</html>

配置nginx端口

参考文献

Clone this wiki locally