言成言成啊 | Kit Chen's Blog

Win服务器迁移Centos

发布于2022-06-25 11:10:21,更新于2023-06-22 22:59:14,标签:life devops  转载随意,文章会持续修订,请注明来源地址:https://meethigher.top/blog

从2018年起,就搞了个人服务器,那时候就会点html和js,啥也不会。就用的winserver,简单操作。

winserver与centos对比,优点即为对方的缺点。

winserver优点

  • 上手简单。界面人性化,便于小白上手,但使用效率上也就那样了。即使你是winserver高手,操作同样东西,也比小白快不了多少。
  • 开发环境可复用。使用跟windows一样,因为我是windows系统,本地配置好环境,直接丢winserver上,开箱即用。

centos优点

  • 轻量,占用内存小。
  • 使用效率高,但学习成本也高。一旦掌握基本操作后,使用效率都是杠杠滴。吊打winserver不要太爽。尤其是bash脚本优于winserver的bat脚本,只要准备好离线rpm包+bash脚本,随便一台内网机器,分分钟开发环境部署好(docker本身不也是这原理吗,目前来说,对docker无感)。
  • 纵观全局,可支配的自由度较高。而winserver的注册表管理属实麻烦。

winserver,内置了IIS及相关的其他服务,比如我有2G内存,被winserver自带服务就占用了1G多,虽然可关闭,但毕竟不如一个纯净的centos来的爽。

另外,IIS还是更适用于aps.net这些网页技术,我常用的web服务刚好是IIS的死对头apache。

以上种种,让我决定,将winserver改头换面!

当然了,这也是一个不小的工程。

  1. 准备centos服务器
  2. 环境迁移

一、云服务器

截止到2022年7月7日,所有的环境才搞好。主要还是浪费在备案上。

ICP备案,服务器提供商备案,国内服务器强制备案。

公网备案,由国家规定,个人平台没有开放在线交流功能,一般不需要备案,当然了各省份要求也略有不同。

百度云就比较恶心了,如果你不在他的平台备案,他直接给你拦截跳转到他自己的官网,还是百度一贯垄断的风格!

目前手里三台服务器

  • 1h2g1m阿里云,北京,学生机,到2023年1月
  • 1h2g6m腾讯云(轻量、限量),上海,首单机,到2022年8月
  • 2h4g6m百度云(轻量、不限量),广州,首单机,到2024年6月

使用体验上来讲,阿里云>腾讯云>百度云。

开放程度上来讲,腾讯云>阿里云≈百度云。

性价比上来讲,百度云>腾讯云>阿里云。

使用体验,就拿官方帮助文档来说,从文字讲解,到具体代码配置,小白啥也不懂比着文档走,都ojbk。阿里云完爆其他两家厂商!

开放程度,腾讯云认其他的厂商备案,百度不认。腾讯云不会内置一堆开机自启且占用端口的监控,而阿里云有云盾,百度云有bcm,需要手动关闭。

性价比,百度云2h4g6m,78一年;腾讯云1h2g6m,74一年;阿里云1h2g1m,99一年。

今天具体就讲下百度云的体验。

首先,控制台界面简洁的一沓糊涂,虽然没有监控,但是服务器里内置了好多开机自启动的监控。

我以为买了服务器直接就能用,对于百度云来说,大错特错!

阿里的已备案域名,可以直接接入到腾讯云,腾讯云是只要域名有备案信息,明面上要求需要在自己这里再次备案,但是睁只眼闭只眼,不会给你拦截。

而百度云,不好意思,只要我这里没有你域名的备案信息,你在别的厂商备案的我都不认!直接给跳转到百度智能云官网。

由此就有了备案的细节,已备案过的,要求接入到百度云。相当于把你所有信息、身份证照片都给百度一份,理解成刻意收集信息也无不可。

耗时12天,备案通过了。

不过还要求公安备案,关于备案,我也查过资料了-时隔六年终于完成了网安备案 - 明月登楼的博客

目前备案分为两种

  1. ICP备-ICP/IP地址/域名信息备案管理系统
  2. 公安网备-全国互联网安全管理服务平台

我阿里云17年就进行过ICP备案了,也一直没要求公安备案,想了下,感觉也没必要了,太麻烦了。

经过网搜,公安备案其实主要是为了监管言论的,而我这小破站,本身也没啥评论系统,就一自说自话的博客而已,没必要又去费劲再公安备案。

二、环境迁移

之后,就会按照上图,对服务器只开放443代理端口,其他应用端口全是内网里面,不暴露到公网。

2.1 安装httpd

参考

根据自己需求实现的编译安装httpd脚本,查看Centos7一键shell脚本

在安装httpd前,要先perl -v检查是否安装perl,否则,需先安装perl

这是由于apxs的要求,否则apxs无法运行

2.2 安装php

参考

根据自己需求实现的编译安装php脚本,查看Centos7一键shell脚本

像我要开启curl与openssl与gd与zip扩展。就需要重新编译。

curl与openssl主要用于抓包相关内容,gd其中又包括libpng、libjpeg、freetype,主要用于生成图片验证码,zip主要用于下载压缩功能。

像curl、openssl、libpng、libjpeg、freetype,直接通过centos7的yum安装即可。

对于php7.4,要求libzip的版本较新,yum提供的就不适用了,需要自己下载编译。

首先处理zip

1
2
3
4
5
6
7
curl -o libzip-1.3.2.tar.gz https://libzip.org/download/libzip-1.3.2.tar.gz
tar -zxvf libzip-1.3.2.tar.gz
cd libzip-1.3.2
./configure
make && make install
# 不配置这个编译php时,是找不到zip的
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"

然后进行php的编译

1
2
3
yum -y install libpng-devel libjpeg-devel freetype freetype-devel 
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-pdo-mysql --with-curl --with-openssl --enable-gd --with-jpeg=/usr/local/libjpeg/ --with-freetype=/usr/local/freetype/ --with-zip=/usr/local/lib/libzip
make && make install

如果不知道,你要开启的扩展,怎么写,可以通过help查询

1
./configure --help

之后,再去/usr/local/lib/php.ini中,开启扩展这两个扩展即可。,亲测php7.4.x已经不需要手动配置了。

先安装perl再安装apache才行,不安装perl的情况下安装apache,apxs就无法运行

linux的vi编辑器中,

向下搜索是/,输入关键字后,enter即可。n表示下一个。

向上搜索是?,输入关键字后,enter即可。n表示上一个。

2.3 部署httpd与php

基本配置

参考

参考htaccess注入攻击,注意apache下的上传bug。

首先配置支持php

1
vi /usr/local/apache2/conf/httpd.conf

添加(如果已经有了,就不需要修改了)

1
2
3
4
5
LoadModule php7_module        modules/libphp7.so
<IfModule mime_module>
...省略...
AddType application/x-httpd-php .php
</IfModule>

配置根目录下,支持index.php

1
2
3
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

配置web服务器根目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
DocumentRoot "/usr/local/apache2/htdocs"
<Directory "/usr/local/apache2/htdocs">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride None

#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>

在配置的根目录下,创建一个index.php

1
<?php echo 'Hello PHP'; ?>

此时重启httpd

1
systemctl restart httpd

访问查看结果,是否只输出Hello PHP,是则表示配置成功!

我的配置

放上我自己的一份httpd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
ServerRoot "/usr/local/apache2"


#Listen 12.34.56.78:80
Listen 54185


LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule access_compat_module modules/mod_access_compat.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule reqtimeout_module modules/mod_reqtimeout.so
LoadModule filter_module modules/mod_filter.so
LoadModule mime_module modules/mod_mime.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule env_module modules/mod_env.so
LoadModule headers_module modules/mod_headers.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule version_module modules/mod_version.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule status_module modules/mod_status.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule dir_module modules/mod_dir.so
LoadModule alias_module modules/mod_alias.so
#新增
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule php7_module modules/libphp7.so

<IfModule unixd_module>
User daemon
Group daemon
</IfModule>


ServerAdmin meethigher@qq.com


<Directory />
AllowOverride none
Require all denied
</Directory>


DocumentRoot "/site/web/youarebitch"
<Directory "/site/web/youarebitch">
# Options Indexes FollowSymLinks 表示支持目录结构索引、跳转链接
Options FollowSymLinks
# 禁用文件夹下的.htaccess,任务指令都不能使用
AllowOverride None
# 都可以访问
Require all granted
# 访问自动跳转成https
# RewriteEngine on
# RewriteCond %{HTTP_HOST} !^meethigher.top$
# RewriteRule ^(.*)?$ https://meethigher.top%{REQUEST_URI} [L,R]
</Directory>


# 下面是美化下载url
<Directory "/site/web/youarebitch/cloud">
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule download/(.*)/sh/(.*) %{ENV:BASE}/vfm-admin/vfm-downloader.php?q=$1&sh=$2 [L]
RewriteRule download/(.*)/h/(.*) %{ENV:BASE}/vfm-admin/vfm-downloader.php?q=$1&h=$2 [L]
RewriteRule download/zip/(.*)/n/(.*) %{ENV:BASE}/vfm-admin/vfm-downloader.php?zip=$1&n=$2 [L]
</IfModule>
</Directory>

<Directory "/site/web/youarebitch/cloud/cloud">
<Files "*.php">
SetHandler none
SetHandler default-handler
Options -ExecCGI
RemoveHandler .php
</Files>
<IfModule mod_php5.c>
php_flag engine off
</IfModule>
Order Deny,Allow
Deny from all
</Directory>
# 上面是美化下载url


<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

#
# 下面的代码行阻止Web客户端查看.htaccess和.htpasswd文件。
#
<Files ".ht*">
Require all denied
</Files>


ErrorLog "logs/error_log"
LogLevel warn

<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" common
</IfModule>

#<IfModule alias_module>
# ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"
#</IfModule>

<IfModule cgid_module>
</IfModule>


#<Directory "/usr/local/apache2/cgi-bin">
# AllowOverride None
# Options None
# Require all granted
#</Directory>

<IfModule headers_module>
RequestHeader unset Proxy early
</IfModule>

<IfModule mime_module>
TypesConfig conf/mime.types

AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
</IfModule>

<IfModule proxy_html_module>
Include conf/extra/proxy-html.conf
</IfModule>

<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

ErrorDocument 404 /404.html
ErrorDocument 403 /404.html

需要上传下载的文件,需要配置授权。否则php执行删除时没有权限。

1
chmod -R 777 /site/web/youarebitch/cloud/

2.4 部署nginx

参考

根据自己需求实现的编译安装nginx脚本,查看Centos7一键shell脚本

放上我的配置文件nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
worker_processes  1;
error_log logs/error.log;
events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;

sendfile on;



keepalive_timeout 65;

# 开启压缩
gzip on;
gzip_comp_level 9;
gzip_min_length 1024k;
gzip_types text/javascript text/html text/css application/json;
gzip_vary on;

# 不显示nginx版本号
server_tokens off;

# 禁止ip访问,跳转到404
server {
listen 80 default_server;
listen 443 ssl default_server;
server_name _;
ssl_certificate cert/meethigher.top.pem;
ssl_certificate_key cert/meethigher.top.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#表示使用的加密套件的类型。
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
return 200 "Hello World";
}

# 通过http域名访问
server {
listen 80;
server_name meethigher.top;
rewrite ^(.*)$ https://$host$1;
}

server {
listen 80;
server_name git.meethigher.top;
# gitblit
location / {
# 配置git上传大小
client_max_body_size 500m;
proxy_pass http://127.0.0.1:54186/;
}
}

server {
listen 80;
server_name snipurl.cc;
location / {
# 配置proxy上传大小
client_max_body_size 1024m;
proxy_pass http://127.0.0.1:54190/;
}
}


# 一级域名
server {
listen 443 ssl;
server_name meethigher.top;

ssl_certificate cert/meethigher.top.pem;
ssl_certificate_key cert/meethigher.top.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#表示使用的加密套件的类型。
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
charset utf-8;
# 转发真正ip
proxy_set_header Host $host;
proxy_set_header x-forwarded-for $remote_addr;
proxy_set_header Proxy-Client-IP $remote_addr;
proxy_set_header WL-Proxy-Client-IP $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;

# 博客+云盘
location / {
# 禁用浏览器直接访问, 如果来源不是我的网站,打回
location ~* \.(gif|svg|css|js|ico|html)$ {
if ($http_referer !~ 'meethigher.top') {
return 404;
}
root /site/web/youarebitch/;
}
# 配置proxy上传大小
client_max_body_size 1024m;
proxy_pass http://127.0.0.1:54185/;
}

# 拦截cgi-bin路径,防止被攻击
location /cgi-bin/ {
default_type text/html;
return 200 "hello world";
}

# 统计
location /census/ {
proxy_pass http://127.0.0.1:54187/;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}
}

2.5 部署git

Gitblit

下载后解压,修改配置文件data/defaults.properties,开放http,禁用https

1
2
3
4
5
6
7
8
9
server.httpPort = 54186

# Secure/SSL https port to serve. <= 0 disables this connector.
# On Unix/Linux systems, ports < 1024 require root permissions.
# Recommended value: 443 or 8443
#
# SINCE 0.5.0
# RESTART REQUIRED
server.httpsPort = 0

直接执行命令启动,限制最大100M,占用超过40%后,就触发GC。不然这个Gitblit吃太多内存了。

1
nohup java -server -Xmx100m -XX:CompressedClassSpaceSize=100m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:InitiatingHeapOccupancyPercent=40 -cp "gitblit.jar:ext/*" com.gitblit.GitBlitServer --baseFolder data  >log 2>&1 &
发布:2022-06-25 11:10:21
修改:2023-06-22 22:59:14
链接:https://meethigher.top/blog/2022/win-to-centos/
标签:life devops 
付款码 打赏 分享
shift+ctrl+1可控制目录显示