linux运维之LAMP(apache+mariadb+php)搭建
linux运维之LAMP(apache+mariadb+php)搭建LAMP 是Linux Apache MySQL(mariadb) PHP的简写,其实就是把Apache, MySQL以及PHP安装在Linux系统上,组成一个环境来运行php的脚本语言,其中mariadb为mysql的一个分支。
搭建环境:
系统: CentOS6.6-x86_64
httpd: httpd-2.4.9
mariadb: mariadb-5.5.43
php: php-5.4.26
注: 安装顺序为 httpd-->mairadb-->php,安装编译前先安装开发环境组件:
"Develoment Tools"和 "Server Platform Development"
# yum groupinstall "Development Tools" "Server Platform Development" -y
---------------------------------------------------------------------------------------------------------
1.apache(httpd)
apache:Web服务器,主要提供网上信息浏览服务,使用应用层http协议
编译安装:
依赖包:
httpd-2.4.12.tar.bz2
pcre-deve
apr-1.5.0.tar.bz2:apr(Apache Portable Runtime)
apr-util-1.5.3.tar.bz2
注: CentOS6.6系统默认安装httpd2.2版本,如果以防和2.4冲突,可以自行移除2.2版本后再编译安装2.4,这里不做移除。
移除命令为:
# yum remove httpd
首先安装pcre-deve包:
# yum install pcre-devel -y 编译安装apr包:
# ls #下载源码包到服务器
anaconda-ks.cfg httpd-2.4.10.tar.bz2 Public
apr-1.5.0.tar.bz2 install.log Templates
apr-util-1.5.3.tar.bz2install.log.syslog Videos
Desktop Music wordpress-4.3.1-zh_CN.zip
Documents phpMyAdmin-4.4.14.1-all-languages.zip
Downloads Pictures# tar xf apr-1.5.0.tar.bz2 #解压
# cd apr-1.5.0
# ls #查看源码包内容
apr-config.inbuildconf dso libapr.rc NOTICE support
apr.dep build.conf emacs-modeLICENSE NWGNUmakefiletables
apr.dsp build-outputs.mkencoding locks passwd test
apr.dsw CHANGES file_io Makefile.in poll threadproc
apr.mak CMakeLists.txt helpers Makefile.winrandom time
apr.pc.in config.layout include memory README tools
apr.spec configure libapr.depmisc README.cmake user
atomic configure.in libapr.dspmmap shmem
build docs libapr.maknetwork_io strings
# ./configure --prefix=/usr/local/apr #编译并指定一个安装路径
# make && make install #编译安装 完成后编译安装apr-util
# tar -xf apr-util-1.5.3.tar.bz2 #解压
# cd apr-util-1.5.3
# ls #查看源码包内容
aprutil.dep buildconf dbd libaprutil.dspNWGNUmakefile
aprutil.dsp build.conf dbm libaprutil.makREADME
aprutil.dsw build-outputs.mkdocs libaprutil.rc README.cmake
aprutil.mak CHANGES encoding LICENSE renames_pending
apr-util.pc.inCMakeLists.txt export_vars.sh.inMakefile.in strmatch
apr-util.spec config.layout hooks Makefile.win test
apu-config.in configure include memcache uri
buckets configure.in ldap misc xlate
build crypto libaprutil.dep NOTICE xml
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr #编译指定路径并针对apr的路径
# make && make install #编译安装 完成后编译安装httpd包:
# tar -xf httpd-2.4.10.tar.bz2
# cd httpd-2.4.10
# ls
ABOUT_APACHE BuildBin.dsp emacs-style LICENSE README.cmake
acinclude.m4 buildconf httpd.dsp Makefile.in README.platforms
Apache-apr2.dswCHANGES httpd.spec Makefile.win ROADMAP
Apache.dsw CMakeLists.txtinclude modules server
apache_probes.dconfig.layout INSTALL NOTICE srclib
ap.d configure InstallBin.dspNWGNUmakefilesupport
build configure.in LAYOUT os test
BuildAll.dsp docs libhttpd.dsp README VERSIONING#./configure --prefix=/usr/local/apache --sysconfdir=/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=event #指定安装目录,配置文件目录,模块等等
# make && make install#编译安装
注: 指定配置文件目录为/etc/httpd24,其中24是为了不与自带httpd2.2冲突而设置。
安装完成后看一下安装目录内容:
# tree -d /usr/local/apache/
/usr/local/apache/
├── bin #启动和关闭脚本
├── build
├── cgi-bin#cgi程序文件存放目录
├── error #服务器端错误时返回给客户端的错误页面
│ └── include
├── htdocs #web页面所在的目录
├── icons #httpd图标文件
│ └── small
├── include #头文件
├── logs #日志文件
├── man #man手册
│ ├── man1
│ └── man8
├── manual #配置手册
│ ├── developer
│ ├── faq
│ ├── howto
│ ├── images
│ ├── misc
│ ├── mod
│ ├── platform
│ ├── programs
│ ├── rewrite
│ ├── ssl
│ ├── style
│ │ ├── css
│ │ ├── lang
│ │ ├── latex
│ │ ├── scripts
│ │ └── xsl
│ │ └── util
│ └── vhosts#虚拟主机
└── modules #httpd模块# tree /etc/httpd24/
/etc/httpd24/
├── extra #以下为扩展的配置文件
│?? ├── httpd-autoindex.conf
│?? ├── httpd-dav.conf
│?? ├── httpd-default.conf
│?? ├── httpd-info.conf
│?? ├── httpd-languages.conf
│?? ├── httpd-manual.conf
│?? ├── httpd-mpm.conf
│?? ├── httpd-multilang-errordoc.conf
│?? ├── httpd-ssl.conf
│?? ├── httpd-userdir.conf
│?? ├── httpd-vhosts.conf
│?? └── proxy-html.conf
├── httpd.conf #主配置文件,包括添加模
├── magic
├── mime.types
└── original
├── extra
│?? ├── httpd-autoindex.conf
│?? ├── httpd-dav.conf
│?? ├── httpd-default.conf
│?? ├── httpd-info.conf
│?? ├── httpd-languages.conf
│?? ├── httpd-manual.conf
│?? ├── httpd-mpm.conf
│?? ├── httpd-multilang-errordoc.conf
│?? ├── httpd-ssl.conf
│?? ├── httpd-userdir.conf
│?? ├── httpd-vhosts.conf
│?? └── proxy-html.conf
└── httpd.conf 编写服务脚本,由于编译安装不会自动生成服务脚本,这里我们拷贝httpd目录到httpd24编辑:
# cd /etc/rc.d/init.d/
# cp httpd httpd24
# vim httpd24
添加到开机启动表并开机自启动:
# chkconfig --add httpd24 #
# chkconfig httpd24 on
# chkconfig --list httpd24
httpd24 0:off 1:off 2:on 3:on 4:on 5:on 6:off 接下来添加PATH变量脚本,不然启动时为httpd2.2版本
# vim /etc/profile.d/httpd.sh #添加脚本 #内容为:
export PATH=/usr/local/apache/bin:$PATH#添加apache执行变量
# . /etc/profile.d/httpd.sh #重新载入 重启服务,80端口处于监听状态
# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::22 :::*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::40538 :::*
LISTEN 0 128 *:57219 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 :::80#已经监听 :::# httpd -M #查看是否为event模式,event是否已经启动
大家看到这里默认启动的模块不是很多,可以根据自己需要自行添加模块位于:
# cd /etc/httpd24/
# ls
extrahttpd.confmagicmime.typesoriginal
# vim httpd.conf#里边模块基本保持默认即可 这样httpd2.4就搭建完成
2.编译安装maraidb-5.5.43
(1)首先解压缩到指定目录并创建数据目录,设定权限
(2)设置配置文件
(3)设置服务脚本文件
# tar xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local/
#解压到/usr/local/目录
# mkdir -pv /mydata/data#创建一个数据存放目录
mkdir: created directory `/mydata'
mkdir: created directory `/mydata/data'
# useradd -r mysql #添加一个管理用户mysql
# chown -R mysql.mysql /mydata/data/#指定目录权限
# ln -sv mariadb-5.5.43-linux-x86_64 mysql #创建软连接指向mariadb数据目录
`mysql' -> `mariadb-5.5.43-linux-x86_64'
# cd mysql/ #通过软链接mysql可以看到mariadb的数据文件
# ls
bin COPYING.LESSEREXCEPTIONS-CLIENTINSTALL-BINARYman README share support-files
COPYINGdata include lib mysql-testscriptssql-bench
# chown -R root.mysql ./* #修改目录mysql里所有文件权限
# ll
total 220
drwxr-xr-x.2 root mysql 4096 Sep 22 08:03 bin
-rw-r--r--.1 root mysql17987 Apr 29 14:55 COPYING
-rw-r--r--.1 root mysql26545 Apr 29 14:55 COPYING.LESSER
drwxr-xr-x.3 root mysql 4096 Sep 22 08:03 data
-rw-r--r--.1 root mysql 8245 Apr 29 14:55 EXCEPTIONS-CLIENT
drwxr-xr-x.3 root mysql 4096 Sep 22 08:03 include
-rw-r--r--.1 root mysql 8694 Apr 29 14:55 INSTALL-BINARY
drwxr-xr-x.3 root mysql 4096 Sep 22 08:03 lib
drwxr-xr-x.4 root mysql 4096 Sep 22 08:03 man
drwxr-xr-x. 11 root mysql 4096 Sep 22 08:03 mysql-test
-rw-r--r--.1 root mysql 108813 Apr 29 14:55 README
drwxr-xr-x.2 root mysql 4096 Sep 22 08:03 scripts
drwxr-xr-x. 27 root mysql 4096 Sep 22 08:03 share
drwxr-xr-x.4 root mysql 4096 Sep 22 08:03 sql-bench
drwxr-xr-x.3 root mysql 4096 Sep 22 08:03 support-files 接着初始化数据库:
# scripts/mysql_install_db --datadir=/mydata/data/ --user=mysql
接下来把mysql里的配置文件拷贝到/etc/mysql/my.cnf(创建)里并编辑:
# mkdir /etc/mysql
# cp support-files/my-large.cnf /etc/mysql/my.cnf
# vim /etc/mysql/my.cnf配置文件设置完成
接下来设置服务脚本:
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld #拷贝服务脚本到init.d目录下名为mysqld 为了使用mysql的安装符合系统规范,并将其开发组件导出给系统使用,这里还需要进行如下步骤:
输出mysql的man手册至man命令的查找路径:编辑/etc/man.config,添加下行:
MANPATH /usr/local/mysql/man
输出msyql的头文件至系统头文件路径/usr/include,通过软链接实现:
# ln -sv /usr/local/mysql/include/ /usr/include/mysql
`/usr/include/mysql' -> `/usr/local/mysql/include/' 输出mysql的库文件给系统查找路径:
# echo '/usr/local/mysql/lib/' > /etc/ld.so.conf.d/mysql.conf 配置好后让系统重新载入系统库
# ldconfig
# chkconfig --add mysqld#加入开机启动表中
# service mysqld start #启动服务
Starting MySQL.. # ss -tnl#查看到3306端口以及监听
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::22 :::*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::40538 :::*
LISTEN 0 128 *:57219 *:*
LISTEN 0 50 *:3306#端口以及监听 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 :::80 :::* 接着我们连接数据库:
能连接到数据库说明数据库正常
3.编译安装php-5.4.26
依赖安装包:
bzip2-devel libmcrypt-devel(epel源,本地光盘没有) libxml2-devel
首先下载源码包到本地目录:
# ls#下载好源码包
anaconda-ks.cfg Downloads php-5.4.26.tar.bz2 #php包
apr-1.5.0 httpd-2.4.10 phpMyAdmin-4.4.14.1-all-languages.zip
apr-1.5.0.tar.bz2 httpd-2.4.10.tar.bz2 Pictures
apr-util-1.5.3 install.log Public
apr-util-1.5.3.tar.bz2install.log.syslog Templates
Desktop mariadb-5.5.43-linux-x86_64.tar.gzVideos
Documents Music wordpress-4.3.1-zh_CN.zip 解压并编译:
# tar xf php-5.4.26.tar.bz2
# cd php-5.4.26
#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/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#with-openssl: 自动寻找本机安装的openssl
#mysql_config: 对mysql数据库进行交互的另外一个接口
#enable-mbstring: 多字节字符串支持
#freetype-dir: 支持的多字体类型
#jpeg-dir: 支持jpeg格式图片
#png-dir: png图片
#zlib: 压缩库
#libxml-dir: 处理xml格式文档
#enable-xml: 支持启用xml
#enable-sockets: 基于套接字通信
#apxs2: 用来把php编译成模块
#mcrypt: 支持加解密库
#config-file: php配置文件路径
#config-file-scan: 详细配置目录,到php.d查看
#bz2:支持bz2格式加密
#maintainer-zts: 编译成zts模块,worker和event模块必须加,prefork则不需要 说明:
(1)这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。
(2)如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。
编译完成后,默认httpd是不支持php的,这里需要为php提供配置文件
# cp php.ini-production /etc/php.ini 编辑apache配置文件httpd.conf以让apache支持php:
# cd /etc/httpd24/
# ls
extrahttpd.confhttpd.conf.bakmagicmime.typesoriginal
# vim httpd.conf 找到AddType选项,添加这两项:
#AddType application/x-httpd-php.php #Add前边没有#号。
#AddType application/x-httpd-php-source.phps
接着定位至DrectoryIndex index.html,修改为DirectoryIndexindex.phpindex.html
而后重启httpd,或让其重新载入配置文件即可,使用httpd -M 可以查看php模块已加载
# httpd -M | tail -5
status_module (shared)
autoindex_module (shared)
dir_module (shared)
alias_module (shared)
php5_module (shared) #可以看到php模块已经加载 编译安装后默认首页文件保存于/usr/local/apache/htdocs里:
# cd /usr/local/apache/
# ls
binbuildcgi-binerrorhtdocsiconsincludelogsmanmanualmodules
# cd htdocs/
# ls #默认为html,改为php
index.html
# mv index.html index.php
# vim index.php #编辑一下内容,看测试效果
接下来在windows浏览器里输入本机IP地址就可以看到测试页:
php首页测试没有问题,这样php就安装完成
关闭数据库测试一下:
# service mysqld stop
Shutting down MySQL..
测试没有问题
# service mysqld start #重新开启mysql服务
到此,LAMP正式搭建完成。。。。
页:
[1]