大纲
一、编译安装Apache
二、安装Mysql数据库
三、编译安装PHP
四、整合Apache与PHP
五、安装Xcache加速器
六、启用服务器状态
七、PHP连接Mysql测试
八、安装Discuz论坛(Discuz_X3.0_SC_GBK)
注:所有软件版本 CentOS6.4+httpd2.4.4+mysql5.6.12+php5.4.16+xcache3.03 都是目前最新(所有的安装包点击这里下载)
一、编译安装Apache
1. 安装前准备
修改yum源(163)
[root@web ~]# yum install wget [root@web ~]# wget http://mirrors.163.com/.help/CentOS6-Base-163.repo --2013-06-29 18:39:02-- http://mirrors.163.com/.help/CentOS6-Base-163.repo Resolving mirrors.163.com... 123.58.173.106 Connecting to mirrors.163.com|123.58.173.106|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 2006 (2.0K) [application/octet-stream] Saving to: a�?CentOS6-Base-163.repo? 100%[================>] 2,006 --.-K/s in 0s 2013-06-29 18:39:02 (152 MB/s) - a�?CentOS6-Base-163.repoa�? saved [2006/2006] [root@web ~]# cd /etc/yum.repos.d/ [root@web yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak [root@web yum.repos.d]# mv /root/CentOS6-Base-163.repo CentOS-Base.repo [root@web yum.repos.d]# ls CentOS-Base.repo CentOS-Media.repo CentOS-Base.repo.bak CentOS-Vault.repo CentOS-Debuginfo.repo [root@web yum.repos.d]# yum clean all Loaded plugins: fastestmirror Cleaning repos: base extras updates Cleaning up Everything Cleaning up list of fastest mirrors [root@web yum.repos.d]# yum makecache Loaded plugins: fastestmirror Determining fastest mirrors base | 3.7 kB 00:00 base/group_gz | 212 kB 00:00 base/filelists_db | 5.9 MB 00:01 base/primary_db | 4.4 MB 00:01 base/other_db | 2.7 MB 00:00 extras | 3.4 kB 00:00 extras/filelists_db | 10 kB 00:00 extras/prestodelta | 905 B 00:00 extras/primary_db | 18 kB 00:00 extras/other_db | 5.7 kB 00:00 updates | 3.4 kB 00:00 updates/filelists_db | 2.9 MB 00:00 updates/prestodelta | 617 kB 00:02 updates/primary_db | 3.2 MB 00:00 updates/other_db | 419 kB 00:00 Metadata Cache Created [root@web yum.repos.d]#
安装编译工具
[root@web yum.repos.d]#yum groupinstall "Development Tools" "Development Libraries" �Cy [root@web yum.repos.d]# yum grouplist Loaded plugins: fastestmirror Setting up Group Process Loading mirror speeds from cached hostfile Installed Groups: Development tools E-mail server Fonts General Purpose Desktop Graphical Administration Tools Input Methods Legacy X Window System compatibility MySQL Database client Perl Support关闭SElinux
[root@web ~]# vim /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of these two values: # targeted - Targeted processes are protected, # mls - Multi Level Security protection. SELINUXTYPE=targeted [root@web ~]# reboot关闭防火墙
[root@web ~]# service iptables stop [root@web ~]# service ip6tables stop [root@web ~]# chkconfig iptables off [root@web ~]# chkconfig ip6tables off [root@web ~]# chkconfig iptables --list iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@web ~]# chkconfig ip6tables --list ip6tables 0:off 1:off 2:off 3:off 4:off 5:off 6:off修改主机名
[root@web ~]# vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=web.test.com ~ [root@web ~]# vim /etc/hosts 127.0.0.1 web web.test.com localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
2. 解决依赖关系
httpd-2.4.4需要较新版本的apr和apr-util,因此需要事先对其进行升级。升级方式有两种,一种是通过源代码编译安装,一种是直接升级rpm包。
(1). 编译安装apr
[root@web src]# tar xf apr-1.4.6.tar.bz2 [root@web src]# cd apr-1.4.6 [root@web src]# ./configure --help | less (可以查看帮助文件) [root@web src]# ./configure --prefix=/usr/local/apr (指定安装路径) [root@web src]# make [root@web src]# make install
(2). 编译安装apr-util
[root@web src]# tar xf apr-util-1.5.2.tar.bz2 [root@web src]# cd apr-util-1.5.2 [root@web src]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr (--prefix指定安装路径;--with-apr指定apr的安装路径,apr-util依赖于apr) [root@web src]# make && make install 附:apache官方对APR的介绍: The mission of the Apache Portable Runtime (APR) project is to create and maintain software libraries that provide a predictable and consistent interface to underlying platform-specific implementations. The primary goal is to provide an API to which software developers may code and be assured of predictable if not identical behaviour regardless of the platform on which their software is built, relieving them of the need to code special-case conditions to work around or take advantage of platform-specific deficiencies or features.
(3). httpd-2.4.4编译过程也要依赖于pcre-devel软件包,需要事先安装。
[root@web ~]# yum install pcre-devel -y
3. 编译安装httpd-2.4.4
[root@web src]# tar xf httpd-2.4.4.tar.gz [root@web src]# cd httpd-2.4.4 [root@web httpd-2.4.4]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-cgid --enable-modules=most --enable-mods-shared=most --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-mpms-shared=all --with-mpm=event --sysconfdir=/etc/httpd :指定配置文件安装位置 --enable-so :支持动态共享模块如果没有这个模块PHP将无法与apache结合工作 --enable-ssl :启用支持ssl --enable-cgi :支持cgi --enable-rewrite :支持URL重写 --with-zlib :压缩库,在互联网上传播时可节约带宽 --with-apr=/usr/local/apr :指定apr路径 --with-apr-util=/usr/local/apr-util :指定apr-util路径 --enable-mpms-shared=all :支持多道处理模块 --with-mpm=event :设定默认的模块
错误:
checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures
解决方法:
[root@web httpd-2.4.4]# yum install -y openssl-devel
补充说明:
(1)构建MPM为静态模块
在全部平台中,MPM都可以构建为静态模块。在构建时选择一种MPM,链接到服务器中。如果要改变MPM,必须重新构建。为了使用指定的MPM,请在执行configure脚本 时,使用参数 --with-mpm=NAME。NAME是指定的MPM名称。编译完成后,可以使用 httpd -l 来确定选择的MPM。 此命令会列出编译到服务器程序中的所有模块,包括 MPM。
(2)构建 MPM 为动态模块
在Unix或类似平台中,MPM可以构建为动态模块,与其它动态模块一样在运行时加载。 构建 MPM 为动态模块允许通过修改LoadModule指令内容来改变MPM,而不用重新构建服务器程序。在执行configure脚本时,使用--enable-mpms-shared选项即可启用此特性。当给出的参数为all时,所有此平台支持的MPM模块都会被安装。还可以在参数中给出模块列表。默认MPM,可以自动选择或者在执行configure脚本时通过--with-mpm选项来指定,然后出现在生成的服务器配置文件中。编辑LoadModule指令内容可以选择不同的MPM。
4. 修改httpd的主配置文件,设置其Pid文件的路径
说明:手动编译安装后,httpd.pid文件是存放在/usr/local/apache/logs/目录下的,这个位置未免有些不方便。
更改配置文件
[root@web httpd-2.4.4]# cd [root@web ~]# cd /etc/httpd/ [root@web httpd]# ls conf conf.d extra httpd.conf logs magic mime.types modules original run [root@web httpd]# cp httpd.conf httpd.conf.bak [root@web httpd]# vim httpd.conf
#在配置文件中找一个位置定义一下Pid文件路径就可以了
Pidfile "/var/run/httpd.pid"
5. 提供SysV服务脚本/etc/rc.d/init.d/httpd
[root@web httpd]# vim /etc/init.d/httpd #!/bin/bash # # chkconfig: - 85 15 # description: Apache is a World Wide Web server. It is used to serve \ . /etc/rc.d/init.d/functions -----读取函数 if [ -f /etc/sysconfig/httpd ]; then . /etc/sysconfig/httpd fi # Start httpd in the C locale by default. HTTPD_LANG=${HTTPD_LANG-"C"} # This will prevent initlog from swallowing up a pass-phrase prompt if # mod_ssl needs a pass-phrase from the user. INITLOG_ARGS="" # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server # with the thread-based "worker" MPM; BE WARNED that some modules may not # work correctly with a thread-based MPM; notably PHP will refuse to start. # Path to the apachectl script, server binary, and short-form for messages. apachectl=/usr/local/apache/bin/apachectl # -----指定apachectl程序位置 httpd=${HTTPD-/usr/local/apache/bin/httpd} #-------httpd程序位置 prog=httpd pidfile=${PIDFILE-/var/run/httpd.pid} #----如果文件存在就使用存在文件路径,如果不存在就使用/var/rum/httpd.pid lockfile=${LOCKFILE-/var/lock/subsys/httpd} # --------创建的锁文件 RETVAL=0 start() { echo -n $"Starting $prog: " LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS #----以$pidfile文件执行httpd 并且使用选项start RETVAL=$? #------定义执行状态返回值 echo [ $RETVAL = 0 ] && touch ${lockfile} #-----成功时创建锁文件 return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} -d 10 $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=$? echo $"not reloading due to configuration syntax error" failure $"not reloading $httpd due to configuration syntax error" else killproc -p ${pidfile} $httpd -HUP RETVAL=$? fi echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; condrestart) if [ -f ${pidfile} ] ; then stop start fi ;; reload) reload ;; graceful|help|configtest|fullstatus) $apachectl $@ RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" exit 1 esac exit $RETVAL
[root@web httpd]# chmod +x /etc/init.d/httpd #添加执行权限
#设置开机自启动
[root@web httpd]# chkconfig httpd --add [root@web httpd]# chkconfig httpd on [root@web httpd]# chkconfig httpd --list httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@web httpd]#
6. 启动apache并测试
[root@web httpd]# service httpd start Starting httpd: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using web.test.com. Set the 'ServerName' directive globally to suppress this message [ OK ] [root@web httpd]# [root@web httpd]# netstat -ntulp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 990/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1066/master tcp 0 0 :::80 :::* LISTEN 36920/httpd tcp 0 0 :::22 :::* LISTEN 990/sshd tcp 0 0 ::1:25 :::* LISTEN 1066/master udp 0 0 0.0.0.0:68 0.0.0.0:* 890/dhclient [root@web httpd]#
启动成功了,但似乎有警告,我们看一下!
Starting httpd: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using web.test.com. Set the 'ServerName' directive globally to suppress this message
解决方案:
编辑httpd.conf文件,搜索"/ServerName",添加ServerName localhost:80
[root@web httpd]# vim /etc/httpd/httpd.conf 再重新启动apache 即可。 [root@web httpd]# service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ] [root@web httpd]#
好了问题解决,下面我们用Windows 7访问一下!效果如下:
好了测试成功,下面我们继续!
7. 为httpd服务的相关命令添加环境变量
[root@web httpd]# httpd -bash: httpd: command not found [root@web httpd]#
大家看到了,虽然我们可以使用 service httpd restart 来重新启动Apache,是我们无法使用httpd命令,下面我们就来解决一下!直接在/etc/profile.d目录下创建的httpd.sh环境变量文件,因为在默认的情况下这个目录下定义的环境变量都是profile文件的组成部分。
[root@web httpd]# vim /etc/profile.d/httpd.sh export PATH=$PATH:/usr/local/apache/bin [root@web httpd]# source /etc/profile #重新读取一下环境变量 [root@web httpd]# httpd �Ct #测试一下 Syntax OK [root@web httpd]#
现在httpd服务的相关命令就可以直接使用了,嘿嘿!
二、安装Mysql数据库
1. 准备数据存放的文件系统
说明:新建一个逻辑卷,并将其挂载至特定目录即可。这里假设其逻辑卷的挂载目录为/mydata,而后需要创建/mydata/data目录做为mysql数据的存放目录。
(1). 先确认下系统里是否有LVM工具,默认没有安装
[root@web httpd]# rpm -qa | grep lvm [root@web httpd]# yum install -y lvm2
(2). 查看一下磁盘
[root@web httpd]# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x0000a0a2 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 1301 10240000 83 Linux /dev/sda3 1301 1938 5120000 83 Linux /dev/sda4 1938 2611 5405696 5 Extended /dev/sda5 1939 2066 1024000 82 Linux swap / Solaris [root@web httpd]#
(3). 创建逻辑分区
[root@web ~]# fdisk /dev/sda /dev/sda7 2066 2327 2099724 8e Linux LVM #我这里是测试环境就创建了一个2G分区 [root@web ~]# partx -a /dev/sda #告诉内核有关存在和磁盘上的分区的编号 [root@web ~]# pvcreate /dev/sda7 #创建物理卷 Physical volume "/dev/sda7" successfully created [root@web ~]# vgcreate myvg /dev/sda7 #创建卷组 Volume group "myvg" successfully created [root@web ~]# [root@web ~]# lvcreate -n mydata -L 1G myvg #创建一个1G的逻辑卷 Logical volume "mydata" created [root@web ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert mydata myvg -wi-a---- 1.00g [root@web ~]# [root@web ~]# mkfs.ext4 /dev/myvg/mydata #格式化 [root@web ~]# mkdir /mydata #创建挂载目录 [root@web ~]# mount /dev/myvg/mydata /mydata/ #挂载 [root@web ~]# vim /etc/fstab /dev/myvg/mydata /mydata ext4 defaults 0 0 #增加这一行 [root@web ~]# mount �Ca #测试挂载是否成功 [root@web ~]# mount /dev/sda2 on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw) /dev/sda1 on /boot type ext4 (rw) /dev/sda3 on /data type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) /dev/mapper/myvg-mydata on /mydata type ext4 (rw) [root@web ~]#
(4). 为了便于管理在/mydata目录下再创建个子目录data用于存放数据
[root@web ~]# mkdir /mydata/data [root@web ~]# ls /mydata/ data lost+found [root@web ~]#
2. 新建用户以安全方式运行进程
[root@web ~]# groupadd -r mysql [root@web ~]# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql [root@web ~]# chown -R mysql:mysql /mydata/data
3. 安装并初始化mysql5.6.12
(1). 说明:mysql 安装包有三种式,rpm,源码包,二进制包(已编译好,解压后简单配置一下就可以用),我这里用的就是二进制包
[root@web src]# tar -xf mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ #-C指定解压目录 [root@web local]# ln -sv mysql-5.6.12-linux-glibc2.5-x86_64/ mysql #创建软链接 `mysql' -> `mysql-5.6.12-linux-glibc2.5-x86_64/' [root@web local]# cd mysql [root@web mysql]# ls bin data include lib mysql-test scripts sql-bench COPYING docs INSTALL-BINARY man README share support-files [root@web mysql]# [root@web mysql]# chown -R mysql:mysql . #更改属主属组 [root@web mysql]# ll total 76 drwxr-xr-x 2 mysql mysql 4096 Jun 29 21:12 bin -rw-r--r-- 1 mysql mysql 17987 May 21 23:18 COPYING drwxr-xr-x 3 mysql mysql 4096 Jun 29 21:12 data drwxr-xr-x 2 mysql mysql 4096 Jun 29 21:12 docs drwxr-xr-x 3 mysql mysql 4096 Jun 29 21:12 include -rw-r--r-- 1 mysql mysql 7469 May 21 23:18 INSTALL-BINARY drwxr-xr-x 3 mysql mysql 4096 Jun 29 21:12 lib drwxr-xr-x 4 mysql mysql 4096 Jun 29 21:12 man drwxr-xr-x 10 mysql mysql 4096 Jun 29 21:12 mysql-test -rw-r--r-- 1 mysql mysql 2496 May 21 23:18 README drwxr-xr-x 2 mysql mysql 4096 Jun 29 21:12 scripts drwxr-xr-x 28 mysql mysql 4096 Jun 29 21:12 share drwxr-xr-x 4 mysql mysql 4096 Jun 29 21:12 sql-bench drwxr-xr-x 3 mysql mysql 4096 Jun 29 21:12 support-files
(2). 执行mysql 初始化的data存放位置的准备
[root@web mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data #执行mysql 初始化的data存放位置的准备 Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory #初始化时报错说缺少libaio.so我们安装一下 [root@web mysql]# yum install libaio [root@web mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data #再次执行mysql 初始化的data存放位置的准备 To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: ./bin/mysqladmin -u root password 'new-password' ./bin/mysqladmin -u root -h web.test.com password 'new-password' Alternatively you can run: ./bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd . ; ./bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd mysql-test ; perl mysql-test-run.pl Please report any problems with the ./bin/mysqlbug script! The latest information about MySQL is available on the web at http://www.mysql.com Support MySQL by buying support/licenses at http://shop.mysql.com WARNING: Found existing config file ./my.cnf on the system. Because this file might be in use, it was not replaced, but was used in bootstrap (unless you used --defaults-file) and when you later start the server. The new default config file was created as ./my-new.cnf, please compare it with your file and take the changes you need. WARNING: Default config file /etc/my.cnf exists on the system This file will be read by default by the MySQL server If you do not want to use this, either remove it, or use the --defaults-file argument to mysqld_safe when starting the server [root@web mysql]# [root@web mysql]# ls /mydata/data/ #查看 data 目录有文件说明初始化成功 ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test
(3). 初始化完成后mysql中目录文件的属主应改回成root,以免被别人攻破mysql用户密码而带来数据破坏等
[root@web mysql]# cd /usr/local/mysql/ [root@web mysql]# chown root /usr/local/mysql/* -R [root@web mysql]# ll total 84 drwxr-xr-x 2 root mysql 4096 Jun 29 21:12 bin -rw-r--r-- 1 root mysql 17987 May 21 23:18 COPYING drwxr-xr-x 3 root mysql 4096 Jun 29 21:12 data drwxr-xr-x 2 root mysql 4096 Jun 29 21:12 docs drwxr-xr-x 3 root mysql 4096 Jun 29 21:12 include -rw-r--r-- 1 root mysql 7469 May 21 23:18 INSTALL-BINARY drwxr-xr-x 3 root mysql 4096 Jun 29 21:12 lib drwxr-xr-x 4 root mysql 4096 Jun 29 21:12 man -rw-r--r-- 1 root root 943 Jun 29 21:18 my.cnf -rw-r--r-- 1 root root 943 Jun 29 21:23 my-new.cnf drwxr-xr-x 10 root mysql 4096 Jun 29 21:12 mysql-test -rw-r--r-- 1 root mysql 2496 May 21 23:18 README drwxr-xr-x 2 root mysql 4096 Jun 29 21:12 scripts drwxr-xr-x 28 root mysql 4096 Jun 29 21:12 share drwxr-xr-x 4 root mysql 4096 Jun 29 21:12 sql-bench drwxr-xr-x 3 root mysql 4096 Jun 29 21:12 support-files [root@web mysql]#
4. 为mysql提供主配置文件
初始化后会自动在当前目录下创建一个my.cnf配置文件,直接修改就可以(在mysql 5.6 以后配置文件自动生成,不需要我们再进行复制)
(1). 查看配置文件
[root@web mysql]# ll total 84 drwxr-xr-x 2 root mysql 4096 Jun 29 21:12 bin -rw-r--r-- 1 root mysql 17987 May 21 23:18 COPYING drwxr-xr-x 3 root mysql 4096 Jun 29 21:12 data drwxr-xr-x 2 root mysql 4096 Jun 29 21:12 docs drwxr-xr-x 3 root mysql 4096 Jun 29 21:12 include -rw-r--r-- 1 root mysql 7469 May 21 23:18 INSTALL-BINARY drwxr-xr-x 3 root mysql 4096 Jun 29 21:12 lib drwxr-xr-x 4 root mysql 4096 Jun 29 21:12 man -rw-r--r-- 1 root root 943 Jun 29 21:18 my.cnf -rw-r--r-- 1 root root 943 Jun 29 21:23 my-new.cnf drwxr-xr-x 10 root mysql 4096 Jun 29 21:12 mysql-test -rw-r--r-- 1 root mysql 2496 May 21 23:18 README drwxr-xr-x 2 root mysql 4096 Jun 29 21:12 scripts drwxr-xr-x 28 root mysql 4096 Jun 29 21:12 share drwxr-xr-x 4 root mysql 4096 Jun 29 21:12 sql-bench drwxr-xr-x 3 root mysql 4096 Jun 29 21:12 support-files [root@web mysql]# [root@web mysql]# cat my.cnf # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. # basedir = ..... # datadir = ..... # port = ..... # server_id = ..... # socket = ..... # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [root@web mysql]#
大家可以看到生成的配置文件很简单,我们得添加一些内容!
(2). 修改配置文件
[root@web mysql]# vim my.cnf 添加内容是: binlog-format=ROW log-bin=master-bin.log log-slave-updates=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=2 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 server-id=1 report-port=3306 port=3306 datadir=/mydata/data socket=/tmp/mysql.sock report-host=master.test.com
5. 为mysql提供sysv服务脚本并启动服务
[root@web mysql]# cp support-files/mysql.server /etc/init.d/mysqld #复制sysv脚本 [root@web mysql]# chkconfig --add mysqld [root@web mysql]# chkconfig mysqld on #开机自启动 [root@web mysql]# chkconfig mysqld --list mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@web mysql]# service mysqld start Starting MySQL.... SUCCESS! [root@web mysql]# netstat -ntulp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 990/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1066/master tcp 0 0 :::80 :::* LISTEN 37120/httpd tcp 0 0 :::22 :::* LISTEN 990/sshd tcp 0 0 ::1:25 :::* LISTEN 1066/master tcp 0 0 :::3306 :::* LISTEN 37924/mysqld #mysql启动成功 udp 0 0 0.0.0.0:68 0.0.0.0:* 890/dhclient
6. 输出mysql的man手册至man命令的查找路径
[root@web mysql]# vim /etc/man.config MANPATH /usr/local/mysql/man #增加这一行
7. 输出mysql的头文件至系统头文件路径/usr/include
[root@web mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql #输出mysql的头文件至系统头文件 `/usr/include/mysql' -> `/usr/local/mysql/include/' [root@web mysql]# cd /usr/include/mysql/
8. 输出mysql的库文件给系统库查找路径
[root@web mysql]# vim /etc/ld.so.conf.d/mysql.conf /usr/local/mysql/lib #直接新建编辑/etc/ld.so.conf.d/mysql.conf文件,把mysql的库文件路径添加进去就可以了 ~ [root@web mysql]# ldconfig �Cv# 让系统重新读取库文件 /usr/local/mysql/lib: libtcmalloc_minimal.so.0 -> libtcmalloc_minimal.so (changed) libmysqlclient.so.18 -> libmysqlclient_r.so.18.0.0 /usr/lib64/mysql: libmysqlclient.so.16 -> libmysqlclient.so.16.0.0 libmysqlclient_r.so.16 -> libmysqlclient_r.so.16.0
9. 修改PATH环境变量,让系统可以直接使用mysql的相关命令
[root@web mysql]# vim /etc/profile.d/mysql.sh #添加环境变量(与添加httpd是一样的) export PATH=$PATH:/usr/local/mysql/bin [root@web mysql]# source /etc/profile #重新读取一下环境变量
10. 测试并连接mysql
[root@web mysql]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.12-log MySQL Community Server (GPL) Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.02 sec) mysql>
三、编译安装PHP
1. 编译安装php-5.4.16
说明:
(1). 这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。
(2). 如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。
# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd # make # make test # make intall
(1). 安装图片资源软件
[root@web php-5.4.16]# yum -y install gd gd-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel
(2). 安装libxml(扩展标记语言)库
[root@web php-5.4.16]# yum -y install libxml2 libxml2-devel
(3). 安装bzip2压缩库
[root@web php-5.4.16]# yum install -y bzip2 bzip2-devel
(4). 安装mcrypt加密库
[root@web src]# rpm -ivh libmcrypt-2.5.8-4.3.x86_64.rpm warning: libmcrypt-2.5.8-4.3.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 04b8b50a: NOKEY Preparing... ########################################### [100%] 1:libmcrypt ########################################### [100%] [root@web src]# rpm -ivh libmcrypt-devel-2.5.8-4.3.x86_64.rpm warning: libmcrypt-devel-2.5.8-4.3.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 04b8b50a: NOKEY Preparing... ########################################### [100%] 1:libmcrypt-devel ########################################### [100%] [root@web src]#
(5). 编译php 5.4.16
[root@web php-5.4.16]# tar -xf php-5.4.16.tar.gz [root@web php-5.4.16]# cd php-5.4.16 [root@web php-5.4.16]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-gd --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-mysql=/usr/local/mysql :mysql相关 --with-openssl :让其能够支持openssl功能 --with-mysqli=/usr/local/mysql/bin/mysql_config :mysql的另外一个接口,让mysql与php交互的接口;接口程序是mysql_config,是个二进制程序 --enable-mbstring :多字节string,支持中文或者是非一个字节能够表示的语言 --with-gd : 支持gd库 --with-freetpye-dir:支持freetype功能,freetype:自由的可移植的字体库,可以实现去引用特定字体的 --with-jpeg-dir:支持jpeg图片 --with-png-dir:支持png图片 --with-zlib:互联网上常用的,通用格式的压缩库,让数据文件先压缩再传送给客户端 --with-libxml-dir:xml(扩展标记语言),现在的很多系统在实现数据交互的时候,都要基于xml来实现,所以要php支持xml,并且让其知道其库文件所在位置 --enable-sockets:让php支持基于套接字的通信 --with-apxs2:基于apxs实现让php编译成apace模块 --with-mcrypt:支持加密功能的,额外的加密库 --with-config-file-path :php配置文件的路径放在了什么地方 主配置文件是php.ini --with-config-file-scan :主配置文件的片段,也是配置文件,这个路径下以.ini结尾的都是配置文件片段 --with-bz2 :压缩库 --enable-maintainer-zts :这一项的使用取决于apache是什么类型的,apache使用的是prefork就不需要;如果使用的是event或者是worker就要添加这一项;apache以线程工作就必须编译这一项 Generating files configure: creating ./config.status creating main/internal_functions.c creating main/internal_functions_cli.c +--------------------------------------------------------------------+ | License: | | This software is subject to the PHP License, available in this | | distribution in the file LICENSE. By continuing this installation | | process, you are bound by the terms of this license agreement. | | If you do not agree with the terms of this license, you must abort | | the installation process at this point. | +--------------------------------------------------------------------+ Thank you for using PHP. config.status: creating php5.spec config.status: creating main/build-defs.h config.status: creating scripts/phpize config.status: creating scripts/man1/phpize.1 config.status: creating scripts/php-config config.status: creating scripts/man1/php-config.1 config.status: creating sapi/cli/php.1 config.status: creating main/php_config.h config.status: executing default commands #编译完成 [root@web php-5.4.16]# make && make install Installing PHP SAPI module: apache2handler /usr/local/apache/build/instdso.sh SH_LIBTOOL='/usr/local/apr/build-1/libtool' libphp5.la /usr/local/apache/modules /usr/local/apr/build-1/libtool --mode=install install libphp5.la /usr/local/apache/modules/ libtool: install: install .libs/libphp5.so /usr/local/apache/modules/libphp5.so libtool: install: install .libs/libphp5.lai /usr/local/apache/modules/libphp5.la libtool: install: warning: remember to run `libtool --finish /root/src/php-5.4.16/libs' chmod 755 /usr/local/apache/modules/libphp5.so [activating module `php5' in /etc/httpd/httpd.conf] Installing PHP CLI binary: /usr/local/php/bin/ Installing PHP CLI man page: /usr/local/php/php/man/man1/ Installing PHP CGI binary: /usr/local/php/bin/ Installing build environment: /usr/local/php/lib/php/build/ Installing header files: /usr/local/php/include/php/ Installing helper programs: /usr/local/php/bin/ program: phpize program: php-config Installing man pages: /usr/local/php/php/man/man1/ page: phpize.1 page: php-config.1 Installing PEAR environment: /usr/local/php/lib/php/ [PEAR] Archive_Tar - installed: 1.3.11 [PEAR] Console_Getopt - installed: 1.3.1 warning: pear/PEAR requires package "pear/Structures_Graph" (recommended version 1.0.4) warning: pear/PEAR requires package "pear/XML_Util" (recommended version 1.2.1) [PEAR] PEAR - installed: 1.9.4 Wrote PEAR system config file at: /usr/local/php/etc/pear.conf You may want to add: /usr/local/php/lib/php to your php.ini include_path [PEAR] Structures_Graph- installed: 1.0.4 [PEAR] XML_Util - installed: 1.2.1 /root/src/php-5.4.16/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin ln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/phar Installing PDO headers: /usr/local/php/include/php/ext/pdo/ [root@web php-5.4.16]#
#安装完成
(6). 为php提供配置文件
查看配置文件
[root@web php-5.4.16]# ls | grep php.ini php.ini-development php.ini-production [root@web php-5.4.16]# php.ini-development :用于开发环境 php.ini-production :用于生产环境:把这项复制到/etc/目录下重命名为php.ini即可;不需要启动服务,因为已经编译成apache模块了;是否启动服务要看工作模型,当做成fastcgi要启动服务 [root@web php-5.4.16]# cd php-5.4.16 [root@web php-5.4.16]# cp php.ini-production /etc/php.ini
(在编译的时候已经指定了配置文件的路径,所以php会自动到/etc/目录下去找其配置文件)
四、整合Apache与PHP
1. 编辑apache配置文件httpd.conf,以apache支持php
(1). 首先要在apache配置文件中定义,使apache能够处理php结尾的文件 ,全文查找AddType字段,添加一下内容
[root@web php-5.4.16]# vim /etc/httpd/httpd.conf AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps PHPIniDir "/usr/local/php"
(2). 定位至DirectoryIndex index.html修改为:
DirectoryIndex index.php index.html
2. 重启apache并测试
[root@web php-5.4.16]# service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ] [root@web php-5.4.16]#
3. 增加测试文件
[root@web php-5.4.16]# vim /usr/local/apache/htdocs/test.php <? phpinfo(); ?>
4. 测试效果
5. 问题
在用PHP5.3以上的PHP版本时,只要是涉及时间的会报一个错!
执行phpinfo();时提示:
Warning: phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /usr/local/apache/htdocs/test.php on line 2
解决办法有三种:
(1). 在页头使用date_default_timezone_set()设置 date_default_timezone_set('PRC'); //东八时区 echo date('Y-m-d H:i:s');
[root@web php-5.4.16]# vim /usr/local/apache/htdocs/test.php <? date_default_timezone_set("PRC"); phpinfo(); ?>
(2). 在页头使用 ini_set('date.timezone','Asia/Shanghai');
(3). 修改php.ini。打开php.ini查找date.timezone 去掉前面的分号修改成为:date.timezone ="PRC" PRC注意加上双引号,要不还会出错!
[root@web php-5.4.16]# vim /etc/php.ini date.timezone = "PRC"
五、安装Xcache加速器
1. 为php添加xcache功能
[root@web src]# tar -xf xcache-3.0.3.tar.gz [root@web src]# cd xcache-3.0.3 [root@web xcache-3.0.3]# /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20100412 Zend Module Api No: 20100525 Zend Extension Api No: 220100525 [root@web xcache-3.0.3]#
2. 编译安装Xcache 3.0.3
[root@web xcache-3.0.3]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config [root@web xcache-3.0.3]# make && make install 编译完成,让php支持xcache功能前提要把xcache的配置信息添加到php的配置文件中 Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20100525/ [root@web xcache-3.0.3]#
3. 编辑php.ini,整合php和xcache
[root@web xcache-3.0.3]# mkdir /etc/php.d [root@web xcache-3.0.3]# cp xcache.ini /etc/php.d/ #也可以直接把xcache.ini配置信息直接追加到php.ini配置文件中 [root@web xcache-3.0.3]# ls /etc/php.d/ xcache.ini [root@web xcache-3.0.3]# vim /etc/php.d/xcache.ini # 接下来编辑/etc/php.d/xcache.ini,找到zend_extension开头的行,修改为如下行: [xcache-common] ;; non-Windows example: extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so #修改这一行 ;; Windows example: ; extension = php_xcache.dll
4. 重启apache并测试
[root@web xcache-3.0.3]# service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ] [root@web xcache-3.0.3]#
说明:测试可以使用windows主机访问linux主机IP,查看php.info中是否有xcache模块相关信息
六、启用服务器状态
说明:
mod_status模块可以让管理员查看服务器的执行状态,它通过一个HTML页面展示了当前服务器的统计数据。这些数据通常包括但不限于:
(1) 处于工作状态的worker进程数;
(2) 空闲状态的worker进程数;
(3) 每个worker的状态,包括此worker已经响应的请求数,及由此worker发送的内容的字节数;
(4) 当前服务器总共发送的字节数;
(5) 服务器自上次启动或重启以来至当前的时长;
(6) 平均每秒钟响应的请求数、平均每秒钟发送的字节数、平均每个请求所请求内容的字节数;
启用状态页面的方法很简单,只需要在主配置文件中添加如下内容即可:
[root@web xcache-3.0.3]# vim /etc/httpd/httpd.conf <Location /server-status> SetHandler server-status Require all granted </Location>
需要提醒的是,这里的状态信息不应该被所有人随意访问,因此,应该限制仅允许某些特定地址的客户端查看。比如使用Require ip 172.16.0.0/16来限制仅允许指定网段的主机查看此页面 。
效果图:
七、PHP连接Mysql测试
(1). 编辑apache的默认页面/usr/local/apache/htdocs/test.php
[root@web xcache-3.0.3]# vim /usr/local/apache/htdocs/test.php <? $conn=mysql_connect('localhost','root',''); if ($conn) echo "Success"; else echo "Failure"; ?>
(2). 测试
连接成功!
八、安装Discuz论坛(Discuz_X3.0_SC_GBK最新版)
(1). 授权主机对mysql的访问
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.%.%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.03 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.02 sec)
(2). 准备Discuz 3.0
[root@web src]# unzip Discuz_X3.0_SC_GBK.zip [root@web src]# mv upload/* /usr/local/apache/htdocs/
(3). 安装
(4). 修改权限
[root@web htdocs]# chmod 777 ./data ./config/ ./data/cache/ ./data/avatar/ ./data/plugindata/ ./data/download/ ./data/addonmd5/ ./data/template/ ./ data/threadcache/ ./data/attachment/ ./data/attachment/album/ ./data/attachment/forum/ ./data/attachment/group/ ./data/log/ ./uc_client/data/cache/ ./uc_server/data ./uc_server/data/avatar/ ./uc_server/data/backup/ ./uc_server/data/logs/ ./uc_server/data/tmp/ ./uc_server/data/view/ ./uc_server/data/cache/ [root@web htdocs]#
修改后效果如下:
(5). 选择全新安装
(6). 配置数据库文件
(7). 安装完成
(8). 最终效果
所有演示全部完成 ! ^_^ ……