小型企业的web架构搭建
架构简介:以www.su.com为例(不要访问,这域名不是我的,我姓苏而已)
前端dns负责以轮询方式返回客户端解析结果,实现基础负载均衡效果;中间的两台web服务器负责外部的web页面请求响应,后端文件服务器smaba服务负责存放web页面的源代码文件,nfs负责数据库存储
详细的LAMP搭建过程请参见:
http://imsupeng.blog.51cto.com/10651675/1696939
http://imsupeng.blog.51cto.com/10651675/1696313
1、搭建DNS服务器:
服务器相关信息:
[root@DNS ~]# ip add | awk -F'[ /]+''/inet\>/&&!/127.0.0.1/ {print $3}' 172.16.113.53 [root@DNS ~]# uname -r 3.10.0-229.el7.x86_64[root@DNS ~]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core)
关闭firewall和selinux
[root@DNS ~]# systemctl stop firewalld [root@DNS ~]# systemctl disable firewalld [root@DNS ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config [root@DNS ~]# setenforce 0
安装bind程序:
[root@DNS ~]# yum install -y bind
配置bind,添加区域su.com,并在区域数据目录添加su.com的区域数据文件
[root@DNS ~]# cp /etc/named.conf{,.bak} //备份配置文件 [root@DNS ~]# vim /etc/named.conf //编辑配置文件,其他保持默认即可 ectory "/var/named"; allow-query { any; }; zone "su.com" IN { type master; file "su/su.com.zone"; }; [root@DNS ~]# mkdir /var/named/su //创建区域数据文件存放目录 [root@DNS ~]# vim /var/named/su/su.com.zone //创建并编辑区域数据文件 $TTL 1D @ IN SOA su.com. admin.su.com. ( 2015092401 1D 1H 1W 5H) IN NS dns.su.com. dns IN A 172.16.113.53 www IN A 172.16.113.55 www IN A 172.16.113.56 [root@DNS ~]# named-checkzone 'su.com'/var/named/su/su.com.zone //检查区域数据文件是够有误 zone su.com/IN: loaded serial 2015092401 OK [root@DNS ~]# named-checkconf //检查配置文件是否有误 [root@DNS ~]# systemctl startnamed.service //启动bind程序 [root@DNS ~]# systemctl enable named.service //添加至开机自动启动
可以发现,两次的查询请求所返回的解析结果不同,至此,以轮询方式实现负载均衡机制的DNS服务已搭建成功
2、安装samba及nfs:
服务器相关信息:
[root@nfs&samba ~]# uname -r 3.10.0-229.el7.x86_64 [root@nfs&samba ~]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core) [root@nfs&samba ~]# ip add | awk -F '[ /]+' '/inet\>/&&!/127.0.0.1/ {print $3}' 172.16.113.54
关闭selinux及防火墙:
[root@nfs&samba ~]# systemctl stop firewalld [root@nfs&samba ~]# systemctl disable firewalld [root@nfs&samba ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config [root@nfs&samba ~]# setenforce 0
安装配置samba
[root@nfs&samba ~]# yum install -y samba [root@nfs&samba ~]# mkdir /web
a、添加一块硬盘,专门做为web源代码存储
b、为新硬盘创建文件系统并挂在至/web目录,设置开机自动挂在
[root@nfs&samba ~]# fdisk /dev/sdb //为新硬盘创建分区 Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0xff0da1a7. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-104857599, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): Using default value 104857599 Partition 1 of type Linux and of size 50 GiB is set Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@nfs&samba ~]# mkfs -t ext4 /dev/sdb1 //创建文件系统 [root@nfs&samba ~]# mount /dev/sdb1 /web/ //挂在新硬盘 [root@nfs&samba ~]# echo "/dev/sdb1 /web ext4 defaults 0 0" >> /etc/fstab //将新硬盘的挂在添加至/etc/fstab
[root@nfs&samba ~]# vim /etc/samba/smb.conf //配置samba workgroup = webdir netbios name = web hosts allow = 127. 172.16.
配置文件最后一行添加此信息
[webdir] comment = Public Stuff path = /web public = yes writable = yes printable = no write list = webserver
为samba提供登录用户:
[root@nfs&samba ~]# useradd -r webserver [root@nfs&samba ~]# chown -R webserver.webserver /web/ [root@nfs&samba ~]# smbpasswd -a webserver //将webserver用户添加至samba中(此命令由samba-client提供) New SMB password: Retype new SMB password: Added user webserver.
启动samba,并添加至开机自动启动:
[root@nfs&samba ~]# systemctl start smb [root@nfs&samba ~]# systemctl enable smb
安装nfs,并进行配置:
[root@nfs&samba ~]# yum install -y rpcbind nfs-utils [root@nfs&samba ~]# systemctl start rpcbind //启动rpcbind [root@nfs&samba ~]# systemctl enabled rpcbind //设置开启自动启动
添加一块新硬盘,用作nfs的共享目录
为新硬盘划分分区,并创建文件系统
[root@nfs&samba ~]# fdisk /dev/sdc Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0xccb37364. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-209715199, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): Using default value 209715199 Partition 1 of type Linux and of size 100 GiB is set Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
[root@nfs&samba ~]# mkfs -t ext4 /dev/sdc1 [root@nfs&samba ~]# mkdir /dbdir [root@nfs&samba ~]# echo "/dev/sdc1 /dbdir ext4 defaults 0 0" >> /etc/fstab [root@nfs&samba ~]# mount -a //挂在新硬盘 [root@nfs&samba ~]# chown -R nobody.nobody /dbdir/ [root@nfs&samba ~]# vim /etc/exports //编辑nfs的配置文件 /dbdir 172.16.0.0/16(sync,rw) [root@nfs&samba ~]# systemctl start nfs.service //启动nfs
3、包安装Mariadb,并载在nfs及samba
挂载nfs,并添加至开机自动挂载
[root@storage ~]# yum install -y nfs-utils rpcbind //支持nfs挂载 [root@storage ~]# mkdir /mydata [root@storage ~]# echo "172.16.113.54:/dbdir /mydata nfs defaults 0 0" >> /etc/fstab
安装mariadb:
[root@storage ~]# yum install -y mariadb-server mariadb 编辑配置文件,更改: [root@storage ~]# vi /etc/my.cnf datadir=/mydata/data 在mysqld innodb_file_per_table = on skip_name_resolve = on
[root@storage ~]# systemctl start mariadb [root@storage ~]# systemctl enable mariadb [root@storage ~]# mysql MariaDB [(none)]> CREATE DATABASE webdb; //创建数据库 MariaDB [(none)]> GRANT ALL ON webdb.* TO "webuser"@"172.16.%.%" IDENTIFIED BY 'root'; //授权用户 MariaDB [(none)]> FLUSH PRIVILEGES; //刷新数据库授权
4、编译安装php+Apache2.4(两台web服务器配置完全一样,此处只演示一台)
挂载samba,并添加开机自动挂载:
[root@web1 ~]# mkdir /sourcefile [root@web1 ~]# mount.cifs //172.16.113.54/webdir /sourcefile -o username=webserver,password=root [root@web1 ~]# echo "mount.cifs //172.16.113.54/webdir /sourcefile -o username=webserver,password=root" >> /etc/rc.d/rc.local
安装httpd2.2:
本机相关信息:
[root@web1 ~]# uname -r 2.6.32-504.el6.x86_64 [root@web1 ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@web1 ~]# ip add|awk -F'[ /]+' '/inet\>/&&!/127.0.0.1/ {print $3}' 172.16.113.55
需要的软件包:
httpd-2.4.16.tar.bz2 apr-1.5.0.tar.bz2 apr-util-1.5.3.tar.bz2
准备开发环境
[root@web1 apr-1.5.0]# yum groupinstall -y "Development Tools" "Server Platform Development"
安装apr
[root@web1 ~]# tar xf apr-1.5.0.tar.bz2 -C /usr/local/src/ [root@web1 ~]# cd /usr/local/src/apr-1.5.0/ [root@web1 apr-1.5.0]# ./configure --prefix=/usr/local/apr [root@web1 apr-1.5.0]# make && make install
安装apr-util
[root@web1 apr-1.5.0]# cd /root/ [root@web1 ~]# tar xf apr-util-1.5.3.tar.bz2 -C /usr/local/src/ [root@web1 ~]# cd /usr/local/src/apr-util-1.5.3/ [root@web1 apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr [root@web1 apr-util-1.5.3]# make && make install
安装httpd2.4
[root@web1 apr-util-1.5.3]# cd /root/ [root@web1 ~]# tar xf httpd-2.4.16.tar.bz2 -C /usr/local/src/ [root@web1 ~]# cd /usr/local/src/httpd-2.4.16/ [root@web1 httpd-2.4.16]# yum install -y pcre-devel //使httpd支持pcre正则表达式 ./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 [root@web1 httpd-2.4.16]# make && make install 编辑配置文件,添加Pidfile路径: [root@web1 httpd-2.4.16]# mkdir /var/run/httpd24 [root@web1 httpd-2.4.16]# vim /etc/httpd24/httpd.conf Pidfile "/var/run/httpd24/httpd.pid"
提供服务脚本:创建/etc/rc.d/init.d下的httpd24
#!/bin/bash # # chkconfig: - 85 15 # description: The Apache HTTP Server management script 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 -f ${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
[root@web1 httpd-2.4.16]# chkconfig --add httpd24 //添加开机自动启动项 [root@web1 httpd-2.4.16]# chkconfig httpd24 on //设置开机自动启动 [root@web1 httpd-2.4.16]# service httpd24 start //启动httpd
编译安装php以fpm方式结合httpd
需要的软件包:
php-5.6.13.tar.bz2
[root@web1 ~]# tar xf php-5.6.13.tar.bz2 -C /usr/local/src/ [root@web1 ~]# cd /usr/local/src/php-5.6.13/
解决依赖关系:
[root@web1 php-5.6.13]# yum install -y bzip2-devel libxml2-devel libmcrypt-devel [root@web1 php-5.6.13]# make && make install
将程序包自带的配置文件模板复制到/etc下作为php的配置文件
[root@web1 php-5.6.13]# cp php.ini-production /etc/php.ini
将程序包自带的服务脚本模板复制到/etc/rc.d/init.d下作为服务脚本,并添加至开机自动启动
[root@web1 php-5.6.13]# cp sapi/fpm/init.d.php-fpm /etc/init.d/phpfpm 修改服务脚本中的pid项: [root@web1 php-5.6.13]# vim /etc/rc.d/init.d/phpfpm php_fpm_PID=/var/run/php5/php-fpm.pid [root@web1 php-5.6.13]# chmod +x /etc/init.d/phpfpm //赋予服务脚本执行权限 [root@web1 php-5.6.13]# chkconfig --add phpfpm [root@web1 php-5.6.13]# chkconfig phpfpm on
将fpm的配置文件模板复制更名为fpm的配置文件,并做相应的配置:
[root@web1 php-5.6.13]# cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf [root@web1 php-5.6.13]# vim /usr/local/php5/etc/php-fpm.conf pid = /var/run/php5/php-fpm.pid //将此项前面的";"去掉,启用该项 修改 Pool Definitions中: pm.max_children = 100 pm.start_servers = 10 pm.min_spare_servers = 5 pm.max_spare_servers = 15 //此项一定要大于等于pm.min_spare_servers [root@web1 php-5.6.13]# mkdir -pv /var/run/php5 [root@web1 php-5.6.13]# service phpfpm start //启动php-fpm
编辑httpd的配置文件,开启以下两个模块,并做相应的配置:
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so 在最后添加如下两行: ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/sourcefile/$1 修改 DocumentRoot "/sourcefile" <Directory "/sourcefile"> DirectoryIndex index.php index.html 添加如下两行 AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps
重启httpd服务:
[root@web1 php-5.6.13]# service httpd24 restart
编辑测试脚本:
[root@web1 php-5.6.13]# vim /sourcefile/index.php <?php $conn = mysql_connect('172.16.113.57','webuser','root'); if ($conn) echo "OK"; else echo "Faulse"; phpinfo(); ?>
访问测试:(将物理主机的DNS指向自己搭建的DNS服务器地址)
搭建phpMyadmin实现数据库的图形接口管理
需要的软件包:
phpMyAdmin-4.4.14.1-all-languages.zip
[root@web1 ~]# unzip /root/phpMyAdmin-4.4.14.1-all-languages.zip -d /sourcefile/ [root@web1 ~]# cd /sourcefile/[root@web1 sourcefile]# ln -sv phpMyAdmin-4.4.14.1-all-languages pma &&&&将phpMyadmin配置文件模板复制改名: [root@web1 pma]# cp config.sample.inc.php config.inc.php
编辑配置文件
root@web1 pma]# vim config.inc.php
在浏览器访问登录即可:
安装xcache,实现php加速:
需要的软件包:
xcache-3.2.0.tar.gz 可在官网下载
[root@web1 ~]# tar xf xcache-3.2.0.tar.gz -C /usr/local/src
[root@web1 ~]# cd /usr/local/src/xcache-3.2.0/
[root@web1 xcache-3.2.0]# /usr/local/php5/bin/phpize //生成configure文件
[root@web1 xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/lo
cal/php5/bin/php-config
[root@web1 xcache-3.2.0]# make && make install
//此步骤结束后,会显示xcache的模块安装位置,例如: /usr/local/php5/lib/php/extensions/no-debug-non-zts-20131226/
[root@web1 xcache-3.2.0]# mkdir /etc/php.d //创建配置文件目录
[root@web1 xcache-3.2.0]# cp xcache.ini /etc/php.d/ //为xcache提供配置文件
[root@web1 xcache-3.2.0]# vim /etc/php.d/xcache.ini //编辑配置文件,指定xcache模块的安装位置
extension = /usr/local/php5/lib/php/extensions/no-debug-non-zts-20131226/xcache.so
其他保持默认即可
[root@web1 xcache-3.2.0]# service phpfpm restart //由于php工作在fpm模式,即php现在是一个独立的服务,因此更改了配置重启php-fpm服务即可。
刷新phpinfo页面,出现以下信息便是添加成功。
待续~~~~~