CentOS7.3搭建Apache+PHP7+web SVN+MariaDB Web服务器(2017-08-20)

前端之家收集整理的这篇文章主要介绍了CentOS7.3搭建Apache+PHP7+web SVN+MariaDB Web服务器(2017-08-20)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

注意:本教程使用干净的 CentOS 7进行安装,如果您已安装其他环境或软件,涉及到内核升级,请您妥善备份,可能导致您已安装的软件不兼容或出现其他问题。

免责声明:本教程仅以个人经验撰写,未必适合所有系统环境。如在使用本教程途中,出现无法挽救的损失(如数据丢失等),本人概不负责。

再次提醒:使用本教程前请妥善备份现有数据!使用本教程前请妥善备份现有数据!使用本教程前请妥善备份现有数据!

如果您使用本教程的方法过程中出现问题,您可留言,我将在能力范围内尽可能协助解决

本文最终服务器环境配置:

  1. Apache 2.7.27
  2. subversion1.9.7
  3. MariaDB10.2.28
  4. PHP7.1.8

下面开始进入教程吧。

一. 升级内核和软件包

  1. [root@instance-l79ltvo6 ~]# yum -y update
  2. ...
  3. Complete!

直到控制台输出Complete!说明升级完成,最好还是重启一下吧

  1. [root@instance-l79ltvo6 ~]# reboot

二. 安装 apache 2.4.27

先安装一些基本依赖

  1. [root@instance-l79ltvo6 ~]# yum install -y gcc gcc-c++ openssl-devel zlib-devel
  2. //在root目录下创建一个soft文件夹,我们本次教程的软件将都放在该目录下
  3. [root@instance-l79ltvo6 ~]# mkdir soft
  4. [root@instance-l79ltvo6 ~]# cd soft

然后开始安装apr、apr-util、pcre 3个依赖,最后再安装 apache

1. 安装 apr

  1. [root@instance-l79ltvo6 ~]# wget https://mirror.tuna.tsinghua.edu.cn/apache/apr/apr-1.6.2.tar.gz
  2. [root@instance-l79ltvo6 ~]# tar zxf apr-1.6.2.tar.gz
  3. [root@instance-l79ltvo6 ~]# cd apr-1.6.2/
  4. [root@instance-l79ltvo6 ~]# ./configure --prefix=/usr/local/apr
  5. [root@instance-l79ltvo6 ~]# make && make install
  6. [root@instance-l79ltvo6 ~]# cd .. //返回上级目录

因为我们使用了 apr 1.6.2 的依赖,所以下面我们必须要用 apr-util 1.6.0 版本,apr-util 1.6.0
不再捆绑安装 expat ,但又需要 expat 的支持,所以我们得手动先安装 expat。不然编译 apache 的时候会报错。

2 安装 expat

  1. [root@instance-l79ltvo6 ~]# wget https://sourceforge.net/projects/expat/files/expat/2.2.3/expat-2.2.3.tar.bz2
  2. [root@instance-l79ltvo6 ~]# tar jxf expat-2.2.3.tar.bz2
  3. [root@instance-l79ltvo6 ~]# cd expat-2.2.3/
  4. [root@instance-l79ltvo6 ~]# ./configure --prefix=/usr/local/expat
  5. [root@instance-l79ltvo6 ~]# make && make install
  6. [root@instance-l79ltvo6 ~]# cd .. //返回上级目录

3 安装 apr-util

  1. [root@instance-l79ltvo6 ~]# wget https://mirror.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.0.tar.gz
  2. [root@instance-l79ltvo6 ~]# tar zxf apr-util-1.6.0.tar.gz
  3. [root@instance-l79ltvo6 ~]# cd apr-util-1.6.0/
  4. [root@instance-l79ltvo6 ~]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr --with-expat=/usr/local/expat
  5. [root@instance-l79ltvo6 ~]# make && make install
  6. [root@instance-l79ltvo6 ~]# cd .. //返回上级目录

4 安装 pcre

  1. [root@instance-l79ltvo6 ~]# wget http://sourceforge.mirrorservice.org/p/pc/pcre/pcre/8.41/pcre-8.41.tar.gz
  2. [root@instance-l79ltvo6 ~]# tar zxf pcre-8.41.tar.gz
  3. [root@instance-l79ltvo6 ~]# cd pcre-8.41/
  4. [root@instance-l79ltvo6 ~]# ./configure --prefix=/usr/local/pcre
  5. [root@instance-l79ltvo6 ~]# make && make install
  6. [root@instance-l79ltvo6 ~]# cd .. //返回上级目录

5 安装 apache

  1. [root@instance-l79ltvo6 ~]# wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.27.tar.gz
  2. [root@instance-l79ltvo6 ~]# tar zxf httpd-2.4.27.tar.gz
  3. [root@instance-l79ltvo6 ~]# cd httpd-2.4.27/
  4. [root@instance-l79ltvo6 ~]# ./configure \
  5. --prefix=/usr/local/apache \
  6. --with-apr=/usr/local/apr \
  7. --with-apr-util=/usr/local/apr-util \
  8. --with-pcre=/usr/local/pcre \
  9. --with-ssl \
  10. --with-zlib \
  11. --with-mpm=worker \
  12. --enable-rewrite \
  13. --enable-so \
  14. --enable-ssl \
  15. --enable-cache \
  16. --enable-disk-cache \
  17. --enable-file-cache \
  18. --enable-mem-cache \
  19. --enable-headers \
  20. --enable-expires \
  21. --enable-deflate \
  22. --enable-dav \
  23. --enable-dav-fs \
  24. --enable-cgi \
  25. --enable-proxy \
  26. --enable-proxy-fcgi
  27. //这里请根据自身实际情况开启相关模块
  28. [root@instance-l79ltvo6 ~]# make && make install
  29. [root@instance-l79ltvo6 ~]# cd .. //返回上级目录

6 修改 apache 配置文件

编辑/usr/local/apache/conf/httpd.conf

  1. #LoadModule ssl_module modules/mod_ssl.so //去掉#开启 SSL
  2. #LoadModule rewrite_module modules/mod_rewrite.so //去掉#开启 rewrite
  3. #ServerName www.example.com:80 //去掉#并把 www.example.com:80 修改为你的IP:80或者域名
  4. #Include conf/extra/httpd-vhosts.conf //去掉#,开启虚拟主机配置
  5. //如果你需要安装svn服务,你需要开启
  6. #LoadModule dav_module modules/mod_dav.so//去掉#

找到以下代码并更换

  1. <Directory />
  2. AllowOverride none
  3. Require all denied
  4. </Directory>
  5. //修改
  6. <Directory />
  7. Options Indexes FollowSymLinks //如不需要显示目录,把 Indexes 去掉
  8. AllowOverride ALL //开启rewrite
  9. Require all granted
  10. </Directory>

编辑/usr/local/apache/conf/extra/httpd-vhosts.conf,删除全部,并添加下列代码

  1. <VirtualHost *:80>
  2. DocumentRoot "/usr/local/apache/htdocs"
  3. ServerName your IP //你的IP地址
  4. ErrorLog "logs/你的IP-error_log"
  5. CustomLog "logs/你的IP-access_log" common
  6. </VirtualHost>

7 添加启动服务

  1. [root@instance-l79ltvo6 ~]# cp /usr/local/apache/bin/apachectl /etc/rc.d/init.d/
  2. [root@instance-l79ltvo6 ~]# mv /etc/rc.d/init.d/apachectl /etc/rc.d/init.d/httpd
  3. [root@instance-l79ltvo6 ~]# cd /etc/rc.d/init.d/

编辑 httpd,在 #!/bin/sh 下面添加下面代码

  1. # Comments to support chkconfig on RedHat Linux
  2. # chkconfig: 2345 90 90
  3. # description:http server

注册服务

  1. [root@instance-l79ltvo6 ~]# chkconfig --add httpd
  2. [root@instance-l79ltvo6 ~]# chkconfig httpd on

把 apache 加入系统环境变量

  1. [root@instance-l79ltvo6 ~]# vim /etc/profile.d/httpd.sh
  2. //写入
  3. export PATH=$PATH:/usr/local/apache/bin
  4. //保存后赋予执行权限
  5. [root@instance-l79ltvo6 ~]# chmod 0777 /etc/profile.d/httpd.sh
  6. [root@instance-l79ltvo6 ~]# source /etc/profile.d/httpd.sh

8 启动 apache

首先检查配置文件是否出错

  1. [root@instance-l79ltvo6 ~]# /usr/local/apache/bin/apachectl -t
  2. Syntax OK //说明没问题,可以直接启动

启动 apache

  1. [root@instance-l79ltvo6 ~]# systemctl start httpd.service

然后打开浏览器,输入你的IP地址,看到It works!,说明apache成功启动了

  1. It works!

三. 安装 subversion 1.9.7

(如不需要svn服务请跳过)

1 安装 scons

  1. [root@instance-l79ltvo6 ~]# cd /root/soft
  2. [root@instance-l79ltvo6 ~]# wget http://sourceforge.mirrorservice.org/s/sc/scons/scons/2.5.1/scons-2.5.1.tar.gz
  3. [root@instance-l79ltvo6 ~]# tar zxf scons-2.5.1.tar.gz
  4. [root@instance-l79ltvo6 ~]# cd scons-2.5.1/
  5. [root@instance-l79ltvo6 ~]# python setup.py install --prefix=/usr/local/scons
  6. [root@instance-l79ltvo6 ~]# cd .. //返回上级目录

2 安装 serf

  1. [root@instance-l79ltvo6 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/serf/serf-1.3.9.tar.bz2
  2. [root@instance-l79ltvo6 ~]# tar xf serf-1.3.9.tar.bz2
  3. [root@instance-l79ltvo6 ~]# cd serf-1.3.9/
  4. [root@instance-l79ltvo6 ~]# /usr/local/scons/bin/scons prefix=/usr/local/serf APR=/usr/local/apr APU=/usr/local/apr-util
  5. [root@instance-l79ltvo6 ~]# /usr/local/scons/bin/scons install
  6. [root@instance-l79ltvo6 ~]# cd .. //返回上级目录

3 编译 subverion

  1. [root@instance-l79ltvo6 ~]# wget http://www.sqlite.org/2017/sqlite-amalgamation-3190300.zip
  2. [root@instance-l79ltvo6 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/subversion/subversion-1.9.7.tar.gz
  3. [root@instance-l79ltvo6 ~]# tar zxf subversion-1.9.7.tar.gz
  4. [root@instance-l79ltvo6 ~]# unzip sqlite-amalgamation-3190300.zip
  5. [root@instance-l79ltvo6 ~]# mv /root/soft/sqlite-amalgamation-3190300 /root/soft/subversion-1.9.7/sqlite-amalgamation
  6. [root@instance-l79ltvo6 ~]# cd subversion-1.9.7/
  7. [root@instance-l79ltvo6 ~]# ./configure \
  8. --prefix=/usr/local/svn \
  9. --with-apr=/usr/local/apr \
  10. --with-apr-util=/usr/local/apr-util \
  11. --with-serf=/usr/local \
  12. --enable-mod-activation \
  13. --with-apache-libexecdir=/usr/local/apache/modules \
  14. --with-apxs=/usr/local/apache/bin/apxs \
  15. --without-berkeley-db
  16. [root@instance-l79ltvo6 ~]# make && make install

给SVN创建一个名为svn的非登录用户

  1. [root@instance-l79ltvo6 ~]# useradd svn -s /sbin/nologin

把svn加入到系统环境变量

  1. [root@instance-l79ltvo6 ~]# vim /etc/profile.d/svn.sh
  2. //添加
  3. export PATH=$PATH:/usr/local/svn/bin
  4. 保存后赋予执行权限
  5. [root@instance-l79ltvo6 ~]# chmod 0777 /etc/profile.d/svn.sh
  6. [root@instance-l79ltvo6 ~]# source /etc/profile.d/svn.sh

在/etc/ld.so.conf.d/创建一个serf-1.3.9.conf,指定lib目录,不然svn启动会报错

  1. [root@instance-l79ltvo6 ~]# vim /etc/ld.so.conf.d/serf-1.3.9.conf
  2. //添加
  3. /usr/local/lib
  4. 保存后刷新
  5. [root@instance-l79ltvo6 ~]# /sbin/ldconfig -v

4 配置 subverion

下面我们先创建一个 test 项目

  1. [root@instance-l79ltvo6 ~]# mkdir -p /data/svn
  2. [root@instance-l79ltvo6 ~]# cd /data/svn
  3. [root@instance-l79ltvo6 ~]# svnadmin create test

然后我们打开 /data/svn/test /就会发现里面自动创建了一些目录

  1. conf
  2. db
  3. format
  4. hooks
  5. locks
  6. README.txt

在conf里面的文件就是配置该项目的人员和权限,但如果多个项目,开发人员一样,那就要配置很多次了,这样很麻烦,所以我们要弄成配置一次权限就能直接应用到所有项目里面。

  1. [root@instance-l79ltvo6 ~]# cp /data/svn/test/conf/authz /data/svn/authz
  2. [root@instance-l79ltvo6 ~]# cp /data/svn/test/conf/passwd /data/svn/passwd
  3. [root@instance-l79ltvo6 ~]# cp /data/svn/test/conf/svnserve.conf /data/svn/svnserve.conf
  4. //然后设置一个用户密码,这里以创建root用户为例,这里的演示是将密码加密而非明文存储
  5. [root@instance-l79ltvo6 ~]# htpasswd -c /data/svn/passwd root
  6. New password: //输入密码
  7. Re-type new password: //再次输入密码

这样 root 用户就创建完成了,创建其他用户同理。 接下来我们需要修改配置文件

  1. [root@instance-l79ltvo6 ~]# vim /data/svn/svnserve.conf
  2. //删除所有内容增加下面的代码
  3. [general]
  4. anon-access = read
  5. auth-access = write
  6. password-db = passwd
  7. authz-db = authz
  8. [sasl]
  9. //保存
  10. [root@instance-l79ltvo6 ~]# vim /data/svn/authz
  11. //删除所有内容增加下面的代码
  12. [aliases]
  13. # joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil,Ltd./OU=Research Institute/CN=Joe Average
  14.  
  15. [groups]
  16. administrators = root
  17.  
  18. [/]
  19. @administrators=rw
  20.  
  21. [test:/]
  22. * = r
  23. //保存

我们尝试启动下svn服务

  1. [root@instance-l79ltvo6 ~]# svnserve --config-file /data/svn/svnserve.conf -d -r /data/svn

没有报错,说明启动成功了
把/data/svn/目录的拥有者更改为svn

  1. [root@instance-l79ltvo6 ~]# chown -R svn:svn /data/svn

最后我们需要修改 apache 的相关配置文件
编辑/usr/local/apche/conf/httpd.conf

  1. //找到
  2. User daemon
  3. Group daemon
  4. //修改
  5. User svn
  6. Group svn

编辑/usr/local/apache/conf/extra/httpd-vhost.conf,把刚刚那次编辑的内容更换为

  1. <VirtualHost *:80>
  2. ServerName 你的IP
  3. ErrorLog "logs/你的IP-error_log"
  4. CustomLog "logs/你的IP-access_log" common
  5. <Location /svn>
  6. DAV svn
  7. #support more repositories
  8. SVNParentPath /data/svn
  9.  
  10. #list all repositories
  11. #SVNListParentPath on
  12. AuthType Basic
  13. AuthName "Please input Username and Password"
  14. AuthUserFile /data/svn/passwd
  15. AuthzSVNAccessFile /data/svn/authz
  16. Require valid-user
  17. </Location>
  18. </VirtualHost>

然后停止 apache 服务

  1. [root@instance-l79ltvo6 ~]# systemctl stop httpd.service

检查 apache 配置文件是否出错

  1. [root@instance-l79ltvo6 ~]# /usr/local/apache/bin/apachectl -t
  2. Syntax OK //说明没问题,可以直接启动

重新启动 apache

  1. [root@instance-l79ltvo6 ~]# systemctl start httpd.service

打开浏览器访问http://你的ip/svn/test,提示输入账号密码 也可以使用TortoiseSVN进行操作
输入刚刚设置的账号密码后显示

  1. test - Revision 0: /

说明配置成功,apache+svn服务启动成功

四. 安装 MariaDB 10.2.8

参考https://segmentfault.com/a/11...,把相关版本号更换为10.2.8即可

五. 安装 PHP 7.1.8

先安装基本依赖

  1. [root@instance-l79ltvo6 ~]# yum -y install gcc gcc-c++ autoconf automake libtool re2c flex bison PHP-mcrypt libmcrypt libmcrypt-devel openssl-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel zlib-devel mcrypt bzip2-devel libicu-devel systemd-devel mhash postgresql-devel libxslt libxslt-devel

编译 PHP

  1. [root@instance-l79ltvo6 ~]# cd /root/soft
  2. [root@instance-l79ltvo6 ~]# wget http://cn.PHP.net/distributions/PHP-7.1.8.tar.gz
  3. [root@instance-l79ltvo6 ~]# tar zxf PHP-7.1.8.tar.gz
  4. [root@instance-l79ltvo6 ~]# cd PHP-7.1.8/
  5. [root@instance-l79ltvo6 ~]# ./configure \
  6. --prefix=/usr/local/PHP \
  7. --with-apxs2=/usr/local/apache/bin/apxs \
  8. --with-curl \
  9. --with-freetype-dir \
  10. --with-gd \
  11. --with-gettext \
  12. --with-iconv-dir \
  13. --with-kerberos \
  14. --with-libdir=lib64 \
  15. --with-libxml-dir \
  16. --with-MysqLi \
  17. --with-openssl \
  18. --with-pcre-regex \
  19. --with-pdo-MysqL \
  20. --with-pdo-sqlite \
  21. --with-pear \
  22. --with-png-dir \
  23. --with-xmlrpc \
  24. --with-xsl \
  25. --with-zlib \
  26. --with-mhash \
  27. --enable-fpm \
  28. --enable-bcmath \
  29. --enable-libxml \
  30. --enable-inline-optimization \
  31. --enable-gd-native-ttf \
  32. --enable-mbregex \
  33. --enable-mbstring \
  34. --enable-opcache \
  35. --enable-pcntl \
  36. --enable-shmop \
  37. --enable-soap \
  38. --enable-sockets \
  39. --enable-sysvsem \
  40. --enable-xml \
  41. --enable-zip \
  42. --enable-MysqLnd
  43. [root@instance-l79ltvo6 ~]# make && make install
  44. ... //漫长的等待

编译成功

  1. [PEAR] Archive_Tar - installed: 1.4.3
  2. [PEAR] Console_Getopt - installed: 1.4.1
  3. [PEAR] Structures_Graph- installed: 1.1.1
  4. [PEAR] XML_Util - installed: 1.4.2
  5. [PEAR] PEAR - installed: 1.10.5
  6. Wrote PEAR system config file at: /usr/local/PHP/etc/pear.conf
  7. You may want to add: /usr/local/PHP/lib/PHP to your PHP.ini include_path
  8. /root/soft/PHP-7.1.8/build/shtool install -c ext/phar/phar.phar /usr/local/PHP/bin
  9. ln -s -f phar.phar /usr/local/PHP/bin/phar
  10. Installing PDO headers: /usr/local/PHP/include/PHP/ext/pdo/

这里提示让我们从源码包复制一份 PHP.ini 到 /usr/local/PHP/lib/

  1. [root@instance-l79ltvo6 ~]# cp PHP.ini-development /usr/local/PHP/lib/PHP.ini

修改 apache ,让 apache 支持 PHP
编辑/usr/local/apache/conf/httpd.conf

  1. LoadModule PHP7_module modules/libPHP7.so //默认是开启PHP7.so
  2. //找到 <IfModule mime_module>,在</IfModule>前面增加
  3. AddType application/x-httpd-PHP .PHP .PHP3 .phtml .inc
  4. AddType application/x-httpd-PHP-source .PHPs
  5. //找到 DirectoryIndex index.html,增加index.PHP
  6. DirectoryIndex index.html index.shtml index.cgi index.PHP index.phtml index.PHP3

保存后看看httpd.conf有没有出错

  1. [root@instance-l79ltvo6 ~]# /usr/local/apache/bin/apachectl -t
  2. Syntax OK //说明没问题,可以直接启动

PHP加入环境变量

  1. [root@instance-l79ltvo6 ~]# vim /etc/profile.d/PHP.sh
  2. //加入
  3. export PATH=$PATH:/usr/local/PHP/bin
  4. //保存后赋予执行权限
  5. [root@instance-l79ltvo6 ~]# chmod 0777 /etc/profile.d/PHP.sh
  6. [root@instance-l79ltvo6 ~]# source /etc/profile.d/PHP.sh

停止 apache,然后重新启动 apache

  1. [root@instance-l79ltvo6 ~]# systemctl stop httpd.service
  2. [root@instance-l79ltvo6 ~]# systemctl start httpd.service

测试PHP

  1. [root@instance-l79ltvo6 ~]# vim /usr/local/apache/htdocs/PHPinfo.PHP
  2. //加入
  3. <?PHP
  4. PHPinfo();
  5. ?>

保存后访问http://你的IP/PHPinfo.PHP
如果成功访问到PHP的相关信息,那么PHP环境配置成功。

教程结束

本次教程本人在 CentOS 7.3 下搭建成功。如果出现编译失败也不要灰心,一般都是缺少相关依赖,善于利用搜索引擎。如果实在不知道怎么解决,给我留言,我会在能力范围尽可能的协助解决。觉得本文章有用,别忘了点赞和收藏喔。

猜你在找的CentOS相关文章