apache + php +mysql 环境搭建lamp
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了apache + php +mysql 环境搭建lamp相关的知识,希望对你有一定的参考价值。
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)
./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
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
make && make install
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:
./configure --prefix=/usr/local/apr --sysconfdir=/etc/apr/
make && make install
2,安装apr-util
./configure --prefix=/usr/local/apr-util --sysconfdir=/etc/apr-util --with-apr=/usr/local/apr/
make && make install
3,安装apache,以apache用户身份运行服务,
groupadd -r apache
useradd -r -g apache -s /sbin/nologin apache
./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
make && make install
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
#
#description: httpd service
#chkconfig: - 85 15
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ];then
. /etc/sysconfig/httpd
fi
HTTPD_LANG=${HTTPD_LANG-"C"}
INITLOG_ARGS=""
proc=httpd
apachectl=/usr/local/httpd/bin/apachectl
httpd=${httpd-/usr/local/httpd/bin/httpd}
pidfile=${pidfile-/var/run/httpd/httpd.pid}
lockfile=${lockfile-/var/lock/subsys/httpd}
STOP_TIMEOUT=${STOP_TIMEOUT-10}
RETVAL=0
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d ${STOP_TIMEOUT} ${httpd}
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -rf ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=6
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
# Force LSB behaviour from killproc
LSB=1 killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
if [ $RETVAL -eq 7 ]; then
failure $"httpd shutdown"
fi
fi
echo
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart|try-restart)
if status -p ${pidfile} $httpd >&/dev/null; then
stop
start
fi
;;
force-reload|reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl [email protected]
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|
fullstatus|graceful|help|configtest}"
RETVAL=2
;;
esac
exit $RETVAL
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 [email protected]
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,使用浏览器打开机,如图
以上是关于apache + php +mysql 环境搭建lamp的主要内容,如果未能解决你的问题,请参考以下文章
PHP开发环境(Apache+mysql+PHPstorm+php)的搭建 1