ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 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/ ~~~