1、什么是LAMP

LAMP=linux+apache+mysql+php

Apache是一种web服务器。mysql是一种小型数据库。php是一种语言,可以写网站程序。

静态网页和动态网页的区别是否涉及数据库。

rpm包类似于windows的.exe程序,依赖平台redhat、centos。源码包大多是C语言写的,是看得见的、可以更改,且不依赖于平台。

2、安装mysql(按顺序安装)

(1)下载mysql-5.1.40-linux-i686-icc-glibc23.tar.gz 二进制免编译包

[root@localhost ~]# cd /usr/local/src/     //默认将安装包放在此目录
[root@localhost src]# wget http://syslab.comsenz.com/downloads/linux/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz     //下载32位mysql免编译包
--2014-04-16 09:13:27--  http://syslab.comsenz.com/downloads/linux/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz
Resolving syslab.comsenz.com... 180.153.5.50
Connecting to syslab.comsenz.com|180.153.5.50|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 123633020 (118M) [application/octet-stream]
Saving to: “mysql-5.1.40-linux-i686-icc-glibc23.tar.gz”
100%[======================================>] 123,633,020  190K/s   in 12m 44s
2014-04-16 09:26:21 (158 KB/s) - “mysql-5.1.40-linux-i686-icc-glibc23.tar.gz” saved [123633020/123633020]

(2)解压mysql免编译包

[root@localhost src]# tar zxvf mysql-5.1.40-linux-i686-icc-glibc23.tar.gz

(3)将解压目录移到并改名为/usr/local/mysql

[root@localhost src]# mv mysql-5.1.40-linux-i686-icc-glibc23 /usr/local/mysql

(4)创建mysql用户,不能登录

[root@localhost src]# useradd -s /sbin/nologin mysql

(5)创建存放数据的目录/data/mysql

[root@localhost src]# cd /usr/local/mysql/
[root@localhost mysql]# mkdir -p /data/mysql

(6)更改/data/mysql属性

[root@localhost mysql]# chown -R mysql:mysql /data/mysql

(7)初始化数据库

[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MySQL system tables...
ERROR: 1004  Can't create file '/tmp/#sql52f_1_0.frm' (errno: 13)
#出现这种问题,解决方法如下:
[root@localhost mysql]# chmod -R 777 /tmp
再重新初始化
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MySQL system tables...
OK
Filling help tables...
OK      //出现两个ok就是初始化成功

(8)定义配置文件

[root@localhost support-files]# ls     //配置文件模板
binary-configure   my-huge.cnf             mysqld_multi.server
config.huge.ini    my-innodb-heavy-4G.cnf  mysql-log-rotate
config.medium.ini  my-large.cnf            mysql.server
config.small.ini   my-medium.cnf           ndb-config-2-node.ini
magic              my-small.cnf
[root@localhost support-files]# cp my-huge.cnf /etc/my.cnf
cp: overwrite `/etc/my.cnf'? y
//mysql配置文件默认放在/etc/my.cnf

(9)修改配置文件

[root@localhost support-files]# vim /etc/my.cnf
key_buffer_size = 128M  //128M实验环境足够用了
#log-bin=mysql-bin  //主生成二进制文件,从读取二进制文件

(10)定义启动脚本并添加到服务

[root@localhost support-files]# cp mysql.server /etc/init.d/mysqld
//拷贝文件到/etc/init.d目录下
[root@localhost support-files]# chmod 755 /etc/init.d/mysqld  //修改权限
[root@localhost support-files]# chkconfig --add mysqld   //添加服务
[root@localhost support-files]# chkconfig --list |grep mysqld
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
//查看服务,0-6表示运行级别,off和on表示所在的运行级别此服务的开启还是关闭。

(11)指定datadir

[root@localhost support-files]# vim /etc/init.d/mysqld
datadir=/data/mysql

(12)启动mysql

[root@localhost mysql]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!   //启动成功

(13)mysql日志

[root@localhost mysql]# cd /data/mysql/
[root@localhost mysql]# ls
ibdata1      ib_logfile1                localhost.localdomain.pid  test
ib_logfile0  localhost.localdomain.err  mysql

3、安装Apache

(1)下载httpd-2.2.16.tar.gz源码包

[root@localhost src]# wget http://syslab.comsenz.com/downloads/linux/httpd-2.2.16.tar.gz

(2)校验源码包

[root@localhost src]# md5sum httpd-2.2.16.tar.gz
7f33f2c8b213ad758c009ae46d2795ed  httpd-2.2.16.tar.gz

(3)解压源码包

[root@localhost src]# tar zxvf httpd-2.2.16.tar.gz

(4)编译安装

[root@localhost httpd-2.2.16]# ./configure --prefix=/usr/local/apache2 --with-included-apr --enable-deflate=shared --enable-expires=shared --enable-rewrite=shared --with-pcre
#报错1;checking for zlib location... not found
checking whether to enable mod_deflate... configure: error: mod_deflate has been            requested but can not be built due to prerequisite failures
#解决方法:
[root@localhost httpd-2.2.16]# yum list |grep zlib
zlib.i686                                  1.2.3-29.el6                  @anaconda-CentOS-201303020136.i386/6.4
jzlib.i686                                 1.0.7-7.5.el6                 base
jzlib-demo.i686                            1.0.7-7.5.el6                 base
jzlib-javadoc.i686                         1.0.7-7.5.el6                 base
zlib-devel.i686                            1.2.3-29.el6                  base
zlib-static.i686                           1.2.3-29.el6                  base
[root@localhost httpd-2.2.16]# yum -y install zlib-devel
[root@localhost httpd-2.2.16]# ./configure --prefix=/usr/local/apache2 --with-included-apr --enable-deflate=shared --enable-expires=shared --enable-rewrite=shared --with-pcre 
//再配置
[root@localhost httpd-2.2.16]# make && make install    //编译安装

最好去官网下载指定的包

(5)启动apache

[root@localhost ~]# /usr/local/apache2/bin/apachectl start
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
解决方法:
[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf
ServerName localhost
[root@localhost ~]# netstat -anpt |grep httpd
tcp        0      0 :::80                       :::*                        LISTEN      7521/httpd

4、安装php

(1)下源码包php-5.3.27.tar.gz

[root@client src]# wget http://am1.php.net/distributions/php-5.3.27.tar.gz

(2)解压php

[root@localhost src]# tar zxvf php-5.3.27.tar.gz

(3)配置php

[root@localhost src]# cd php-5.3.27
[root@localhost php-5.3.27]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif --disable-ipv6

报错1:

configure: error: xml2-config not found. Please check your libxml2 installation.

解决1:

[root@localhost php-5.3.27]# yum list |grep libxml2
libxml2.i686                               2.7.6-8.el6_3.4               @anaconda-CentOS-201303020136.i386/6.4
libxml2.i686                               2.7.6-14.el6                  base
libxml2-devel.i686                         2.7.6-14.el6                  base
libxml2-python.i686                        2.7.6-14.el6                  base
libxml2-static.i686                        2.7.6-14.el6                  base
[root@localhost php-5.3.27]# yum install -y libxml2-devel

报错2:

configure: error: Cannot find OpenSSL's <evp.h>

解决2:

[root@localhost php-5.3.27]# yum list |grep openssl
openssl.i686                               1.0.1e-16.el6_5.7             @updates
krb5-pkinit-openssl.i686                   1.10.3-15.el6_5.1             updates
openssl-devel.i686                         1.0.1e-16.el6_5.7             updates
openssl-perl.i686                          1.0.1e-16.el6_5.7             updates
openssl-static.i686                        1.0.1e-16.el6_5.7             updates
openssl098e.i686                           0.9.8e-17.el6.centos.2        base
[root@localhost php-5.3.27]# yum install -y openssl-devel

报错3:

checking for BZip2 in default path... not found
configure: error: Please reinstall the BZip2 distribution

解决3:

[root@localhost php-5.3.27]# yum list |grep bzip2
bzip2.i686                                 1.0.5-7.el6_0                 @anaconda-CentOS-201303020136.i386/6.4
bzip2-libs.i686                            1.0.5-7.el6_0                 @anaconda-CentOS-201303020136.i386/6.4
bzip2-devel.i686                           1.0.5-7.el6_0                 base
[root@localhost php-5.3.27]# yum install -y bzip2-devel

报错4:

configure: error: jpeglib.h not found.

解决4:

[root@localhost php-5.3.27]# yum list |grep jpeg
libjpeg-turbo.i686                         1.2.1-3.el6_5                 updates
libjpeg-turbo-devel.i686                   1.2.1-3.el6_5                 updates
libjpeg-turbo-static.i686                  1.2.1-3.el6_5                 updates
openjpeg.i686                              1.3-10.el6_5                  updates
openjpeg-devel.i686                        1.3-10.el6_5                  updates
openjpeg-libs.i686                         1.3-10.el6_5                  updates
[root@localhost php-5.3.27]# yum install -y libjpeg-turbo-devel

报错5:

configure: error: png.h not found.

解决5:

[root@localhost php-5.3.27]# yum list |grep png
dvipng.i686                               1.11-3.2.el6                  base
libpng.i686                               2:1.2.49-1.el6_2              base
libpng-devel.i686                         2:1.2.49-1.el6_2              base
libpng-static.i686                        2:1.2.49-1.el6_2              base
[root@localhost php-5.3.27]# yum install -y libpng-devel

报错6:

configure: error: freetype.h not found.

解决6:

[root@localhost php-5.3.27]# yum list |grep freetype
freetype.i686                             2.3.11-14.el6_3.1             base
freetype-demos.i686                       2.3.11-14.el6_3.1             base
freetype-devel.i686                       2.3.11-14.el6_3.1             base
[root@localhost php-5.3.27]# yum install -y freetype-devel

报错7:

configure: error: mcrypt.h not found. Please reinstall libmcrypt.

解决7:

[root@localhost php-5.3.27]# rpm -ivh "http://www.lishiming.net/data/p_w_upload/forum/month_1211/epel-release-6-7.noarch.rpm"
Retrieving http://www.lishiming.net/data/p_w_upload/forum/month_1211/epel-release-6-7.noarch.rpm
warning: /var/tmp/rpm-tmp.CBYMdI: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]
[root@localhost php-5.3.27]# yum install -y  libmcrypt-devel

(4)编译安装

[root@localhost php-5.3.27]# make && make install

5、apache支持php配置

(1)添加配置

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>
    AddType application/x-httpd-php .php   //支持php

(2)检测配置文件是否有错

[root@localhost ~]# /usr/local/apache2/bin/apachectl -t
Syntax OK

(3)编辑测试页

[root@localhost ~]# vim /usr/local/apache2/htdocs/1.php
<?php
 Echo "wellcome to php !";
?>

(4)测试

[root@localhost php-5.3.27]# curl localhost/1.php
wellcome to php ![root@localhost php-5.3.27]#

扩展学习

1、什么是扩展模块

  apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象,使之可以用由mod_so提供的LoadModule指令在运行时加载到Apache服务器中。在Apache的配置文件中西东增加一行配置文件。

 编译二进制文件的时候,所加载的一些模块,所有的可执行文件都会依赖一些包或库,这些库是由一些小模块。加载库的两种形式,一是所有模块全部直接编译进二进制文件中,但文件会越来越大,耗费资源。二是暂时编译进去一些常用的模块到二进制文件中,不常用的以扩展模块的形式加载。需要指定扩展模块的路径。

2、添加扩展模块

(1)Apache安装扩展模块 - mod_status

[root@localhost generators]# /usr/local/apache2/bin/apxs -i -a -c -n statuts mod_status.c 
[root@localhost generators]# vim /usr/local/apache2/conf/httpd.conf
LoadModule deflate_module modules/mod_deflate.so
LoadModule expires_module modules/mod_expires.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule php5_module        modules/libphp5.so
LoadModule statuts_module     modules/mod_status.so  #自动添加

(2)PHP扩展模块安装 - memcache

[root@localhost src]# wget http://www.lishiming.net/data/p_w_upload/forum/memcache-2.2.3.tgz
[root@localhost src]# tar -zxvf memcache-2.2.3.tgz
[root@localhost src]# cd memcache-2.2.3
[root@localhost memcache-2.2.3]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
[root@localhost memcache-2.2.3]# yum install m4
[root@localhost memcache-2.2.3]# yum install autoconf
[root@localhost memcache-2.2.3]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
[root@localhost memcache-2.2.3]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@localhost memcache-2.2.3]# make && make install
[root@localhost memcache-2.2.3]# cp /usr/local/src/php-5.3.27/php.ini-development /usr/local/php/etc/php.ini
[root@localhost memcache-2.2.3]# vim /usr/local/php/etc/php.ini
extension = memcache.so

3、常用技巧

(1)查看Apache编译参数

[root@localhost etc]# cat /usr/local/apache2/build/config.nice
#! /bin/sh
#
# Created by configure
"./configure" \
"--prefix=/usr/local/apache2" \
"--with-included-apr" \
"--enable-deflate=shared" \
"--enable-expires=shared" \
"--enable-rewrite=shared" \
"--with-pcre" \
"$@"

(2)查看Apache加载的

[root@localhost modules]# /usr/local/apache2/bin/apachectl -M

(3)重新加载配置文件

[root@localhost modules]# /usr/local/apache2/bin/apachectl graceful
httpd not running, trying to start

(4)查看mysql的配置参数

[root@localhost modules]# cat /usr/local/mysql/bin/mysqlbug|grep configure
# This is set by configure
CONFIGURE_LINE="./configure  '--prefix=/usr/local/mysql' '--localstatedir=/usr/local/mysql/data' '--libexecdir=/usr/local/mysql/bin' '--with-comment=MySQL Community Server (GPL)' '--with-server-suffix=' '--enable-thread-safe-client' '--enable-local-infile' '--enable-assembler' '--with-pic' '--with-fast-mutexes' '--with-client-ldflags=-static' '--with-mysqld-ldflags=-static' '--with-zlib-dir=bundled' '--with-big-tables' '--with-readline' '--with-embedded-server' '--with-partition' '--with-innodb' '--without-ndbcluster' '--with-archive-storage-engine' '--with-blackhole-storage-engine' '--with-csv-storage-engine' '--without-example-storage-engine' '--with-federated-storage-engine' '--with-extra-charsets=complex' 'CC=icc  -static-intel -static-libgcc' 'CFLAGS=-g -O3 -unroll2 -ip -mp -restrict' 'CXX=icpc -static-intel -static-libgcc' 'CXXFLAGS=-g -O3 -unroll2 -ip -mp -restrict'"

(5)查看php编译参数

[root@localhost modules]# /usr/local/php/bin/php -i |head
phpinfo()
PHP Version => 5.3.27
System => Linux localhost.localdomain 2.6.32-358.el6.i686 #1 SMP Thu Feb 21 21:50:49 UTC 2013 i686
Build Date => Apr 16 2014 11:23:29
Configure Command =>  './configure'  '--prefix=/usr/local/php' '--with-apxs2=/usr/local/apache2/bin/apxs' '--with-config-file-path=/usr/local/php/etc' '--with-mysql=/usr/local/mysql' '--with-libxml-dir' '--with-gd' '--with-jpeg-dir' '--with-png-dir' '--with-freetype-dir' '--with-iconv-dir' '--with-zlib-dir' '--with-bz2' '--with-openssl' '--with-mcrypt' '--enable-soap' '--enable-gd-native-ttf' '--enable-mbstring' '--enable-sockets' '--enable-exif' '--disable-ipv6'
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /usr/local/php/etc
Loaded Configuration File => /usr/local/php/etc/php.ini

(6)查看php的加载模块

[root@localhost modules]# /usr/local/php/bin/php -m
[PHP Modules]
bz2
Core
ctype
date
dom
ereg

(7)查看php.ini文件在那里

[root@localhost modules]# /usr/local/php/bin/php -i |grep 'Configuration File'
Configuration File (php.ini) Path => /usr/local/php/etc
Loaded Configuration File => /usr/local/php/etc/php.ini

(8)查看extension_dir路径

[root@localhost modules]# /usr/local/php/bin/php -i |grep 'extension_dir'
PHP Warning:  Unknown: 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 'Asia/Chongqing' for 'CST/8.0/no DST' instead in Unknown on line 0
extension_dir => /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626 => /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626
sqlite3.extension_dir => no value => no value

(9)查看php依赖的库

[root@localhost modules]# ldd /usr/local/php/bin/php
        linux-gate.so.1 =>  (0x00d51000)
        libcrypt.so.1 => /lib/libcrypt.so.1 (0x001a6000)
        librt.so.1 => /lib/librt.so.1 (0x003ec000)