从0开始的服务器配置 — Debian 9 — apache2+php+mysql+phpmyadmin+smtp(s)+imap(s)

栏目: 服务器 · Apache · 发布时间: 7年前

内容简介:鉴于服务器要续费而且服务器内部实在是太乱了…所以有必要重做一次服务器,但是每一次搞服务器都是大坑,所以写一篇博文记录这次的服务器维护过程,给自己提示,也希望能够给初学者(虽然自己也是)予以帮助,因而,如果文章有不准确的地方或着错误的地方,欢迎评论或者邮箱纠正对于一台服务器而言,首先你需要选择一个合适的服务器系统,虽然说市面上大多使用Tips: 如果可以的话,建议在

写在前面

鉴于服务器要续费而且服务器内部实在是太乱了…所以有必要重做一次服务器,但是每一次搞服务器都是大坑,所以写一篇博文记录这次的服务器维护过程,给自己提示,也希望能够给初学者(虽然自己也是)予以帮助,因而,如果文章有不准确的地方或着错误的地方,欢迎评论或者邮箱纠正

0x00 基础搭建

对于一台服务器而言,首先你需要选择一个合适的服务器系统,虽然说市面上大多使用 Centosyum 类包管理器的系统,但是对于博主而言,因为平日电脑上使用的就是使用 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 , NginxLighttpd ,博主这里选择的是 Apache2

1×01 安装Apache2

apt-get install apache2
apt包管理器用起来真舒服

如果你是的服务器正常的话,运行下面的命令应该会与我的返回值 类似

< 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 选项,并且开启 headerhttp/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

其中 <域名> 为当前需要配置的域名如 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

myadmin

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 服务器运行环境配置

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 邮箱服务搭建

我们使用 postfixdovecot 相结合来搭建邮箱服务,当然不可能只有安装这么简单,我们还需要配置

apt-get install postfix libsasl2-2 sasl2-bin libsasl2-modules dovecot-imapd dovecot-pop3d dovecot-common
又一次证明apt管理的方便

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 致谢与版权声明

在撰写本博文的时候,博主参考并整理许多博文,以下是主要部分,还有许多杂七杂八的小问题来自网上或来自自己的经验,并于此表示感谢:

除此之外,其他 基本上 没有引用

当然,如果有任何问题,希望你能提出并发于评论


以上所述就是小编给大家介绍的《从0开始的服务器配置 — Debian 9 — apache2+php+mysql+phpmyadmin+smtp(s)+imap(s)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Hacking Growth

Hacking Growth

Sean Ellis、Morgan Brown / Crown Business / 2017-4-25 / USD 29.00

The definitive playbook by the pioneers of Growth Hacking, one of the hottest business methodologies in Silicon Valley and beyond. It seems hard to believe today, but there was a time when Airbnb w......一起来看看 《Hacking Growth》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

html转js在线工具
html转js在线工具

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换