编译安装apache、mysql、 PHP

一,说明

软件源代码包存放位置:/usr/local/src
源码包编译安装位置:/usr/local/软件名字

二,修改源(非必须)

1、进入存放源配置的文件夹

cd /etc/yum.repos.d

2、备份默认源

mv ./CentOS-Base.repo ./CentOS-Base.repo.bak

3、使用wget下载163的源

wget http://mirrors.163.com/.help/CentOS-Base-163.repo

4、把下载下来的文件CentOS-Base-163.repo设置为默认源

mv CentOS-Base-163.repo CentOS-Base.repo

三,关闭SELINUX(防止编译报错)

临时关闭

setenforce 0 

永久关闭

vi /etc/selinux/config
SELINUX=enforcing #注释掉
SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq 保存,关闭
shutdown -r now #重启系统

四,准备软件

1、下载apache

wget  apache.dataguru.cn/httpd/httpd-2.4.7.tar.gz

2、下载mysql

wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.35.tar.gz

3、下载php 5.5.8

wget cn2.php.net/get/php-5.5.8.tar.gz/from/this/mirror

4、下载cmake(MySQL编译工具)

wget  http://www.cmake.org/files/v2.8/cmake-2.8.12.1.tar.gz

5、下载apr(Apache库文件)

wget mirror.bit.edu.cn/apache/apr/apr-1.5.0.tar.gz

6、下载apr-util(Apache库文件)

wget mirror.bit.edu.cn/apache/apr/apr-util-1.5.3.tar.gz

7、下载libmcrypt(PHP扩展)

wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz

8、下载mcrypt(PHP 扩展)

wget http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz

9、下载mhash(PHP 扩展)

wget http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz

10、下载pcre(PHP 扩展)

wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.38/pcre-8.38.tar.gz

五、编译工具及库文件(使用CentOS yum命令安装)

yum install make apr* autoconf automake gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch

六、安装软件

1、安装cmake

cd /usr/local/src
tar zxvf cmake-2.8.7.tar.gz
cd cmake-2.8.7
./configure
make #编译
make install #安装

2、安装apr

首先卸载旧版本

yum remove apr-util-devel apr apr-util-mysql apr-docs apr-devel apr-util apr-util-docs
cd /usr/local/src
tar zxvf apr-1.5.0.tar.gz
cd apr-1.5.0
./configure --prefix=/usr/local/apr
make
make install

3、安装apr-util

tar zxvf apr-util-1.5.3.tar.gz
cd apr-util-1.5.3
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config
make
make install

4、安装mysql

新增mysql用户组

groupadd mysql  

新增mysql用户

useradd -r -g mysql mysql  

新建mysql安装目录

mkdir -p /usr/local/mysql  

新建mysql数据库数据文件目录

mkdir -p /data/mysql

设置MySQL数据库目录权限

chown -R mysql:mysql /data/mysql 

编译安装mysql

tar -zxvf mysql-5.6.16.tar.gz  
cd mysql-5.6.16
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data/mysql -DMYSQL-USER=mysql -DSYSCONFDIR=/etc -DDOWNLOAD_BOOST=1
make && make install

初始化MySQL配置

ln -s /usr/local/mysql/bin/* /usr/bin/
chmod +x /usr/local/mysql/scripts/mysql_install_db

修改配置文件

cp support-files/my-default.cnf /etc/my.cnf
vim /etc/my.cnf
在[mysqld]节点中添加:
datadir = /data/mysql
log-error = /data/mysql/error.log
pid-file = /data/mysql/mysql.pid
user = mysql
/usr/local/mysql/bin/mysql_install_db \
--defaults-file=/etc/my.cnf  \
--basedir=/usr/local/mysql/ \
--datadir=/data/mysql \
--user=mysql

配置开机自动启动

cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 2345 mysqld on
chkconfig --list mysqld #查看是否已应用上

启动MySQL

service mysqld start  # 或 /etc/init.d/mysqld start
/usr/local/mysql/bin/mysqladmin -u root -p password "123456"

安装相关扩展
安装 libmcrypt
安装 mhash
安装 mcrypt 若报 configure: error: * libmcrypt was not found
运行 运行 export LD_LIBRARY_PATH=/usr/local/lib: LD_LIBRARY_PATH 再 编译安装

5、编译安装apache

tar xf httpd-2.4.16.tar.bz2
cd httpd-2.4.16
./configure \
--prefix=/usr/local/apache \
--sysconfdir=/etc/httpd \
--enable-so --enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib --with-pcre \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=event \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util
make && make install

初始化配置Apache

cp /usr/local/apache/bin/apachectl /etc/init.d/apache
groupadd apache #添加apache用户组及用户
useradd -g apache -s /usr/sbin/nologin apache
chown -R apache:apache /usr/local/apache
chmod +x /etc/init.d/apache
chkconfig --add apache  #增加执行权限
chkconfig --level 2345 apache on  #设置开机启动
chkconfig --list apache    #查看是否设置成功

将apachectl 加入bin

ln -s /usr/local/apache/bin/apachectl  /usr/bin/apachectl

6、编译安装PHP

编译前安装扩展库

yum -y install libxml2
yum -y install libxml2-devel
yum -y install openssl
yum -y install openssl-devel
yum -y install curl
yum -y install curl-devel
yum -y install libjpeg
yum -y install libjpeg-devel
yum -y install libpng
yum -y install libpng-devel
yum -y install freetype
yum -y install freetype-devel
yum -y install pcre
yum -y install pcre-devel
yum -y install libxslt
yum -y install libxslt-devel
yum -y install bzip2
yum -y install bzip2-devel
tar -xzvf ./php-5.6.13.tar.gz
cd php-5.6.13
./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-mcrypt --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  --enable-maintainer-zts --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip
make && make install

复制配置文件php.ini

cp /usr/local/src/php-5.6.13/php.ini-production /etc/php.ini

整合Apache

vim /etc/httpd/httpd.conf
添加 AddType application/x-httpd-php .php
修改 DirectoryIndex index.php index.html

重启Apache

service httpd restart  # 或 /etc/init.d/httpd restart

添加测试页面

cat /usr/local/apache/htdocs/index.php


       //phpinfo();

$conn = mysql_connect('localhost','root','') or die('connect fail') ;
mysql_select_db("test",$conn);
$res = mysql_query("insert into user(name)values('luoxianjie')",$conn);    
var_dump($res);       
die; 
?>

七、常见问题(*)

1,mysql安装后启动失败

查看/data/mysql所处用户组是否为mysql,查看mysql.sock文件是否存在,并且用读写权限

2,apache安装后启动不了

查看配置文件是否有错,查看http.conf 里面是否存在serverName 如不存在则添加为http://localhost:80

3,apache外网无法访问

关闭防火墙,用ps aux | grep fillwalld 查看进程是否开启

4,mysql 客户端可以连接,但PHP连接时报sock文件不存在

用phpinfo 查看mysql 连接sock 默认存储位置是否为我们设置的 /data/mysql/mysql.sock 若否
修改mysql_config 中sock地址为/data/mysql/mysql.sock
打开/etc/php.ini 搜索mysql.default_sock 将其修改为 /data/mysql/mysql.sock

5, 解决:error: Cannot find libmysqlclient_r under /usr/local/mysql.

cd /usr/local/mysql/lib/mysql/
ln -s libmysqlclient.so.15.0.0 libmysqlclient_r.so
路径问题,一般编译前添加环境变量就可以解决,例如这里只要:
export MYSQL_LIB_DIR=/usr/lib64/mysql
这里的/usr/lib64/mysql就是那个提示缺少的so文件所在文件夹

你可能感兴趣的:(lnmp)