# Linux環境下LAMP環境編譯安裝
[TOC]
>[info]
搭建LAMP環境時需要按照一定順序安裝,按照Apache->MySQL ->PHP的順序,其中在安裝php之前需要安裝php5需要的庫文件libxml2、gd2等。gd2庫是為了支持gif/png/jpeg圖片格式,因此安裝gd2之前還要安裝zlib、libpng、freetype、jpegsrc等文件。
**安裝前準備**
## gcc編譯器
### 安裝gcc編譯器
`yum install gcc gcc-c++ -y`
### 檢查Linux系統是否安裝了gcc編譯器
`#gcc --version`
### 檢查是否安裝和卸載
~~~
# rpm -qa | grep -i httpd //檢查是否安裝apache
# service httpd stop //停止apache服務
# rpm -e httpd-xxx --nodeps //卸載apache
對于php、mysql等組件的檢查和卸載類似。
~~~
> **下載各種安裝包文件**
httpd-2.4.16.tar.gz http://httpd.apache.org/download.cgi
mysql-5.0.41.tar.gz http://dev.mysql.com/downloads/
php-5.5.11.tar.gz http://mirrors.sohu.com/php/
libxml2-2.7.2.tar.gz ftp://xmlsoft.org/libxml2/
libmcrypt-5.5.8.tar.gz http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/
gd-2.0.33.tar.gz http://www.boutell.com/gd/http/gd-2.0.33.tar.gz
zlib-1.2.8.tar.gz http://www.zlib.net
autoconf-2.6.9.tar.gz http://ftp.gnu.org/gnu/autoconf/
freetype-2.4.9.tar.gz http://download.savannah.gnu.org/releases/freetype/
libpng-1.5.23.tar.gz ftp://ftp.simplesystems.org/pub/libpng/png/src/
jpegsrc.v8b.tar.gz http://www.ijg.org/files/
apr-1.5.2.tar.gz http://mirrors.cnnic.cn/apache//apr/apr-1.5.2.tar.gz
apr-util-1.5.3.tar.gz http://archive.apache.org/dist/apr/apr-util-1.5.3.tar.gz
pcre-8.35.tar.gz http://exim.mirror.fr/pcre/pcre-8.35.tar.gz
# Apache安裝
## yum方式安裝apche
~~~
[root@localhost.localdomain /usr/local/src]
# yum install httpd -y
[root@localhost.localdomain /usr/local/src]
# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built: Aug 24 2015 17:52:49
[root@localhost.localdomain /usr/local/src]
# chkconfig --list |grep httpd
httpd 0:關閉 1:關閉 2:關閉 3:關閉 4:關閉 5:關閉 6:關閉
[root@localhost.localdomain /usr/local/src]
# chkconfig httpd on # 將apache加入自啟動
[root@localhost.localdomain /usr/local/src]
# chkconfig --list |grep httpd
httpd 0:關閉 1:關閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關閉
~~~
**啟動apache如下錯誤:**
~~~
[root@localhost.localdomain /usr/local/src]
# service httpd start
正在啟動 httpd:httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[確定]
~~~
**解決辦法:**
~~~
[root@localhost.localdomain /usr/local/src]
# vim /etc/httpd/conf/httpd.conf
ServerName localhost:80 # 指定ServerName
~~~
## 使用源代碼包安裝apache
### 1. 安裝libxml2
~~~
tar xf libxml2-2.7.2.tar.gz && cd libxml2-2.2.7
./configure --prefix=/usr/local/libxml2
make && make install
~~~
> 安裝完成后會在/usr/local/libxml2文件夾下出現bin、include、lib、man、share這幾個文件夾。
* * * * *
### 2. 安裝libmcrypt
~~~
tar xf libmcrypt-2.5.8.tar.gz && cd libmcrypt-2.5.8
./configure --prefix=/usr/local/libmcrypt
make && make install
~~~
> 安裝完成后會在`/usr/local/libmcrypt`文件夾下出現bin、include、lib、man、share這幾個文件夾。
然后 我們稍后在安裝php的時候可以指定配置項 `--with-mcrypt-dir=/usr/local/libmcrypt` 來指定libmcrypt庫的位置。
* * * * *
### 3. 安裝zlib
~~~
tar xf zlib-1.2.8.tar.gz && cd zlib-1.2.8
./configure --prefix=/usr/local/zlib
make && make install
~~~
> 安裝完成后會在`/usr/local/zlib`文件夾下出現include、lib、share這幾個文件夾。然后 在安裝php的時候可以指定配置項 `--with-zlib-dir=/usr/local/zlib` 來指定zlib庫文件的位置。
* * * * *
### 4. 安裝libpng
~~~
tar xf libpng-1.5.23.tar.gz && cd libpng-1.5.23
./configure --prefix=/usr/local/libpng
~~~
>[danger] 此處執行的時候遇到“configure: error : zlib not installed”。
解決辦法如下:
~~~
cd /usr/local/src/zlib-1.2.8
make clean
./configure
make && make install
~~~
然后`cd /usr/local/src/libpng-1.5.23`重新回到libpng源目錄下,重新`./configure --prefix=/usr/local/libpng`和安裝。
安裝完成后,會在`/usr/local/libpng`目錄下有 bin、include、lib、share四個文件夾。 在安裝php的時候可以指定配置項 `--with-png=/usr/local/libpng` 來指定libpng庫文件的位置。
* * * * *
### 5. 安裝jpeg([常見錯誤](http://www.phpiii.com/thread-544-1-1.html))
~~~
tar xf jpegsrc.v8b.tar.gz && cd jpeg-8b/
./configure --prefix=/usr/local/jpeg/ --enable-shared --enable-static
make && make install
~~~
> 上述執行完畢之后,在安裝php的時候可以指定配置項 `--with-jpeg=/usr/local/jpeg` 來指定jpeg庫文件的位置。
* * * * *
### 6. 安裝freetype
~~~
tar xf freetype-2.4.9.tar.gz && cd freetype-2.4.9
./configure --prefix=/usr/local/freetype
make && make install
~~~
> 安裝成功將會在`/usr/local/freetype`目錄下存在bin,include,lib和share四個目錄。并在 安裝GD2庫時, 通過configure命令選項中加上`--with-freetype=/usr/local/freetype/`選項,指定freetype庫文件的位置。
* * * * *
### 7. 安裝autoconf
~~~
tar xf autoconf-2.69.tar.gz && cd autoconf-2.69
./configure
make && make install
~~~
直接默認安裝即可。
* * * * *
### 8. 安裝GD2庫
~~~
tar xf gd-2.0.33.tar.gz && cd gd-2.0.33
./configure \
--prefix=/usr/local/gd \
--with-zlib=/usr/local/zlib \
--with-jpeg=/usr/local/jpeg \
--with-png=/usr/local/libpng/ \
--with-freetype=/usr/local/freetype/
//使用前面安裝的zlib、jpeg、png和freetype庫文件位置
make && make install
~~~
>[danger] 安裝過程中出現如下錯誤
~~~
gd_png.c:741: error: expected ')' before 'gdMalloc'
gd_png.c:798: error: expected ')' before 'gdMalloc'
make[2]: *** [gd_png.lo] 錯誤 1
make[2]: Leaving directory `/usr/local/src/gd-2.0.33'
make[1]: *** [all-recursive] 錯誤 1
make[1]: Leaving directory `/usr/local/src/gd-2.0.33'
make: *** [all] 錯誤 2
~~~
**解決方案為:**
>[success] `vi /usr/local/src/gd-2.0.33/gd_png.c`
大概在文件15行處找到`#include "png.h"`改成`#include "/usr/local/libpng/include/png.h"` 后再執行`make && make install`,在`/usr/local/gd`文件夾下出現 bin 、include、lib文件夾。 在安裝PHP時,通過在configure命令選項中加上`--with-gd=/usr/local/gd`選項,指定GD庫文件的位置。
* * * * *
### 9. 安裝apr、apr-util和pcre
~~~
tar -zxf apr-1.5.2.tar.gz
cd apr-1.5.2
./configure --prefix=/usr/local/apr-httpd
make && make install
~~~
>[danger] 注意這里安裝的`apr`目錄在安裝`apr-util`時候需要指定。
~~~
tar -zxf apr-util-1.5.3.tar.gz
cd apr-util-1.5.3
./configure \
--prefix=/usr/local/apr-util-httpd \
--with-apr=/usr/local/apr-httpd
make && make install
~~~
~~~
tar xf pcre-8.35.tar.gz && cd pcre-8.35
./configure --prefix=/usr/local/pcre
make && make install
~~~
* * * * *
編譯三部曲: 1. 生成編譯配置文件 2. 進行編譯 3.進行安裝
## 安裝apache
### 獲取apache源代碼
~~~
cd /usr/local/src/ && wget http://mirrors.hust.edu.cn/apache//httpd/httpd-2.4.16.tar.gz
~~~
### 解壓源代碼
`tar xf httpd-2.4.16.tar.gz && cd httpd-2.4.16`
### 配置編譯地圖
~~~
./configure --prefix=/usr/local/apache --enable-mods-shared=all --enable-so --enable-cache --enable-file-cache --enable-mem-cache --enable-disk-cache --enable-rewrite=shared --enable-expires=shared --enable-static-support --disable-cgid --disable-cgi --enable-deflate=shared --sysconfdir=/etc/httpd/ --with-z=/usr/local/zlib/ --with-apr=/usr/local/apr-httpd/ --with-apr-util=/usr/local/apr-util-httpd/ --with-pcre=/usr/local/pcre/
~~~
### 執行安裝
`#make && make install`
### 設置開機自啟動
首先應該在`/etc/httpd/httpd.conf`文件中修改配置文件,特別是要指定**ServerName localhost:80**,其他配置項根據自己需要指定。
然后啟動方式為:
`/usr/local/apache/bin/apachectl start`
`start`換為`stop`或者`restart`可以執行關閉和重啟服務,`netstat - tnl|grep 80` 可以查看是否開啟apache服務。`curl localhost`可以查看頁面內容。
設置自啟動:
`echo "/usr/local/apache/bin/apachectl start" >> /etc/rc.d/rc.local`
## 安裝MySQL
這里以cmake方式安裝
### 安裝依賴包
`yum install cmake ncurses-devel -y`
### 添加用戶
`useradd mysql -s /sbin/nologin`
### 編譯安裝
~~~
tar -zxvf mysql-5.5.29.tar.gz && cd mysql-5.5.29 #默認情況下是安裝在/usr/local/mysql
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
make && make install
~~~
### 改變目錄所有者
`chown -R mysql.mysql /usr/local/mysql`
### 注冊為服務
~~~
cd /usr/local/src/mysql-5.5.29/support-files
#注冊服務
cp mysql.server /etc/rc.d/init.d/mysqld
#使用默認配置文件
cp my-small.cnf /etc/my.cnf
#讓chkconfig管理mysql服務
chkconfig --add mysqld
# 給mysqld授權執行
chmod a+x /etc/init.d/mysqld
#或者使用 setfacl -m u:root:rwx /etc/init.d/mysqld
#開機啟動
chkconfig mysqld on
~~~
### 初始化數據庫
~~~
cd /usr/local/mysql/scripts
./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
~~~
### 啟動MySQL服務
`service mysqld start`
### 將mysql的bin加入到path中
~~~
ln -s /usr/local/mysql/bin/mysql /usr/local/sbin/mysql
cd ~
另外
#把path添加到當前用戶目錄的bashrc中,如果需要全局設定,請修改`/etc/profile` vi .bashrc #加入以下內容
PATH=/usr/local/mysql/bin:$PATH
source /etc/profile
~~~
### 修改MySQL數據庫的root密碼
`/usr/local/mysql/bin/mysqladmin -u root password 'aaaaaa'`
### 改變編碼,防止亂碼
`SHOW VARIABLES LIKE 'character%'`
####修改mysql的my.cnf文件增加如下內容
~~~
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
pid-file=/usr/local/mysql/data/mysqld.pid
[mysql]
default-character-set=utf8
[mysqld_safe]
log-error=/usr/local/mysql/data/mysql_error.err
~~~
### 配置用戶密碼和遠程訪問權限
`GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.%' IDENTIFIED BY '123456' WITH GRANT OPTION;`
* * * * *
## 安裝php
~~~
tar xf php-5.3.28.tar.gz && cd php-5.3.28
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache/bin/apxs --with-zlib-dir=/usr/local/zlib --with-libxml-dir=/usr/local/libxml2 --with-gd=/usr/local/gd --with-freetype-dir=/usr/local/freetype --with-jpeg-dir=/usr/local/jpeg --with-png-dir=/usr/local/libpng --with-mcrypt=/usr/local/libmcrypt --enable-mbstring=all --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/etc/php --with-iconv --enable-sockets --enable-soap
make && make install
~~~
如下即為配置成功后的信息。
~~~
+--------------------------------------------------------------------+
| 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.
~~~
>[danger] **編譯報錯**
php-5.3.28/Zend/zend_language_parser.h:317: error: conflicting types for zendparse
php-5.3.28/Zend/zend_globals_macros.h:35: note: previous declaration of zendparse was here
make: *** [ext/standard/basic_functions.lo] Error 1
**解決方法**
把`zend_language_parser.h`文件(在`php-5.3.28/Zend/zend_language_parser.h`)中**317**行的內容`int zendparse(void *)`與`zend_globals_macros.h` 35行 `int zendparse(void *compiler_globals)`;弄成一致。再進行`make`可以成功。
* * * * *
#### 設置配置文件路徑
安裝完成后,進行配置,進入`/etc/php`文件夾,將源碼包下面的`php.ini-development` 文件復制到 `/usr/local/php/etc/`中,并改名為`php.ini`即可。
#### 修改php.ini
把;date.timezone 前面的分號去掉,改成date.timezone ="PRC"。
* * * * *
## apache與php的整合
> 編譯php我們使用configure命令安裝配置時,使用`--with-apxs2=/usr/local/apache2/bin/apxs`選項以使Apache 2將PHP作為功能模塊使用。
但我們還要修改Apahce配置文件,添加PHP的支持,告 訴Apache將哪些后綴作為PHP解析。
打開Apache的配置文件`vim /etc/httpd/httpd.conf`
* 找到`AddType application/x-gzip .gz .tgz`指令選項,并在其下方添加一條指令**`AddType application/x-httpd-php .php .phtml`**
* 添加對默認首頁文件的支持 `DirectoryIndex index.php index.html index.html.var`
重啟Apache后即可測試。
## apache 安裝
### 安裝依賴
~~~
~~~
### 下載并編譯安裝
~~~
cd /usr/local/src && wget -O httpd-2.4.18.tar.gz http://archive.apache.org/dist/httpd/httpd-2.4.18.tar.gz
./configure --prefix=/application/tools/apache-2.4.18 --enable-mods-shared=all --enable-so --enable-cache --enable-file-cache --enable-mem-cache --enable-disk-cache --enable-rewrite=shared --enable-expires=shared --enable-static-support --disable-cgid --disable-cgi --enable-deflate=shared --sysconfdir=/application/tools/apache-2.4.18/conf/ --with-z=/usr/local/zlib/ --with-apr=/usr/local/apr-httpd/ --with-apr-util=/usr/local/apr-util-httpd/ --with-pcre=/usr/local/pcre/
~~~
- Linux的安裝
- Linux的軟件安裝管理
- Linux零碎的命令
- 了解ssh
- 系統調優及安全設置
- Linux系統中的硬鏈接和軟連接
- Linux文件和目錄的屬性及權限
- 命令總結
- 文件目錄管理命令
- cat
- cd
- cp
- ls
- mkdir
- mv
- rm
- touch
- vi
- 硬件檢測內核shell命令
- echo
- alias
- dd
- diff
- date
- grep(重要)
- head
- rpm
- sed(重要)
- tree
- unalias
- yum
- seq
- su
- less
- more
- tail
- awk(重要)
- pwd
- history
- whoami
- find(重要)
- which
- vimdiff
- sudo
- wget
- ln
- chmod
- chown
- vim常用技巧
- scp
- 工作場景應用總結
- 自動刪除n天前日志
- 刪除一個目錄下的所有文件但保留一個文件
- Linux軟件安裝
- php安裝(apache版本)
- vsftpd安裝
- git安裝
- python安裝
- LNMP安裝
- LAMP安裝I
- LAMP安裝II
- svn安裝
- svn在Linux下的命令操作
- svn鉤子簡介
- svn代碼上傳流程
- Crond介紹
- sersync應用指南
- 其他
- 小結一
- 系統調優重新整理tmp
- linux禁止root用戶直接登錄sshd并修改默認端口