LCMP环境搭建 | 基于CentOS7.5

这里的LCMP是指一种类似于LAMP,LNMP的技术栈(虽然没有LCMP这个词)

LCMP分别指Linux,Caddy,MariaDB,PHP

CentOS官方库的MariaDB和PHP版本比较旧,且没有Caddy软件包,想要安装Caddy,或10.0及以上版本的MariaDB,或7.0及以上版本的PHP,需要通过额外的仓库来安装。具体参见另一篇文章CentOS | 社区版的企业操作系统来安装额外的仓库,然后回到这里继续

以下所有操作都将在root用户下进行

Caddy

Caddy和Apache、Nginx一样是HTTP服务器,但相对于它俩,Caddy配置简单(不代表可用性低,人家Caddy的并发不输Nginx),自动HTTPS,非常适合个人使用

安装

运行下面这条命令来安装

1
2
yum install caddy
systemctl start caddy.service

现在可以通过浏览器输入主机的IP进行访问了,会显示Caddy的欢迎页面

配置

配置文件的目录在/etc/caddy目录下

配置请参考Caddy官方文档

测试

见php的测试

MariaDB

MariaDB是MySQL的分之,由MySQL的创始人主导开发,完全兼容MySQL,并且增添了许多功能,是MySQL的一个不错的替代品。目前各大发行版的MySQL包都被替换为MariaDB了

安装

先删除CentOS自带的MariaDB依赖,否则依赖会冲突,然后安装较新的MariaDB

1
2
yum remove mariadb-libs
yum install mariadb101u-server

也可以按照官网的指导来安装最新版

配置

启动MariaDB并进行数据库级别的配置

1
2
systemctl start mariadb
mysql_secure_installation

按照提示进行配置,除了输入两次密码,其他操作都是回车

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

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]
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]
... 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]
... Success!

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]
- 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]
... 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!

测试

1
mysql -u root -p

然后输入密码,成功会有以下提示

1
2
3
4
5
6
7
8
9
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.1.35-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.

MariaDB [(none)]>

(可按Ctrl+D退出

PHP

PHP是一门网页动态脚本语言,它有易于学习且开发快速的优点,非常适合于中小型网站开发

安装

1
yum install php72u php72u-cli php72u-mysqlnd php72u-fpm

配置

编辑/etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000加上英文分号;以注释掉
并去掉;listen = /run/php-fpm/www.sock的注释
然后复制一行;listen.acl_users = nginx,去掉注释并把nginx改为caddy

测试

1
2
3
systemctl start php-fpm.service
mkdir /var/www/html
echo -e "<?php\nphpinfo();\n?>" > /var/www/html/info.php

html目录将会是网站的根目录

编辑/etc/caddy/caddy.conf,把root /usr/share/caddy改成root /var/www/html,并添加fastcgi / /run/php-fpm/www.sock php

打开浏览器输入主机IP和/info.php,可以看到php的信息页面

参见

  1. CentOS | 社区版的企业操作系统
  2. Caddy User Guide
  3. MariaDB - Setting up MariaDB Repositories - MariaDB