写在前面 鉴于服务器要续费而且服务器内部实在是太乱了…所以有必要重做一次服务器,但是每一次搞服务器都是大坑,所以写一篇博文记录这次的服务器维护过程,给自己提示,也希望能够给初学者(虽然自己也是)予以帮助,因而,如果文章有不准确的地方或着错误的地方,欢迎评论或者邮箱纠正
0x00 基础搭建
对于一台服务器而言,首先你需要选择一个合适的服务器系统,虽然说市面上大多使用Centos
等yum
类包管理器的系统,但是对于博主而言,因为平日电脑上使用的就是使用apt
包管理器的deepin
,而deepin
的祖上就是debian
,因而为了维护的方便和日常使用习惯而言,选择Debian 9 x64
Tips: 如果可以的话,建议在选择完服务器系统后,就要开始基础维护了Debian
的所有中优先使用Debian 9
因为Debian 9
的软件包维护较为广泛与新,可以避免一些服务必须要编译或者添加软件源的问题
注意,从此处开始,下文所有部分都是基于首先,完成现有包更新Debian 9
而写的,如果你在使用Ubuntu
系统或其他apt
系的系统,请在确保不会产生软件包冲突的情况下,酌情参考,如果你是非apt
系的系统,可以将本文命令替换成自己系统上相同或者相同作用的语句
apt-get update
apt-get full-upgrade
1×00 Web 服务器搭建
一般 Web 服务器就是Apache2
,Nginx
与Lighttpd
,博主这里选择的是Apache2
1×01 安装Apache2
apt-get install apache2
< apache2 -v
> Server version: Apache/2.4.25 (Debian)
Server built: 2018-03-31T08:47:16
1×02 开启 Apache2 所需服务
a2enmod rewrite
a2enmod ssl
a2enmod headers
a2enmod http2
开启重定义链接与 ssl 选项,并且开启header
与http/2
使用
1×10 SSL 加密证书申请
为了方便
certbot
然后为了简介wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
./certbot-auto certonly -d *.你的域名 --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory
中间有输出这么一端话:
-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.xxx.xx with the following value:
<TXT记录内容>
Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------
Press Enter to Continue
意思就是让你给_acme-challenge.你的域名
加个txt解析,记录内容是中间的<TXT记录内容>,用dig
命令确认完记录后回车即可
1×20 Apache2 配置
默认 Apache2 配置文件: /etc/apache2/apache2.conf
站点配置文件: /etc/apache2/sites-enabled/*.conf
1×21 目录配置
将AllowOverride None
更改为AllowOverride All
1×22 站点配置
养成一个好习惯,把站点配置文件按照/etc/apache2/sites-enabled/<优先级>-<域名>.conf
eg:001-blog.woshiluo.site.conf
不然你的后期维护将会非常的麻烦~~血的教训
标准配置文件如下
<VirtualHost *:80>
# http 配置
ServerAdmin <你的邮箱>
ServerName <域名>
DocumentRoot <域名目录>
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
# https 配置
# 开启 http2
Protocols h2 http/1.1
ServerAdmin <你的邮箱>
ServerName <域名>
DocumentRoot <域名目录>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/<申请证书域名>/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/<申请证书域名>/privkey.pem
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
其中 <你的邮箱>表示Admin
邮箱,当然这个就写你自己的邮箱啦
<域名> 表示正在配置的域名
<域名目录> 表示当前域名指向的目录,如/var/www/html
<申请证书域名> 表示你在上文申请证书时所用的域名,注意通配符证书的域名不带*.
比如你申请的为*.woshiluo.sie
,那么此处填写woshiluo.site
1×24 SSL 加强[选做]
经过这一步,你的服务器在 SSL Lab 的评测会得到 A+本部分部分源自:https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html在你的配置加上这些语句:
# 去除弱加密部分
SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
SSLProtocol All -SSLv2 -SSLv3
SSLHonorCipherOrder On
# HSTS
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
Header always set X-Content-Type-Options nosniff
# 修复了一堆漏洞
SSLCompression off
SSLUseStapling on
SSLStaplingCache "shmcb:logs/stapling-cache(150000)"
SSLSessionTickets Off
至此,你的Apache2
基础配置完毕
1×30 服务器运行环境配置
Upd: 2019.03.04 目前看来, 使用官方的 apt 源是一个更加优秀的选择 wget https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb dpkg -i mysql-apt-config_0.8.12-1_all.deb 或者去官网下载 然后执行 apt-get update apt-get install mysql-community-server php phpmyadmin 注意,使用弱加密模式才可使用phpmyadmin 以下是更新前内容
apt-get install php mysql-server phpmyadmin
一个命令解决一切问题(划
然而实际上这样子会出一点问题,使用这个命令安装很有可能会出现一个问题,那就是mysql root
用户密码为空…
按照下面的操作一般就可以修复了:
mysql -uroot
update mysql.user set authentication_string=PASSWORD('<你的密码>'),
flush privileges;
quit;
service mysql restart
1×31 关于http2的细小配置
Apache 2.4.27, HTTP/2 在prefork模式下将不受支持 所以防患于未然,我们来一波配置:
apachectl stop
apt-get install php-fpm
a2enmod proxy_fcgi setenvif
a2enconf php7.0-fpm
a2dismod php7.0
a2dismod mpm_prefork
a2enmod mpm_event
apachectl start
1×40 邮箱服务搭建
我们使用
postfix
与dovecot
相结合来搭建邮箱服务,当然不可能只有安装这么简单,我们还需要配置
apt-get install postfix libsasl2-2 sasl2-bin libsasl2-modules dovecot-imapd dovecot-pop3d dovecot-common
1×41 配置postfix与dovecot
首先我们需要停止服务:service postfix stop
service dovecot stop
修改postfix基础配置文件:/etc/postfix/main.cf
在文件末尾加入下面的语句:
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = <你的域名>
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
smtpd_sasl_security_options = noanonymous
message_size_limit = 10240000
其中 <你的域名>表示你要进行邮箱配置的域名
修改dovecot配置文件:/etc/dovecot/dovecot.conf
在开头添加:
protocols = pop3 imap
mail_location = mbox:~/mail:INBOX=/var/mail/%u
disable_plaintext_auth = no
auth default {
mechanisms = plain login
socket listen {
client {
path = /var/spool/postfix/private/auth
mode = 0660
user = postfix
group = postfix
}
}
}
修改sasl配置文件:`/etc/default/saslauthd中的
START=no
为
START=yes
修改
OPTIONS="-c -m /var/run/saslauthd"
为
OPTIONS="-c -m /var/spool/postfix/var/run/saslauthd"
当然,咱把目录改了…文件也得改啊:
rm -r /var/run/saslauthd/
mkdir -p /var/spool/postfix/var/run/saslauthd
ln -s /var/spool/postfix/var/run/saslauthd /var/run
chgrp sasl /var/spool/postfix/var/run/saslauthd
adduser postfix sasl
启动这几个服务以及处理一些权限:
service postfix start
service dovecot start
usermod -G root postfix
chmod -R 777 /var/mail
然后根据想要的用户前缀建个用户
adduser <你的用户名字>
然后根据需求加密码什么的就行了
1×42 启用 imaps / pop3s / smtps[选做]
在Postfix
基础配置中写入下列语句:
smtpd_tls_key_file = <你的加密证书>
smtpd_tls_cert_file = <你的CA证书>
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
tls_random_source = dev:/dev/urandom
<你的CA证书> <你的加密证书>参考上文apache2配置
在Postfix
端口配置中写入下列语句:/etc/postfix/master.cf
smtps inet n - n - - smtpd
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
在 Dovecot 的 ssl 配置中写入下列语句:
ssl = yes
ssl_cert = <<你的CA证书>
ssl_key = <<你的加密证书>
<你的CA证书> <你的加密证书>参考上文apache2配置 重启服务:
service dovecot restart
service postfix restart
以上,你的邮箱服务其就配置完成了
开启BBR优化
BBR是谷歌最新的TCP拥塞算法,效果较为优秀,我们当然要添加一波 虽然有要求版本4.9以上,但是我们 Debian 9默认刚好4.9,擦边球wq 那么执行下面的命令即可:echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p
sysctl net.ipv4.tcp_available_congestion_control
2×00 致谢与版权声明
在撰写本博文的时候,博主参考并整理许多博文,以下是主要部分,还有许多杂七杂八的小问题来自网上或来自自己的经验,并于此表示感谢: 除此之外,其他基本上没有引用 当然,如果有任何问题,希望你能提出并发于评论]]>