Apache + php +mysql 环境搭建lamp
此次试验搭建lamp环境,Apache 服务前端服务器,PHP、mysql为后端服务器,NFS为请求页面资源共享存储,试验设计如图:(系统Centos6.7,源码软件版本Apache 2.4.3、php5.6.7、Mariadb-10.1.19 )
一、配置NFS服务器
1,安装nfs-utils,启动rpcbind服务,启动nfs服务。
2,创建NFS目录 mkdir /htdoc
3,编辑 vim /etc/exports,添加 /htdoc 192.168.76.0/24(rw,no_root_squash),重新导入。
命令 :exportfs -a
rpcinfo(可以查看信息)
4,可以使用showmount -e 192.168.76.170查看nfs共享目录。
二、配置mysql服务器
1,需要安装gcc-c++、libaio、libaio-devel等
2,解压mariadb-10.1.19-linux-x86_64.tar.gz到/usr/local目录下,
命令:tar xf mariadb-10.1.19-linux-x86_64.tar.gz -C /usr/local/
3,创建软链接
命令:ln -sv /usr/local/mariadb-10.1.19-linux-x86_64/ /usr/local/mysql
4,创建mysql组、用户
命令: groupadd -r mysql 、useradd -r -g mysql -s /sbin/nologin mysql
5,创建lv磁盘,命令如下:
fdisk /dev/sdb (进行分区,指定磁盘类型为8e)
pvcreate /dev/sdb1(创建物理磁盘1)
pvcreate /dev/sdb2(创建物理磁盘2)
vgcreate myvg /dev/sdb1 /dev/sdb2 (将sdb1、sdb2加入到myvg组中)
lvcreate -L 5G -n mylvdata /dev/myvg(创建5G的逻辑卷)
mke2fs -t ext4 /dev/myvg/mylvdata (格式化磁盘)
mkdir /data(创建挂载目录)
vim /etc/fstab 增加如下内容 /dev/myvg/mylvdata /data ext4 defaults,noatime 0 0
mount -a(挂载)
mkdir /data/mydata(创建Mysql数据data目录)
6,更改mydata目录权限 chown -R mysql:mysql /data/mydata/
7,更改/usr/local/mysql/* 的所有文件属组 chown -R root.mysql /usr/local/mysql/*
8, 安装mysql,进入cd /usr/local/mysql/scripts/
命令: ./mysql_install_db --ldata=/data/mydata/ --user=mysql --basedir=/usr/local/mysql/
9,拷贝/usr/local/mysql/support-files/my-large.cnf 到/etc/目录
命令 cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
10,拷贝服务启动脚本到/etc/rc.d/init.d/目录,给予执行权限,并添加开机启动
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld (给予执行权限)
chkconfig --add mysqld(添加服务)
chkconfig mysqld on(开机启动)
11,创建include、lib的软件链接
ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
ln -sv /usr/local/mysql/lib/ /usr/lib64/mysql
12,编辑/etc/my.cnf 在[mysqld]段内增加 datadir=/data/mydata,并启动mysqld服务。
13,登录mysql,初始没有密码直接使用mysql即可登录。
14,mysql配置,请注意命令以分号结束。
a,使用mysql数据库命令: use mysql;
b,查看所有用户命令:select user,password,host from user;
c,为了安全删除一些用户,命令如下
delete from user where user=''; (删除空用户)
delete from user where host='::1'; (删除IPv6的用户)
update user set password=password('password') where user='root';(设置统一密码)
grant all privileges on *.* to 'phpserver'@'192.168.76.166' identified by 'password'; (设置php授权增加phpserver用户)
flush privileges;(刷新权限)
15,数据库配置完成。
三、配置php服务器
1,安装前需要安装gcc-c++ 、Development tools、pcre-devel、libxml2-devel、openssl-devel、bzip2-devel、libmcrypt-devel、 libmcrypt等。
2,编译安装php5.6.27,一定要启用--enable-maintainer-zts(如果httpd启用event特性) --enable-fpm(启用fcgi需要启用fpm)
1 | <span style= "font-size:14px;" >./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl -- enable-mbstring --with-freetype-dir --with-jpeg-dir --with-pn<br>g-dir --with-zlib --with-libxml-dir=/usr -- enable-xml -- enable-sockets --with-mcrypt --with-config -file -path=/etc --with-config -file -scan-dir=/etc/php.d --with-bz2 -- enable-maintainer -zts -- enable-fpm <br>make && make install<br></span> |
3,安装完成后,复制如下文件:
a,复制 php配置文件php.ini-production(此文件在源码目录中)到/etc 目录,
命令:cp php.ini-production /etc/php.ini
b,复制init.d.php-fpm.in启动脚本到/etc/rc.d/init.d/,在源码目录/root/php-5.6.27/sapi/fpm
命令: cp init.d.php-fpm /etc/rc.d/init.d/php-fpm,
c,复制php-fpm.service配置文件到/etc/sysconfig/,在源码目录/root/php-5.6.27/sapi/fpm
命令:cp php-fpm.service /etc/sysconfig/php-fpm.service
4,修改php-fpm监听地址为 listen = [::]:9000
5,安装nfs-utils,启动rpcbind服务。
6,创建nfs挂载目录mkdir -pv /www/htdoc
7,挂载nfs共享目录mount -t nfs 192.168.76.170:htdoc /www/htdoc
四、配置apache服务器,编译安装apache2.4.3,需要pcre-devel,apr,apr-util,gcc,gcc-c++等包。
1,安装apr:
1 | <span style= "font-size:14px;" >./configure --prefix=/usr/local/apr --sysconfdir=/etc/apr/ <br> make && make install<br></span> |
2,安装apr-util
1 | <span style= "font-size:14px;" >./configure --prefix=/usr/local/apr-util --sysconfdir=/etc/apr-util --with-apr=/usr/local/apr/<br> make && make install<br></span> |
3,安装apache,以apache用户身份运行服务,
1 | <span style= "font-size:14px;" >groupadd -r apache <br>useradd -r -g apache -s /sbin/nologin apache<br>./configure --prefix=/usr/local/httpd --sysconfdir=/etc/httpd/ --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ -- enable-mods -shared=all --with-mpm=event -- enable-ssl -- enable-cgi -- enable-moudles =all -- enable-rewrite -- enable-mpms -shared=all --with-user=apache -with-group=apache <br>make && make install</span> |
4,创建include链接
ln -sv /usr/local/httpd/include/ /usr/local/include/httpd
5,添加man路径
vim /etc/man.config 添加 MANPAATH=/usr/local/httpd/man
6,添加init服务,编辑/etc/rc.d/init.d/httpd
增加如下代码
#!/bin/bash
1 | <span style= "font-size:14px;" > #<br>#description: httpd service<br>#chkconfig: - 85 15<br>. /etc/rc.d/init.d/functions<br>if [ -f /etc/sysconfig/httpd ];then<br> . /etc/sysconfig/httpd<br>fi<br>HTTPD_LANG=${HTTPD_LANG-"C"}<br>INITLOG_ARGS=""<br>proc=httpd<br>apachectl=/usr/local/httpd/bin/apachectl<br>httpd=${httpd-/usr/local/httpd/bin/httpd}<br>pidfile=${pidfile-/var/run/httpd/httpd.pid}<br>lockfile=${lockfile-/var/lock/subsys/httpd}<br>STOP_TIMEOUT=${STOP_TIMEOUT-10}<br>RETVAL=0<br>start() {<br>echo -n $"Starting $prog: "<br>LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS<br> RETVAL=$?<br> echo<br> [ $RETVAL = 0 ] && touch ${lockfile}<br> return $RETVAL<br>}<br>stop() {<br> echo -n $"Stopping $prog: "<br> killproc -p ${pidfile} -d ${STOP_TIMEOUT} ${httpd} <br> RETVAL=$?<br> echo<br> [ $RETVAL = 0 ] && rm -rf ${lockfile} ${pidfile}<br>}<br>reload() {<br> echo -n $"Reloading $prog: "<br> if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then<br> RETVAL=6<br> echo $"not reloading due to configuration syntax error"<br> failure $"not reloading $httpd due to configuration syntax error"<br> else<br> # Force LSB behaviour from killproc<br> LSB=1 killproc -p ${pidfile} $httpd -HUP<br> RETVAL=$?<br> if [ $RETVAL -eq 7 ]; then<br> failure $"httpd shutdown"<br> fi<br> fi<br> echo<br>}<br>case "$1" in<br> start)<br> start<br> ;;<br> stop)<br> stop<br> ;; <br> status)<br> status -p ${pidfile} $httpd<br> RETVAL=$?<br> ;;<br> restart)<br> stop<br> start<br> ;;<br> condrestart|try-restart)<br> if status -p ${pidfile} $httpd >&/dev/null; then<br> stop<br> start<br> fi<br> ;;<br> force-reload|reload)<br> reload<br> ;;<br> graceful|help|configtest|fullstatus)<br> $apachectl $@<br> RETVAL=$?<br> ;;<br> *)<br> echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|<br>fullstatus|graceful|help|configtest}" <br> RETVAL=2<br> ;;<br>esac<br>exit $RETVAL</span> |
7,编辑/etc/sysconfig/httpd文件
添加如下内容
httpd=/usr/local/httpd/bin/httpd
pidfile=/usr/local/httpd/logs/httpd.pid
lockfile=/var/lock/subsys/httpd
STOP_TIMEOUT=10
8,添加开机启动chkconfig --add httpd ,chkconfig httpd on ,启动httpd服务,service httpd start,在浏览输入主机ip,显示it's works!即可。
9,安装nfs-utils,启动rpcbind服务。
10,创建nfs挂载目录mkdir -pv /www/htdoc
11,编辑/etc/httpd/httpd.conf文件,
a,启用如下配置
LoadModule proxy_module modules/mod_proxy.so(启用代理模块)
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so(启用fcgi模块)
Include /etc/httpd//extra/httpd-mpm.conf(启用mpm配置文件)
Include /etc/httpd//extra/httpd-vhosts.conf(启用vhost配置文件)
b,增加如下配置
ServerName localhost(如果不写此配置启动会报错,但能否启动服务)
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
c,修改如下配置
使用apache用户和组: User apache Group apache
注释 #DocumentRoot "/usr/local/httpd/htdocs"
<IfModule dir_module>
DirectoryIndex index.php(增加php)index.html
</IfModule>
12,挂载nfs共享目录mount -t nfs 192.168.76.170:htdoc /www/htdoc
13,编辑/etc/httpd/extra/httpd-vhosts.conf文件
<VirtualHost *:80>
ServerAdmin admin@myweb.com
DocumentRoot "/www/htdoc/"
ServerName myweb.com
ServerAlias www.myweb.com
ErrorLog "logs/myweb-error_log"
CustomLog "logs/myweb-access_log" common
ProxyRequests off (关闭正向代理)
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.76.166:9000/www/htdoc/$1 (配置fcgi)
<Directory /www/htdoc/>
AllowOverride none
Require all granted(赋予权限访问)
</Directory>
</VirtualHost>
14,挂载nfs共享目录mount -t nfs 192.168.76.170:htdoc /www/htdoc
15,测试php页面,增加/www/htdoc/index.php
文件内容如下:
<?php
$link =mysql_connect('192.168.76.178','phpserver','password');
if ($link)
echo "Mysql connect seccuss";
else
echo "Mysql connect failure";
phpinfo();
?>
16,编辑windows主机host文件C:\Windows\System32\drivers\etc\hosts,增加如下内容
192.168.76.156 myweb.com
192.168.76.156 www.myweb.com
17,使用浏览器打开机,如图