欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

CentOS 上搭建 PHP7 开发测试环境

程序员文章站 2024-03-07 18:38:09
最近公司增加了一台测试用的服务器,当然因为只是测试用,所以决定所有东西都装到一个服务器上,用来我们自己撸代码,发测试版功能,做点小实验神码的,等等等等……反正就是方便用吧,...

最近公司增加了一台测试用的服务器,当然因为只是测试用,所以决定所有东西都装到一个服务器上,用来我们自己撸代码,发测试版功能,做点小实验神码的,等等等等……反正就是方便用吧,所以自己来试了下搭建一台完全的 lamp 开发/测试环境,当然同样踩到了无数的坑。

准备开始吧!

step.1 准备服务器

那第一步,我们先搞个服务器吧,搞啥服务器呢,既然是测试用那就搞点不要钱的,或者方便的吧,这里就推荐大家试试用 aws 或者自己机器上搭建虚拟机,比如 vagrant 这样的开发平台也是可以的,这里,我们就用 aws 了,反正可以免费一年, aws 的优点是访问国外网站超快,缺点自然是国内访问超慢,当然大家用啥都可以的啦,这里假设我们已经有一台 centos 7 的服务器了,并且假设你可以远程链接到这台服务器(比如 ssh )。

setp.2 安装配置 lamp

首先我们安装 apache
安装 apache 很简单,只需要通过 yum 安装就可以了。

yum install httpd

安装完成后,使用 systemctl 命令启动,并设置为开机自动启动。

systemctl start httpd.service
systemctl enable httpd.service

出现问题时可以通过 systemctl status 来查看服务是否正确的启动了,也可以到 /var/log/httpd 路径下看访问日志和错误日志来排查问题。

新的 centos 已经装了 firewalld 了,所以如果装了这个,我们还需要告诉 firewalld 打开防火墙。

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload


接下来装 mysql

首先,我们要知道的是, mysql 没有在 centos 7 的软件源库里面, centos 7 里面有对应的替代品 mariadb ,因为龟壳公司收购了 mysql ,大家怕万一将来 mysql 闭源了,那不都跪了,于是就开了个分支自己开始做起 mariadb 来了,这里我们还是手工添加 mysql 的软件源来安装 mysql 保证还是那个味道。

首先添加 mysql 的软件源。

yum install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

接下来安装 mysql 的客户端和服务端。

yum install mysql-server mysql-client

安装完成后,使用 mysql_secure_installation 来完成 mysql 的安装配置。

同样需要为 mysql 配置防火墙。

firewall-cmd --permanent --zone=public --add-service=mysql
firewall-cmd --reload

 
接下来我们安装 php 7

跟 mysql 类似,目前发行包的软件源里面是不包含 php 7 ,只包含了 php 5.x ,所以为了安装 php 7 我们同样需要添加 php 7 的软件源,根据这几天的实验情况看, remi 的 php 7 的软件包和插件比较完善,所以我们就先添加 remi 的软件源。

yum install scl-utils
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

添加完成软件源后,我们就可以安装 php 7 和各种常用的包了。

yum install php70 php70-php-mysqlnd php70-php-curl php70-php-simplexml
yum install php70-php-devel php70-php-gd php70-php-json php70-php-mcrypt
yum install php70-php-mbstring php70-php-opcache php70-php-pear
yum install php70-php-pecl-apcu php70-php-pecl-geoip php70-php-pecl-imagick
yum install php70-php-pecl-json-post php70-php-pecl-memcache php70-php-pecl-memcached
yum install php70-php-pecl-xmldiff php70-php-pecl-zip php70-php-pspell php70-php-soap
yum install php70-php-tidy php70-php-xml php70-php-xmlrpc

接下来呢,这里其实还没装完,因为最初我是为了升级服务器 php 5.x 而装的 php 7 ,所以如果你的服务器已经装了 php 5.x 需要简单的升级下,那么在这里安装完成 php 7 的软件包以后删除 php 5.x 然后在安装 php70-php 这个包。

yum remove php php-common

然后

yum install php70-php 

之后修改配置文件重启 apache 就可以了,要注意的是, php 7 的配置文件放到了 /etc/opt/remi/php70 路径下, php 7 的相关文件放到了 /opt/remi/php70/root/lib64/php 路径下。

好,这样,我们一个带着 php 7 的服务器就装好了,大家可以在上面*玩耍享受 php 7 带来的性能。

setp.3 安装 sasl 和 memcached

首先 memcached 大家都知道是什么东西了,那么我来说说 sasl 是什么鬼。 sasl 全称 simple authentication and security layer 用来做安全机制验证的,说简单点,就是用这个东西,我们可以让我们的 memcached 在访问前需要验证下用户名密码,另外 memcached 的 binary 接口比需要使用 sasl 验证,否则就会输出 writing an error: unknown command 的错误(然而在 php 5.x 下,却正常。。。所以这个问题研究了好会儿)。

我们先安装 sasl 。

yum install cyrus-sasl-plain
yum install cyrus-sasl-devel
yum install cyrus-sasl
yum install cyrus-sasl-lib
yum install cyrus-sasl-gssapi
yum install cyrus-sasl-md5

安装完成后,通过 systemctl start saslauthd.service 命令启动这个服务,接下来,我们需要创建一个用户通过 sasl 验证来访问 memcached 。

首先我们要修改 sasl 的配置来使用当前 /etc/shadow 中的用户账户以及密码来进行验证,所以我们修改 /etc/sysconfig/saslauthd 中的 mech=shadow 告诉 sasl 使用系统的账户密码来验证,修改完后通过 systemctl restart saslauthd.service 重启 sasl 的进程。

可以通过下面的命令来验证用户是否可以正确的被验证了。

/usr/sbin/testsaslauthd -u username -p password

成功将会显示 0: ok “success.” ,要注意的是这里的 username 和 password 是需要能够正常登录系统的用户名和密码,也就是需要记录在 /etc/shadow 文件中。

接下来我们安装 memcached 需要的 libevent 。

yum install libevent libevent-devel

再接下来,我们需要通过源代码编译安装 memcached ,因为默认在 yum 中的 memcached 是没有开启 sasl 验证。

wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
tar zxvf memcached-1.4.15.tgz
cd memcached-1.4.15
./configure --enable-sasl --with-php-config=/opt/remi/php70/root/bin/php-config
make
make install


这样我们的 memcached 也编译并安装完成了,接下来为 memcached 的服务添加 sasl 验证和防火墙规则。

saslpasswd2 -a memcached -c [用来访问 memcached 用户]
firewall-cmd --permanent --zone=public --add-port=11211/tcp
firewall-cmd --reload


接下来我们启动 memcached 。

/usr/local/bin/memcached -d -u [用来访问 memcached 的用户] -p 11211 -m 512 -c 1024 -s

上面参数中 -s 就是告诉 memcached 需要开启 sasl 验证。

step.end

到此,开发环境的配置就结束了,大家愉快的玩耍吧,欢迎掷砖>_<