CentOS7下LAMP搭建
本文所使用的服务器搭建LAMP环境,其系统及软件源码包版本情况如下:
- CentOS7
- httpd-2.4.29
- apr-1.6.3
- apr-util-1.6.1
- mariadb-5.5.59
- php-5.6.34
LAMP简介
LAMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写:
- Linux,操作系统
- Apache,网页服务器
- MariaDB或MySQL,数据库管理系统(或者数据库服务器)
- PHP、Perl或Python,脚本语言
虽然这些开放源代码程序本身并不是专门设计成同另几个程序一起工作的,但由于它们的廉价和普遍,这个组合开始流行(大多数Linux发行版本捆绑了这些软件)。当一起使用的时候,它们表现的像一个具有活力的“解决方案包”(Solution Packages)。其他的方案包有苹果的WebObjects(最初是应用服务器),Java/J2EE和微软的.NET架构。
“LAMP包”的脚本组件中包括了CGI web接口,它在90年代初期变得流行。这个技术允许网页浏览器的用户在服务器上执行一个程序,并且和接受静态的内容一样接受动态的内容。程序员使用脚本语言来创建这些程序因为它们能很容易有效的操作文本流,甚至当这些文本流并非源自程序自身时也是。正是由于这个原因系统设计者经常称这些脚本语言为胶水语言。
Michael Kunze在一篇为德国电脑杂志《c’t》(1998,第12期,230页)而写的文章中使用了缩略语“LAMP”。这篇文章意在展示一系列的自由软件成为了商业包的替换物。由于IT世界众所周知的对缩写的爱好,Kunze提出“LAMP”这一容易被市场接受的术语来普及自由软件的使用。 —— [ 维基百科 ]
安装Apache
从官网下载以下软件源码包
httpd-2.4版本,依赖于 apr-1.4+,apr-util-1.4+
[root@host /]# wget http://mirrors.hust.edu.cn/apache//httpd/httpd-2.4.29.tar.gz
[root@host /]# wget http://mirrors.hust.edu.cn/apache//apr/apr-1.6.3.tar.gz
[root@host /]# wget http://mirrors.hust.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
安装GCC,安装pcre-devel
[root@host /]# yum install gcc
[root@host /]# yum install pcre pcre-devel
apr源码包编译
[root@host /]# cd apr-1.6.3
[root@host /]# ./configure --prefix=/usr/local/apr
[root@host /]# make && make install
apr-util源码包编译
[root@host /]# cd apr-util-1.6.1
[root@host /]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@host /]# make && make install
注意: 编译遇到以下错误
make[1]: *** [xml/apr_xml.lo] Error 1 make[1]: Leaving directory `/root/download/apr-util-1.6.1' make: *** [all-recursive] Error 1
运行
yum install expat-devel
创建用户和用户组
# -r 创建系统组 apache
[root@host /]# groupadd -r apache
# -r 创建系统用户 apache ,-g 把用户组归属于 apache
[root@host /]# useradd -r -g apache apache
httpd源码包编译
[root@host /]# cd httpd-2.4.29
[root@host /]# ./configure --prefix=/usr/local/apache \
--sysconf=/etc/httpd24 \
--enable-so --enable-ssl \
--enable-cgi --enable-rewrite \
--with-zlib --with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
[root@host /]# make && make install
补充:
(1)构建MPM为静态模块
在全部平台中,MPM都可以构建为静态模块。在构建时选择一种MPM,链接到服务器中。如果要改变MPM,必须重新构建。为了使用指定的MPM,请在执行configure脚本 时,使用参数 --with-mpm=NAME。NAME是指定的MPM名称。编译完成后,可以使用 ./httpd -l 来确定选择的MPM。 此命令会列出编译到服务器程序中的所有模块,包括 MPM。
(2)构建 MPM 为动态模块
在Unix或类似平台中,MPM可以构建为动态模块,与其它动态模块一样在运行时加载。 构建 MPM 为动态模块允许通过修改LoadModule指令内容来改变MPM,而不用重新构建服务器程序。在执行configure脚本时,使用–enable-mpms-shared选项即可启用此特性。当给出的参数为all时,所有此平台支持的MPM模块都会被安装。还可以在参数中给出模块列表。默认MPM,可以自动选择或者在执行configure脚本时通过–with-mpm选项来指定,然后出现在生成的服务器配置文件中。编辑LoadModule指令内容可以选择不同的MPM。
注意: 编译遇到以下错误
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_GetErrorCode' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetEntityDeclHandler' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_ParserCreate' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetCharacterDataHandler' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_ParserFree' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetUserData' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_StopParser' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_Parse' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_ErrorString' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetElementHandler' collect2: error: ld returned 1 exit status make[2]: *** [htpasswd] Error 1 make[2]: Leaving directory `/root/download/httpd-2.4.29/support' make[1]: *** [all-recursive] Error 1 make[1]: Leaving directory `/root/download/httpd-2.4.29/support' make: *** [all-recursive] Error 1
该错误是因为缺少了xml相关的库,导致reference无效
运行yum install -y libxml2-devel
然后重新编译安装 apr-util 源码包
启动服务测试,如有提示 “… domain name… Set the ‘ServerName’ … ”,暂时不用理会,如何去掉该提示,请参考Apache2.4配置
[root@host /]# /usr/local/apache/bin/apachectl
通过浏览器访问服务器IP地址,页面显示“It works!”,到此处 Apache搭建完成
添加 Systemd 服务脚本
[root@host /]# cd /usr/lib/systemd/system
[root@host /]# vim 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=simple
ExecStart=/usr/local/apache/bin/httpd -DFOREGROUND
ExecReload=/usr/local/apache/bin/httpd -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
启动服务测试
[root@host /]# systemctl start httpd
安装MariaDB
从官网下载 MariaDB 源码包
[root@host /]# wget https://downloads.mariadb.org/interstitial/mariadb-5.5.59/source/mariadb-5.5.59.tar.gz
# 安装 cmake 工具
[root@host /]# yum install cmake
# 安装 gcc-c++ 工具
[root@host /]# yum install gcc-c++
# 安装 ncurses-devel
[root@host /]# yum install ncurses-devel
准备数据目录
# 以 /data/mysql 为例, 为数据库存放位置
[root@host /]# mkdir -p /data/mysql
配置mariadb
[root@host /]# groupadd -r -g 306 mysql
[root@host /]# useradd -r -g 306 -u 306 mysql
[root@host /]# tar -zxvf mariadb-5.5.59.tar.gz
[root@host /]# cd mariadb-5.5.59
# 可使用 cmake . -LH 查看更多配置选项
[root@host /]# cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr/local/mariadb-5.5.59 -DMYSQL_DATADIR:PATH=/data/mysql
[root@host /]# make && make install
[root@host /]# ln -sv /usr/local/mariadb-5.5.59 /usr/local/mariadb
[root@host /]# cd /usr/local/mariadb
[root@host /]# chown -R root:mysql ./*
[root@host /]# scripts/mysql_install_db --datadir=/data/mysql --user=mysql
准备配置文件
[root@host /]# mkdir /etc/mysql
[root@host /]# cp /usr/local/mariadb/support-files/my-large.cnf /etc/mysql/my.cnf
注意: 配置文件的查找顺序为
/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf
如 存在 /etc/my.cnf,请删除。避免放于 /etc/mysql/my.cnf 文件失效
修改配置文件
在配置文件 [mysqld] 段 添加以下三个选项:
datadir = /data/mysql
innodb_file_per_table = on
skip_name_resolve = on
首次启动MariaDB
[root@host /]# /usr/local/mariadb/bin/mysqld_safe --user=mysql &
[root@host /]# /usr/local/mariadb/bin/mysql_secure_installation
安装PHP
从官网下载 PHP 源码包
[root@host /]# wget http://php.net/distributions/php-5.6.34.tar.gz
# 安装 bzip2
[root@host /]# yum install bzip2 bzip2-devel
# 安装 libmcrypt
[root@host /]# yum install libmcrypt-devel
php 源码包编译
[root@host /]# tar -zxvf php-5.6.34.tar.gz
[root@host /]# cd php-5.6.34
[root@host /]# ./configure \
--prefix=/usr/local/php \
--with-mysql=/usr/local/mariadb \
--with-openssl --with-mysqli=/usr/local/mariadb/bin/mysql_config \
--enable-mbstring \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--enable-sockets \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-mcrypt \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--with-bz2 \
--enable-maintainer-zts
说明:
这里为了支持apache的worker或event这两个MPM,编译时使用了–enable-maintainer-zts选项。
[root@host /]# make
[root@host /]# make test
[root@host /]# make install
准备配置文件
[root@host /]# cp php.ini-production /etc/php.ini
编辑 Apache 配置文件httpd.conf [IfModule mime_module]段 添加以下两项,使 Apache 支持 php
vim /etc/httpd24/httpd.conf
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
定位至 [ DirectoryIndex index.html ] 修改为
DirectoryIndex index.php index.html
测试 PHP
[root@host /]# cd /usr/local/apache/htdocs/
[root@host /]# echo "<?php phpinfo(); ?>" > index.php
通过浏览器访问http://服务器IP地址/index.php,PHP 能够正常解析
到此 LAMP环境搭建完成