Apache

CentOS7.6 Apache配置安全网页并搭建wordpress(Apache Mariadb Wordpress)

我们每天上网访问的页面都是存储在服务器中的,服务器通过运行http服务把网页发布到互联网上供人们使用。常见的http服务有这三种:Nginx、Apache、IIS。

配置Apache Mariadb Wordpress,其中Apache用于发布网站,Mariadb作为Wordpress的数据库,Wordpress则用于承担网站的内容。

​ 接下来,我会教大家如何配置Apache服务,并讲解其中的一些参数和理论。

下载Apache服务和其他服务包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@appsrv ~]# yum install -y mod_ssl openssl httpd
//mod_ssl是http的安全模块
//openssl用于签发证书
//httpd为Apache服务

[root@appsrv ~]# yum install -y php php-mysql mariadb* lrzsz
//下载php是因为wordpress文件是php格式,
//服务端要能解析php格式的文件才能把网站完整地显示出来
//mariadb作为wordpress的数据库
//lrzsz是用来在远程服务器上上传文件和下载文件的

自己去wordpress官网下载wordpress文件
但是要看wordpress和Apache、mariadb的版本是否兼容
我是用的wordpress-4.9.4-zh_CN.zip

点击并拖拽以移动

网站证书签发

​ 这里我用的openssl自带的脚本文件创建的证书文件,在创建过程中除非你知道你在干什么不然不要去改证书的文件名字和路径。创建完成后可以去更改证书文件。

CA根证书自签发

​ CA根证书发布,发布证书要注意commonName的要求。

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
[root@appsrv ~]# /etc/pki/tls/misc/CA -newca //openssl脚本创建ca根证书

CA certificate filename (or enter to create)

Making CA certificate ...
Generating a 2048 bit RSA private key
...........................................................................................+++
...........................................................................................................................................................................+++
writing new private key to '/etc/pki/CA/private/./cakey.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase: //输入密码,后面都要用这个密码
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
//这里按照自己的需求填就好,但是Common Name要注意一下
//我也不知道Common Name的CA根证书要求,但是尽量保证有Root CA这几个字吧
Country Name (2 letter code) [XX]:CN //国家简称
State or Province Name (full name) []:China //国家全称
Locality Name (eg, city) [Default City]:BeiJing //城市
Organization Name (eg, company) [Default Company Ltd]:skills //组织名
Organizational Unit Name (eg, section) []:Operations Departments //组织单位名称
Common Name (eg, your name or your server's hostname) []:CSK Global Root CA //通用名
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for /etc/pki/CA/private/./cakey.pem:
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number:
ee:45:5e:8b:1f:a7:00:5e
Validity
Not Before: Jan 10 13:34:46 2024 GMT
Not After : Jan 9 13:34:46 2027 GMT
Subject:
countryName = CN
stateOrProvinceName = China
organizationName = skills
organizationalUnitName = Operations Departments
commonName = CSK Global Root CA
X509v3 extensions:
X509v3 Subject Key Identifier:
62:08:B0:98:FF:F5:98:7E:4E:1B:3F:BE:08:7E:30:F3:E6:BF:0E:90
X509v3 Authority Key Identifier:
keyid:62:08:B0:98:FF:F5:98:7E:4E:1B:3F:BE:08:7E:30:F3:E6:BF:0E:90

X509v3 Basic Constraints:
CA:TRUE
Certificate is to be certified until Jan 9 13:34:46 2027 GMT (1095 days)

Write out database with 1 new entries
Data Base Updated
[root@routersrv ~]#

点击并拖拽以移动

web证书签发

​ 申请web证书,然后签发web证书。web证书的参数除了commonName是*.chinaskills.cn类似的域名格式,其他参数和根CA证书保持一致。

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
[root@routersrv ~]# /etc/pki/tls/misc/CA -newreq    //申请证书
Generating a 2048 bit RSA private key
....................................................................................................+++
............+++
writing new private key to 'newkey.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:China
Locality Name (eg, city) [Default City]:BeiJing
Organization Name (eg, company) [Default Company Ltd]:skills
Organizational Unit Name (eg, section) []:Operations Departments
Common Name (eg, your name or your server's hostname) []:*.chinaskills.cn
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Request is in newreq.pem, private key is in newkey.pem
[root@routersrv ~]#

点击并拖拽以移动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@routersrv ~]# /etc/pki/tls/misc/CA -sign    //签发证书

Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for /etc/pki/CA/private/cakey.pem:
Check that the request matches the signature
Signature ok
...

Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
...
Signed certificate is in newcert.pem
[root@routersrv ~]# ls
anaconda-ks.cfg newcert.pem newkey.pem newreq.pem
[root@routersrv ~]#

点击并拖拽以移动

Apache服务配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@appsrv ~]# vim /etc/httpd/conf
conf/ conf.d/ conf.modules.d/
配置文件 执行文件 模板

conf/中我们只关系apache主配置文件(/etc/httpd/conf/httpd.conf)就可以了
//apache主配置文件中的配置相当于默认的全局网页配置文件,如果你什么都没有配置
//那么你的网站将按照apache主配置文件中的配置执行;你没配置到某个参数,但是网站
//需要这个参数时,它会遵循apache主配置文件中的配置执行;

conf.d/中存放的是各种网站、配置执行文件,其中ssl.conf welcome.conf是我们需要参考的
[root@appsrv ~]# vim /etc/httpd/conf.d/
ssl.conf welcome.conf

conf.d/中的后缀为.conf的文件都会在apache启动的时候执行;我们可以写一个
局部的网页文件,用于配置我们想要的网页效果;

点击并拖拽以移动

Apache主文件配置

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
[root@appsrv ~]# useradd -r webuser    //创建apache系统用户
[root@appsrv ~]# vim /etc/httpd/conf/httpd.conf

ServerRoot "/etc/httpd" //apache服务的根目录

#Listen 12.34.56.78:80 //监听主机
Listen 80 //监听端口

#User apache
#Group apache
User webuser
Group webuser
//apache的系统用户和组,apache服务会用这个系统用户和组在系统上运行

<Directory /> //默认文件目录权限
AllowOverride none
Require all denied
</Directory>

//.htaccess(分布式隐含配置文件):提供了针对每个目录改变配置的方法,
//即在一个特定的目录中放置一个包含特定指令的文件,其中的指令作用于此目录及其所有子目录

//当AllowOverride设置成None时,相应的配置目录下的.htaccess文件是不被读取的,即无法生效。
//当AllowOverride设置成All时,每一次请求访问相应目录下的文件时,都会读取.htaccess文件的配置,
//意味着原Apache指令会被.htaccess文件中的指令重写
//从性能和安全性考虑,一般都尽可能避免使用.htaccess文件,任何希望放在.htaccess文件中的配置,
//都可放在主配置文件(httpd.conf)的 <Directory>段中,而且高效。
//因此AllowOverride属性一般都配置成None

//Require all granted:允许所有主机访问
//Require all denied:拒绝所有主机访问
//Require local:仅允许本地主机访问
//Require [not] host <主机名或域名列表>:允许或拒绝指定主机或域名访问
//Require [not] ip <IP 地址或网段列表>:允许或拒绝指定 IP 地址网络访问

DocumentRoot "/var/www/html" //指定网站的根目录

点击并拖拽以移动

虚拟主机配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@appsrv ~]# vim /etc/httpd/conf.d/vhost.conf

//这里的vhost.conf文件是新建的一个虚拟主机配置文件
//虚拟主机文件中可以写多个网页配置

<VirtualHost *:80>
redirect permanent / https://www.chinaskills.cn
</VirtualHost>
//访问所有网页重定向到https://www.chinaskills.cn

<VirtualHost 192.168.100.100:443>
DocumentRoot "/webdata/wordpress/" //指定网站的根目录
ServerName www.chinaskills.cn //设置网页域名
<Directory "/webdata/wordpress/"> //设置目录权限
AllowOverride None
Require all granted
</Directory>
SSLEngine on //开启ssl证书认证
SSLCertificateFile /csk-rootca/httpd.crt
SSLCertificateKeyFile /csk-rootca/httpd.key
</VirtualHost>

点击并拖拽以移动

Mariadb配置

Mariadb初始化

​ Mariadb需要创建一个wordpress库。顺便创建一个root用户来管理Mariadb数据库。

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
[root@appsrv ~]# systemctl restart mariadb
[root@appsrv ~]# systemctl enable mariadb
//必须先启用mariadb,因为下载完后默认不开启

[root@storagesrv ~]# mysql_secure_installation //初始化数据库

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y //设置root密码
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y //是否移除匿名用户
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n //是否禁止root远程登陆
... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y //是否移除测试数据库和其访问权
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y //重载privilege tables,就是保存
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[root@appsrv ~]#

点击并拖拽以移动

Mariadb配置

​ Mariadb初始化完成后,进入mariadb进行配置

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
[root@appsrv ~]# mysql -u root -p //使用root用户和密码登录
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

//创建wordpress库
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

//创建root用户并赋予所有权限
MariaDB [(none)]> grant all privileges on *.* to 'root'@'%' identified by '000000' with grant option;
Query OK, 0 rows affected (0.00 sec)

//保存配置
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

//退出数据库
MariaDB [(none)]> exit
Bye
[root@appsrv ~]#

点击并拖拽以移动

Wordpress导入

​ 在拥有了wordpress包后,把他上传到服务器上。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@appsrv /]# mkdir /webdata
mkdir: cannot create directory ‘/webdata’: File exists
[root@appsrv /]# cd /webdata/
[root@appsrv webdata]# ls
[root@appsrv webdata]# rz
rz waiting to receive.
zmodem trl+C ȡ
正在传输 wordpress-4.9.4-zh_CN.zip...
100% 9617 KB 9617 KB/ 00:00:01 0

[root@appsrv webdata]# ls
wordpress-4.9.4-zh_CN.zip
[root@appsrv webdata]#unzip wordpress-4.9.4-zh_CN.zip
[root@appsrv webdata]# ls
wordpress wordpress-4.9.4-zh_CN.zip
[root@appsrv webdata]#

点击并拖拽以移动

​ 改wordpress文件的权限,因为mariadb对wordpress初始化时需要权限去访问wordpress文件。当然你也可以去wordpress的配置文件中添加数据库的信息,这样就不用改权限。但是图方便直接改个权限就好了。

1
[root@appsrv webdata]# chmod 777 -R /webdata/wordpress

点击并拖拽以移动

​ 重启httpd服务放行防火墙和selinux或者关掉防火墙和selinux

1
2
3
4
5
[root@appsrv ~]# systemctl restart httpd
[root@appsrv webdata]# systemctl stop firewalld
[root@appsrv webdata]# systemctl disable firewalld
[root@appsrv webdata]# yum remobe -y selinux*
//没把防火墙直接删了,因为它和dns有些联系,删了会出问题

点击并拖拽以移动

完成

现在你就可以通过www.chinaskills.cn去访问wordpress了,接下来就看着填信息就好了。

限制Web服务只能使用系统500M物理内存

​ 服务自启动,其实是把/usr/lib/systemd/system/里的服务链接到/etc/systemd/system/multi-user.target.wants/中

​ 因为systemd开机时会去运行/etc/systemd/system/里的服务

​ 我们去/usr/lib/systemd/system/httpd.service文件中进行修改就能实现限制内存功能

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
[root@appsrv webdata]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

[root@appsrv webdata]# vim /usr/lib/systemd/system/httpd.service //进入文件

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

memory_limit_in_bytes=500*1024*1024 //添加这一条

[Install]
WantedBy=multi-user.target

点击并拖拽以移动

1
2
3
4
[root@appsrv webdata]# systemctl daemon-reload //重载服务
[root@appsrv webdata]# systemctl restart httpd //重启httpd
Enter SSL pass phrase for www.chinaskills.cn:443 (RSA) : ****
[root@appsrv webdata]#

点击并拖拽以移动

更改文件大小

1
[root@appsrv ~]# dd if=/dev/zero of=text.mp4 bs=100M count=1

点击并拖拽以移动