天天看點

LAMP配置

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      
[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      
[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      
[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)