CentOS7.3搭建Apache+PHP7+web SVN+MariaDB Web服務器(2018-12-29)

注意:本教程使用乾淨的 CentOS 7進行安裝,若是您已安裝其餘環境或軟件,涉及到內核升級,請您妥善備份,可能致使您已安裝的軟件不兼容或出現其餘問題。php

免責聲明:本教程僅以我的經驗撰寫,未必適合全部系統環境。如在使用本教程途中,出現沒法挽救的損失(如數據丟失等),本人概不負責。html

再次提醒:使用本教程前請妥善備份現有數據!使用本教程前請妥善備份現有數據!使用本教程前請妥善備份現有數據!python

若是您使用本教程的方法過程當中出現問題,您可留言,我將在能力範圍內儘量協助解決。mysql

本文最終服務器環境配置:c++

Apache 2.7.28
subversion 1.11.0
MariaDB 10.3.11
php 7.3.0

下面開始進入教程吧。sql

一. 升級內核和軟件包

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

直到控制檯輸出Complete!說明升級完成,最好仍是重啓一下吧apache

[root@instance-l79ltvo6 ~]# reboot

二. 安裝 apache 2.4.37

先安裝一些基本依賴vim

[root@instance-l79ltvo6 ~]# yum install -y gcc gcc-c++ openssl-devel zlib-devel
//在root目錄下建立一個soft文件夾,咱們本次教程的軟件將都放在該目錄下
[root@instance-l79ltvo6 ~]# mkdir soft 
[root@instance-l79ltvo6 ~]# cd soft
而後開始安裝apr、apr-util、pcre 3個依賴,最後再安裝 apache

1. 安裝 apr

[root@instance-l79ltvo6 ~]# wget http://mirror.bit.edu.cn/apache/apr/apr-1.6.5.tar.gz
[root@instance-l79ltvo6 ~]# tar zxf apr-1.6.5.tar.gz
[root@instance-l79ltvo6 ~]# cd apr-1.6.5/
[root@instance-l79ltvo6 ~]# ./configure --prefix=/usr/local/apr
[root@instance-l79ltvo6 ~]# make && make install
[root@instance-l79ltvo6 ~]# cd ..    //返回上級目錄
由於咱們使用了 apr 1.6.5 的依賴,因此下面咱們必需要用 apr-util 1.6.1 版本,apr-util 1.6.1
再也不捆綁安裝 expat ,但又須要 expat 的支持,因此咱們得手動先安裝 expat。否則編譯 apache 的時候會報錯。

2 安裝 expat

[root@instance-l79ltvo6 ~]# wget https://sourceforge.net/projects/expat/files/expat/2.2.3/expat-2.2.3.tar.bz2
[root@instance-l79ltvo6 ~]# tar jxf expat-2.2.3.tar.bz2 //使用minimal安裝的centos請先執行yum install -y bzip2
[root@instance-l79ltvo6 ~]# cd expat-2.2.3/
[root@instance-l79ltvo6 ~]# ./configure --prefix=/usr/local/expat
[root@instance-l79ltvo6 ~]# make && make install
[root@instance-l79ltvo6 ~]# cd ..    //返回上級目錄

3 安裝 apr-util

[root@instance-l79ltvo6 ~]# wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[root@instance-l79ltvo6 ~]# tar zxf apr-util-1.6.1.tar.gz
[root@instance-l79ltvo6 ~]# cd apr-util-1.6.1/
[root@instance-l79ltvo6 ~]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr --with-expat=/usr/local/expat
[root@instance-l79ltvo6 ~]# make && make install
[root@instance-l79ltvo6 ~]# cd ..    //返回上級目錄

4 安裝 pcre

[root@instance-l79ltvo6 ~]# wget http://sourceforge.mirrorservice.org/p/pc/pcre/pcre/8.41/pcre-8.41.tar.gz
[root@instance-l79ltvo6 ~]# tar zxf pcre-8.41.tar.gz
[root@instance-l79ltvo6 ~]# cd pcre-8.41/
[root@instance-l79ltvo6 ~]# ./configure --prefix=/usr/local/pcre
[root@instance-l79ltvo6 ~]# make && make install
[root@instance-l79ltvo6 ~]# cd ..    //返回上級目錄

5 安裝 apache

[root@instance-l79ltvo6 ~]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.37.tar.gz
[root@instance-l79ltvo6 ~]# tar zxf httpd-2.4.37.tar.gz
[root@instance-l79ltvo6 ~]# cd httpd-2.4.37/
[root@instance-l79ltvo6 ~]# ./configure \
--prefix=/usr/local/apache \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--with-pcre=/usr/local/pcre \
--with-ssl \
--with-zlib \
--with-mpm=worker \
--enable-rewrite \
--enable-so \
--enable-ssl \
--enable-cache \
--enable-disk-cache \
--enable-file-cache \
--enable-mem-cache \
--enable-headers \
--enable-expires \
--enable-deflate \
--enable-dav \
--enable-dav-fs \
--enable-cgi \
--enable-proxy \
--enable-proxy-fcgi
//這裏請根據自身實際狀況開啓相關模塊
[root@instance-l79ltvo6 ~]# make && make install
[root@instance-l79ltvo6 ~]# cd ..    //返回上級目錄

6 修改 apache 配置文件

編輯/usr/local/apache/conf/httpd.conf
#LoadModule ssl_module modules/mod_ssl.so //去掉#開啓 SSL
#LoadModule rewrite_module modules/mod_rewrite.so  //去掉#開啓 rewrite
#ServerName www.example.com:80    //去掉#並把 www.example.com:80 修改成你的IP:80或者域名
#Include conf/extra/httpd-vhosts.conf     //去掉#,開啓虛擬主機配置
//若是你須要安裝svn服務,你須要開啓
#LoadModule dav_module modules/mod_dav.so//去掉#
找到如下代碼並更換
<Directory />
    AllowOverride none
    Require all denied
</Directory>
//修改成
<Directory />
    Options Indexes FollowSymLinks    //如不須要顯示目錄,把 Indexes 去掉
    AllowOverride ALL    //開啓rewrite
    Require all granted
</Directory>
編輯/usr/local/apache/conf/extra/httpd-vhosts.conf,刪除所有,並添加下列代碼
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs"
    ServerName your IP    //你的IP地址
    ErrorLog "logs/你的IP-error_log"
    CustomLog "logs/你的IP-access_log" common
</VirtualHost>

7 添加啓動服務

[root@instance-l79ltvo6 ~]# cp /usr/local/apache/bin/apachectl  /etc/rc.d/init.d/
[root@instance-l79ltvo6 ~]# mv /etc/rc.d/init.d/apachectl /etc/rc.d/init.d/httpd
[root@instance-l79ltvo6 ~]# cd /etc/rc.d/init.d/
編輯 httpd ,在 #!/bin/sh 下面添加下面代碼
# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 90 90
# description:http server
註冊服務
[root@instance-l79ltvo6 ~]# chkconfig --add httpd
[root@instance-l79ltvo6 ~]# chkconfig httpd on
把 apache 加入系統環境變量
[root@instance-l79ltvo6 ~]# vim /etc/profile.d/httpd.sh
//寫入
export PATH=$PATH:/usr/local/apache/bin
//保存後賦予執行權限
[root@instance-l79ltvo6 ~]# chmod 0777 /etc/profile.d/httpd.sh
[root@instance-l79ltvo6 ~]# source /etc/profile.d/httpd.sh

8 啓動 apache

首先檢查配置文件是否出錯
[root@instance-l79ltvo6 ~]# /usr/local/apache/bin/apachectl -t
Syntax OK    //說明沒問題,能夠直接啓動
啓動 apache
[root@instance-l79ltvo6 ~]# systemctl start httpd.service
而後打開瀏覽器,輸入你的IP地址,看到It works!,說明apache成功啓動了
It works!

三. 安裝 subversion 1.11.0

(如不須要svn服務請跳過)

1 安裝 scons

[root@instance-l79ltvo6 ~]# cd /root/soft
[root@instance-l79ltvo6 ~]# wget http://sourceforge.mirrorservice.org/s/sc/scons/scons/2.5.1/scons-2.5.1.tar.gz
[root@instance-l79ltvo6 ~]# tar zxf scons-2.5.1.tar.gz
[root@instance-l79ltvo6 ~]# cd scons-2.5.1/
[root@instance-l79ltvo6 ~]# python setup.py install --prefix=/usr/local/scons
[root@instance-l79ltvo6 ~]# cd ..    //返回上級目錄

2 安裝 serf

[root@instance-l79ltvo6 ~]# wget http://mirror.bit.edu.cn/apache/serf/serf-1.3.9.tar.bz2
[root@instance-l79ltvo6 ~]# tar xf serf-1.3.9.tar.bz2
[root@instance-l79ltvo6 ~]# cd serf-1.3.9/
[root@instance-l79ltvo6 ~]# /usr/local/scons/bin/scons prefix=/usr/local/serf APR=/usr/local/apr APU=/usr/local/apr-util
[root@instance-l79ltvo6 ~]# /usr/local/scons/bin/scons install
[root@instance-l79ltvo6 ~]# cd ..    //返回上級目錄

3 編譯 subverion

[root@instance-l79ltvo6 ~]# wget http://www.sqlite.org/2017/sqlite-amalgamation-3190300.zip
[root@instance-l79ltvo6 ~]# wget https://mirror.bit.edu.cn/apache/subversion/subversion-1.11.0.tar.gz
[root@instance-l79ltvo6 ~]# tar zxf subversion-1.11.0.tar.gz
[root@instance-l79ltvo6 ~]# unzip sqlite-amalgamation-3190300.zip
[root@instance-l79ltvo6 ~]# mv /root/soft/sqlite-amalgamation-3190300 /root/soft/subversion-1.11.0/sqlite-amalgamation
[root@instance-l79ltvo6 ~]# cd subversion-1.11.0/
[root@instance-l79ltvo6 ~]# ./configure \
--prefix=/usr/local/svn \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--with-serf=/usr/local \
--enable-mod-activation \
--with-apache-libexecdir=/usr/local/apache/modules \
--with-apxs=/usr/local/apache/bin/apxs  \
--without-berkeley-db  \
--with-lz4=internal  \
--with-utf8proc=internal
[root@instance-l79ltvo6 ~]# make && make install
給SVN建立一個名爲svn的非登陸用戶
[root@instance-l79ltvo6 ~]# useradd svn -s /sbin/nologin
把svn加入到系統環境變量
[root@instance-l79ltvo6 ~]# vim /etc/profile.d/svn.sh
//添加
export PATH=$PATH:/usr/local/svn/bin
保存後賦予執行權限
[root@instance-l79ltvo6 ~]# chmod 0777 /etc/profile.d/svn.sh
[root@instance-l79ltvo6 ~]# source /etc/profile.d/svn.sh
在/etc/ld.so.conf.d/建立一個serf-1.3.9.conf,指定lib目錄,否則svn啓動會報錯
[root@instance-l79ltvo6 ~]# vim /etc/ld.so.conf.d/serf-1.3.9.conf
//添加
/usr/local/lib
保存後刷新
[root@instance-l79ltvo6 ~]# /sbin/ldconfig -v

4 配置 subverion

下面咱們先建立一個 test 項目
[root@instance-l79ltvo6 ~]# mkdir -p /data/svn
[root@instance-l79ltvo6 ~]# cd /data/svn
[root@instance-l79ltvo6 ~]# svnadmin create test
而後咱們打開 /data/svn/test /就會發現裏面自動建立了一些目錄
conf
db
format
hooks
locks
README.txt
在conf裏面的文件就是配置該項目的人員和權限,但若是多個項目,開發人員同樣,那就要配置不少次了,這樣很麻煩,因此咱們要弄成配置一次權限就能直接應用到全部項目裏面。
[root@instance-l79ltvo6 ~]# cp /data/svn/test/conf/authz  /data/svn/authz
[root@instance-l79ltvo6 ~]# cp /data/svn/test/conf/passwd  /data/svn/passwd
[root@instance-l79ltvo6 ~]# cp /data/svn/test/conf/svnserve.conf  /data/svn/svnserve.conf
//而後設置一個用戶密碼,這裏以建立root用戶爲例,這裏的演示是將密碼加密而非明文存儲
[root@instance-l79ltvo6 ~]# htpasswd -c /data/svn/passwd root
New password:       //輸入密碼
Re-type new password:    //再次輸入密碼
這樣 root 用戶就建立完成了,建立其餘用戶同理。 接下來咱們須要修改配置文件
[root@instance-l79ltvo6 ~]# vim /data/svn/svnserve.conf
//刪除全部內容,增長下面的代碼
[general]
anon-access = read
auth-access = write
password-db = passwd
authz-db = authz
[sasl]
//保存
[root@instance-l79ltvo6 ~]# vim /data/svn/authz
//刪除全部內容,增長下面的代碼
[aliases]
# joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average

[groups]
administrators = root

[/]
@administrators=rw

[test:/]
* = r
//保存
咱們嘗試啓動下svn服務
[root@instance-l79ltvo6 ~]# svnserve --config-file /data/svn/svnserve.conf -d -r /data/svn
沒有報錯,說明啓動成功了
把/data/svn/目錄的擁有者更改成svn
[root@instance-l79ltvo6 ~]# chown -R svn:svn /data/svn
最後咱們須要修改 apache 的相關配置文件
編輯/usr/local/apche/conf/httpd.conf
//找到
User daemon
Group daemon
//修改成
User svn
Group svn
編輯/usr/local/apache/conf/extra/httpd-vhost.conf,把剛剛那次編輯的內容更換爲
<VirtualHost *:80>
    ServerName 你的IP
    ErrorLog "logs/你的IP-error_log"
    CustomLog "logs/你的IP-access_log" common
    <Location /svn>
      DAV svn
      #support more repositories
      SVNParentPath /data/svn

      #list all repositories
      #SVNListParentPath on
      AuthType Basic
      AuthName "Please input Username and Password"
      AuthUserFile /data/svn/passwd
      AuthzSVNAccessFile /data/svn/authz
      Require valid-user
    </Location>
</VirtualHost>
而後中止 apache 服務
[root@instance-l79ltvo6 ~]# systemctl stop httpd.service
檢查 apache 配置文件是否出錯
[root@instance-l79ltvo6 ~]# /usr/local/apache/bin/apachectl -t
Syntax OK    //說明沒問題,能夠直接啓動
從新啓動 apache
[root@instance-l79ltvo6 ~]# systemctl start httpd.service
打開瀏覽器訪問 http://你的ip/svn/test,提示輸入帳號密碼 也可使用TortoiseSVN進行操做
輸入剛剛設置的帳號密碼後顯示
test - Revision 0: /
說明配置成功,apache+svn服務啓動成功

四. 安裝 MariaDB 10.3.11

參考 https://segmentfault.com/a/11...,把相關版本號更換爲10.3.11便可(第6步無需操做:6. 複製MariaDB配置文件到/etc目錄)

五. 安裝 PHP 7.3.0

先安裝基本依賴
[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
安裝libzip
[root@instance-l79ltvo6 ~]# cd /root/soft
[root@instance-l79ltvo6 ~]# wget https://nih.at/libzip/libzip-1.2.0.tar.gz
[root@instance-l79ltvo6 ~]# tar -zxvf libzip-1.2.0.tar.gz
[root@instance-l79ltvo6 ~]# cd libzip-1.2.0
[root@instance-l79ltvo6 ~]# ./configure
[root@instance-l79ltvo6 ~]# make && make install
即便安裝了 libzip ,編譯PHP的時候也可能會出現找不到 zipconf.h,因此手動複製一下
[root@instance-l79ltvo6 ~]# cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
添加搜索路徑到配置文件
[root@instance-l79ltvo6 ~]# echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf
//更新配置
[root@instance-l79ltvo6 ~]# ldconfig -v
編譯 PHP
[root@instance-l79ltvo6 ~]# cd /root/soft
[root@instance-l79ltvo6 ~]# wget http://cn2.php.net/distributions/php-7.3.0.tar.gz
[root@instance-l79ltvo6 ~]# tar zxf php-7.3.0.tar.gz
[root@instance-l79ltvo6 ~]# cd php-7.3.0/
[root@instance-l79ltvo6 ~]# ./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-mhash \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip \
--enable-mysqlnd
[root@instance-l79ltvo6 ~]# make && make install
...    //漫長的等待
編譯成功
[PEAR] Archive_Tar    - installed: 1.4.3
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.2
[PEAR] PEAR           - installed: 1.10.5
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/root/soft/php-7.1.8/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers:           /usr/local/php/include/php/ext/pdo/
這裏提示讓咱們從源碼包複製一份 php.ini 到 /usr/local/php/lib/
[root@instance-l79ltvo6 ~]# cp php.ini-development /usr/local/php/lib/php.ini
修改 apache ,讓 apache 支持 php
編輯/usr/local/apache/conf/httpd.conf
LoadModule php7_module        modules/libphp7.so    //默認是開啓php7.so
//找到 <IfModule mime_module>,在</IfModule>前面增長
    AddType application/x-httpd-php .php .php3 .phtml .inc
    AddType application/x-httpd-php-source .phps
//找到 DirectoryIndex index.html,增長index.php
DirectoryIndex index.html index.shtml index.cgi index.php index.phtml index.php3
保存後看看httpd.conf有沒有出錯
[root@instance-l79ltvo6 ~]# /usr/local/apache/bin/apachectl -t
Syntax OK    //說明沒問題,能夠直接啓動
把PHP加入環境變量
[root@instance-l79ltvo6 ~]# vim /etc/profile.d/php.sh
//加入
export PATH=$PATH:/usr/local/php/bin
//保存後賦予執行權限
[root@instance-l79ltvo6 ~]# chmod 0777 /etc/profile.d/php.sh
[root@instance-l79ltvo6 ~]# source /etc/profile.d/php.sh
中止 apache,而後從新啓動 apache
[root@instance-l79ltvo6 ~]# systemctl stop httpd.service
[root@instance-l79ltvo6 ~]# systemctl start httpd.service
測試PHP
[root@instance-l79ltvo6 ~]# vim /usr/local/apache/htdocs/phpinfo.php
//加入
<?php
  phpinfo();
?>

保存後訪問http://你的IP/phpinfo.php
若是成功訪問到PHP的相關信息,那麼PHP環境配置成功。segmentfault

教程結束

本次教程本人在 CentOS 7.3 下搭建成功。
若是出現編譯失敗也不要灰心,通常都是缺乏相關依賴,善於利用搜索引擎。若是實在不知道怎麼解決,給我留言,我會在能力範圍儘量的協助解決。
以爲本文章有用,別忘了點贊和收藏喔。centos

相關文章
相關標籤/搜索