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 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 $@ 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
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
17,使用浏览器打开机,如图
来源: http://www.bubuko.com/infodetail-1858942.html