十九、LAMP環境搭建

LAMP 是Linux Apache MySQL PHP的簡寫,其實就是把Apache, MySQL以及PHP安裝在Linux系統上,組成一個環境來運行php的腳本語言。至於什麼是php腳本語言,阿銘不介紹,請自己查資料吧。Apache是最常用的WEB服務軟件,而MySQL是比較小型的數據庫軟件,這兩個軟件以及PHP都可以安裝到windows的機器上。下面阿銘就教你如何構建這個LAMP環境。

安裝MySQL

我們平時安裝MySQL都是源碼包安裝的,但是由於它的編譯需要很長的時間,所以,阿銘建議你安裝二進制免編譯包。你可以到MySQL官方網站去下載 http://dev.mysql.com/downloads/ 具體版本根據你的平臺和需求而定,目前比較常用的爲mysql-5.0/mysql-5.1, 5.5版本雖然已經發布有段日子了,但是貌似用在線上跑服務的還是少數。。下面是安裝步驟(注意,下面的安裝步驟是基於32位操作系統的,如果你的系統爲64位,則需要下載一個64位的包):

  1. 下載mysql到/usr/local/src/

cd /usr/local/src/
wget http://syslab.comsenz.com/downloads/linux/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz
  1. 解壓

[root@localhost src]# tar zxvf /usr/local/src/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz
  1. 把解壓完的數據移動到/usr/local/mysql

[root@localhost src]# mv mysql-5.1.40-linux-i686-icc-glibc23 /usr/local/mysql
  1. 建立mysql用戶

[root@localhost src]# useradd -s /sbin/nologin mysql
  1. 初始化數據庫

[root@localhost src]# cd /usr/local/mysql
[root@localhost mysql]# mkdir -p /data/mysql ; chown -R mysql:mysql /data/mysql
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

--user 定義數據庫的所屬主, --datadir 定義數據庫安裝到哪裏,建議放到大空間的分區上,這個目錄需要自行創建。這一步驟很關鍵,如果你看到兩個 “OK” 說明執行正確,否則請仔細查看錯誤信息,如果你實在解決不了,請把問題發到論壇教程答疑版塊(http://www.aminglinux.com/bbs/forum-40-1.html)阿銘會來幫你解決問題。

  1. 拷貝配置文件

[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf
  1. 拷貝啓動腳本文件並修改其屬性

[root@localhost mysql]# cp support-files/mysql.server  /etc/init.d/mysqld
[root@localhost mysql]# chmod 755 /etc/init.d/mysqld
  1. 修改啓動腳本

[root@localhost mysql]# vim /etc/init.d/mysqld

需要修改的地方有 “datadir=/data/mysql” (前面初始化數據庫時定義的目錄)

  1. 把啓動腳本加入系統服務項,並設定開機啓動,啓動mysql

[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig mysqld on
[root@localhost mysql]# service mysqld start

如果啓動不了,請到 /data/mysql/ 下查看錯誤日誌,這個日誌通常是主機名.err. 檢查mysql是否啓動的命令爲:

[root@localhost mysql]# ps aux |grep mysqld

安裝Apache

同樣apache也需要到官網下載合適的版本,目前使用較多的版本爲2.0或者2.2阿銘建議下載2.2版本。apache官網下載地址: http://www.apache.org/dyn/closer.cgi 你也可以使用阿銘提供的地址下載。

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

解壓:

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

配置編譯參數:

[root@localhost src]# cd httpd-2.2.16
[root@localhost httpd-2.2.16]# ./configure \
--prefix=/usr/local/apache2 \
--with-included-apr \
--enable-so \
--enable-deflate=shared \
--enable-expires=shared \
--enable-rewrite=shared \
--with-pcre

--prefix 指定安裝到哪裏, --enable-so 表示啓用DSO [1] --enable-deflate=shared 表示共享的方式編譯deflate,後面的參數同理。如果這一步你出現了這樣的錯誤:

configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/.

解決辦法:

wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.5.2.tar.gz

wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.5.4.tar.gz

tar zxf apr-1.5.2.tar.gz && cp -fr apr-1.5.2 ./httpd-2.4.9/srclib/apr

tar zxf apr-util-1.5.4.tar.gz && cp -fr apr-util-1.5.4 ./httpd-2.4.9/srclib/apr-util

然後重新執行./configure --prefix=.....


錯誤:

configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

解決辦法:由於缺少pcreftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre2-10.20.tar.gz

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre2-10.20.tar.gz 下載地址,然後編譯安裝


error: mod_deflate has been requested but can not be built due to prerequisite failures

解決辦法是:

yum install -y zlib-devel

爲了避免在make的時候出現錯誤,所以最好是提前先安裝好一些庫文件:

yum install -y pcre pcre-devel apr apr-devel

編譯:

[root@localhost httpd-2.2.16]# make

安裝:

[root@localhost httpd-2.2.16]# make install

以上兩個步驟都可以使用 echo $? 來檢查是否正確執行,否則需要根據錯誤提示去解決問題。

安裝PHP

阿銘寫這本教程時,php當前最新版本爲5.5, 相信大多網站還在跑着5.2甚至更老的版本,其實5.2版本的php很經典也很穩定,因爲阿銘的公司一直在使用5.2版本,但是考慮到版本太老,難免會有些漏洞,所以建議你使用5.3或者5.4版本,php官方下載地址: http://www.php.net/downloads.php

下載php:

[rot@localhost httpd-2.2.16]# cd /usr/local/src
[root@localhost src]# wget http://am1.php.net/distributions/php-5.3.27.tar.gz

解壓:

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

配置編譯參數:

[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

錯誤

jpeglib.h not found.

解決:

#yum install libjpeg-devel


錯誤:

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

解決辦法是:

yum install -y libxml2-devel

還有錯誤:

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

解決辦法是:

yum install -y openssl openssl-devel

錯誤:

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

解決辦法:

yum install -y bzip2 bzip2-devel

錯誤:

configure: error: png.h not found.

解決辦法:

yum install -y libpng libpng-devel

錯誤:

configure: error: freetype.h not found.

解決辦法:

yum install -y freetype freetype-devel

錯誤:

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

解決辦法:

rpm -ivh "http://www.aminglinux.com/bbs/data/p_w_upload/forum/month_1211/epel-release-6-7.noarch.rpm"
yum install -y  libmcrypt-devel

因爲centos6.x 默認的yum源沒有libmcrypt-devel 這個包,只能藉助第三方yum源。

編譯:

[root@localhost php-5.3.27]# make

在這一步,你也許還會遇到諸多錯誤,沒有關係,請仔細查看報錯信息,解決辦法很簡單,就是裝缺少的庫。你可以把錯誤信息複製到google上搜一下,


安裝:

[root@localhost php-5.3.27]# make install

拷貝配置文件:

[root@localhost php-5.3.27]# cp php.ini-production /usr/local/php/etc/php.ini

[root@linux6 php-5.3.27]# /usr/local/php/bin/php -m    查看php模塊

[PHP Modules]

bz2

Core

ctype

date

dom

ereg

exif

fileinfo

filter

gd

hash

iconv

json

libxml

mbstring

mcrypt

mysql

openssl

pcre

PDO


apache結合php

Apache主配置文件爲:/usr/local/apache2/conf/httpd.conf

vim /usr/local/apache2/conf/httpd.conf

找到:

AddType application/x-gzip .gz .tgz

在該行下面添加:

AddType application/x-httpd-php .php

找到:

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

將該行改爲:

<IfModule dir_module>
    DirectoryIndex index.html index.htm index.php
</IfModule>

找到:

#ServerName www.example.com:80

修改爲:

ServerName localhost:80

測試LAMP是否成功

啓動apache之前先檢驗配置文件是否正確:

/usr/local/apache2/bin/apachectl -t

如果有錯誤,請繼續修改httpd.conf, 如果是正確的則顯示爲 “Syntax OK”, 啓動apache的命令爲:

/usr/local/apache2/bin/apachectl start

查看是否啓動:

[root@localhost ~]# netstat -lnp |grep httpd
tcp        0      0 :::80                       :::*   LISTEN      7667/httpd

如果有顯示這行,則啓動了。 也可以使用curl命令簡單測試:

[root@localhost ~]# curl localhost
<html><body><h1>It works!</h1></body></html>

只有顯示這樣才正確。

測試是否正確解析php:

vim /usr/local/apache2/htdocs/1.php

寫入:

<?php
    echo "php解析正常";
?>

保存後,繼續測試:

curl localhost/1.php

看是否能看到如下信息:

[root@localhost ~]# curl localhost/1.php
php解析正常[root@localhost ~]#

只有顯示如阿銘這樣才正確。

初次使用瀏覽器訪問我們的web服務的時候,你可能無法訪問,這是因爲防火牆的緣故。請運行下面的命令:

[root@localhost ~]# iptables -F

這樣就可以清除系統默認的防火牆規則,放行80端口。

LAMP環境是搭建好了,這其實僅僅是安裝上了軟件而已,而具體的配置還是有很多工作要做的呢?也就是說,你雖然搭建出來了環境,但是如果不會配置細節的東西,相當於沒有任何工作經驗,所以還是多配置配置apache或者php吧,具體參考資料可以到阿銘論壇的相應版本中找到,大多帖子爲阿銘工作中所配置過的,阿銘真心希望你能夠按照阿銘的帖子配置一下,這樣對你有很大的好處。論壇地址:http://www.aminglinux.com/bbs/forum.php

擴展學習一下: http://www.aminglinux.com/bbs/thread-5441-1-1.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章