centos 源码编译安装lnmp(linux + nginx1.5.5 + mysql5.5.21 + php5.3.27) 详细版

作者:袖梨 2022-06-30

一:安装相关依赖组件

先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

(yum安装过后基本的组件已安好,以下编译安装可以安装最新版的组件)

1:安装zlib

zlib 官网:http://zlib.net


wget http://zlib.net/zlib-1.2.8.tar.gz
tar -xvzf zlib-1.2.8.tar.gz
cd zlib-1.2.8.tar.gz
./configure
make
make install

2:安装freetype

wget http://download.savannah.gnu.org/releases/freetype/freetype-2.5.0.1.tar.gz
cd /usr/local/src
tar -zxvf freetype-2.5.0.1.tar.gz
cd freetype-2.5.0.1
./configure --prefix=/usr/local/freetype
make
make install
make 的时候可能出现错误:
/usr/include/libpng12/pngconf.h:336: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
/usr/include/libpng12/pngconf.h:337: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘include’
make: *** [/usr/local/src/freetype-2.5.0.1/objs/sfnt.lo] Error 1

解决办法:

./configure –prefix=/usr/local/freetype –without-png(加上这个选项就行了)

3:安装libpng


wget http://sourceforge.net/projects/libpng/files/libpng12/older-releases/1.2.31/libpng-1.2.31.tar.gz/download
cd /usr/local/src
tar -zxvf libpng-1.2.31.tar.gz
cd libpng-1.2.31
./configure
make
make install


4:安装libxml2


wget http://xmlsoft.org/sources/old/libxml2-2.7.1.tar.gz
cd /usr/local/src
tar -xzvf libxml2-2.7.1.tar.gz
cd libxml2-2.7.1
./configure --prefix=/usr/local/libxml2 --without-zlib (记得加上后面这句,不然会出错)
make
make install

5:安装libmcypt


wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
cd /usr/local/src
tar zxvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure --prefix=/usr/local/libmcrypt
mak
make install

6:安装gd2(php的gd库,压缩和处理图片会用到)


wget http://ring.u-toyama.ac.jp/archives/graphics/gd/gd-2.0.35.tar.gz
cd /usr/local/src
tar -zxvf gd-2.0.35.tar.gz
mkdir -p /usr/local/gd
cd gd-2.0.35
./configure --prefix=/usr/local/gd --enable-m4_pattern_allow --with-jpeg --with-png --with-zlib --with-freetype=/usr/local/freetype --with-libmcrypt=/usr/local/libmcrypt
make
make instal
(–with-jpeg:使其支持jpeg图片处理;–with-png:支持png图片处理)
编译过程可能出现错误:

就先编译安装下gettext

wget http://ftp.gnu.org/pub/gnu/gettext/gettext-0.18.3.1.tar.gz
tar xzf gettext-0.18.3.1.tar.gz
cd gettext-0.18.3.1
./configure
make
make install
注意:gd库版本如果是gd-2.0.35以上,则需要 ./configure –enable-m4_pattern_allow或者编译安装 gettext组件后再重新编译gd

7:安装openssl

wget https://www.openssl.org/source/openssl-1.0.1c.tar.gz  #openssldir 是配置文件目录,建议安装两次,shared 作用是生成动态连接库。
tar -zxf openssl-1.0.1c.tar.gz
cd openssl-1.0.1c/
./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl/ssl
make
make install
./config shared --prefix=/usr/local/openssl --openssldir=/usr/local/openssl/ssl
make clean
make
make install

8:安装curl

wget http://curl.haxx.se/download/curl-7.17.1.tar.gz
tar -zxvf curl-7.17.1.tar.gz
cd curl-7.17.1
./configure --prefix=/usr/local/curl
make
make install
如果出现错误:
curl.so -lz -lrt -Wl,-rpath -Wl,/usr/local/curl/lib
../lib/.libs/libcurl.so: undefined reference to `ERR_remove_thread_state’
collect2: ld returned 1 exit status
make[2]: *** [curl] Error 1
make[2]: Leaving directory `/usr/local/src/curl-7.32.0/src’
make[1]: *** [all] Error 2
make[1]: Leaving directory `/usr/local/src/curl-7.32.0/src’
make: *** [all-recursive] Error 1
建议安装旧点的版本,特别是如果openssl的版本是1.0以下

二:安装nginx需要的组件

1:安装pcre

wget http://sourceforge.net/projects/pcre/files/pcre/8.33/pcre-8.33.tar.gz/download
tar -zxvf pcre-8.33.tar.gz
cd pcre-8.33
./configure --enable-utf8
make
make install

三:安装nginx

wget http://nginx.org/download/nginx-1.5.5.tar.gz
tar xf nginx-1.5.5.tar.gz
cd nginx-1.5.5
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/local/openssl --with-pcre=/usr/local/src/pcre-8.33
#注意:--with-pcre=/usr/local/src/pcre-8.31指向的是源码包解压的路径,而不是安装的路径,否则会报错
make
make install

添加nginx开机启动脚本:


#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
# By www.osyunwei.com
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=([^ ]*).*/1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
#configtest || return $?
stop
sleep 1
start
}

reload() {
#configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
#=======================================================
wq!#保存退出
chmod 775 /etc/rc.d/init.d/nginx #赋予文件执行权限
chkconfig nginx on #设置开机启动
/etc/rc.d/init.d/nginx restart
service nginx restart

四:安装mysql

mysql官网:http://dev.mysql.com/downloads/ 下载源码包


groupadd mysql #添加mysql组
useradd -g mysql mysql -s /bin/false #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
mkdir -p /usr/local/mysql #创建MySQL安装目录
mkdir -p /usr/local/mysql/data #创建MySQL数据存放目录
tar zxvf mysql-5.5.21.tar.gz
cd mysql-5.5.21
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_TCP_PORT=3306 -DWITH_EXTRA_CHARSETS:STRING=utf8,gbk -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DWITH_READLINE=1 -DWITH_SSL=yes
#(有时候会出现找不到/usr/local/mysql/data 等警告,不管他,直接继续编译)
make
make install
cd /usr/local/mysql
cp ./support-files/my-huge.cnf /etc/my.cnf #提示文件存在的话直接覆盖
vim /etc/my.cnf #在 [mysqld] 部分增加
datadir =/usr/local/mysql/data #数据库存放路径
./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql #生成mysql系统数据库
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld #把Mysql加入系统启动
chmod 755 /etc/init.d/mysqld #增加执行权限
chkconfig mysqld on #加入开机启动
vim /etc/rc.d/init.d/mysqld #编辑
basedir = /usr/local/mysql #MySQL程序安装路径
datadir = /usr/lcoal/mysql/data #MySQl数据库存放目录
service mysqld start #启动
/usr/local/mysql/bin/mysql -u root –p # 登录mysql

#安装成功后进行root用户密码修改:
/usr/local/mysql/bin/mysqladmin -u root -p password "新密码" #回车后提示输入旧密码,输入成功后密码修稿成功
/etc/init.d/mysqld restart #重启mysql,安装成功!


五:安装php


wget http://cn2.php.net/get/php-5.3.27.tar.gz/from/ar2.php.net/mirror
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd=/usr/local/gd  --with-png-dir=/usr/lib --with-jpeg-dir=/usr/local/jpeg9 --with-freetype-dir=/usr/local/freetype --with-iconv --with-zlib --enable-xml --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl=/usr/local/openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt=/usr/local/libmcrypt --with-curl=/usr/local/curl --enable-ctype
make
make install
(–with-jpeg-dir 使用yum安装的默认版本)

 

cp php.ini-production /usr/local/php/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid  #取消一下行的注释:
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on

service php-fpm start #启动php-fpn
ps aux|grep php-fpm ##检查是否已经正常启动
netstat -ant|grep :9000 #查看端口是否监听
configure配置的时候可能出现的问题:
checking for cURL in default path… not found
configure: error: Please reinstall the libcurl distribution -
因为:curl的dev包没有安装
解决办法:yum -y install curl-devel

make编译的时候可能出现的问题:
virtual memory exhausted: Cannot allocate memory
原因:编译过程是一个内存消耗较大的动作,内存吃紧,无法分配
解决办法:配置项加上 –disable-fileinfo这个参数,重新编译

六:配置nginx支持php


vim /usr/local/nginx/conf/nginx.conf
user www www; #首行user去掉注释,修改Nginx运行组为www www
index index.php index.html index.htm; #找到这行,添加index.php
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#取消FastCGI server部分location的注释,注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径
vim /usr/local/php/etc/php-fpm.conf #同样把user ,group 改为www www
#重启nginx:

 

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid` //重启php,www www的用户组和用户才能生效

vim /usr/local/nginx/html/index.php #创建测试的php文件

浏览器输入主机ip地址,成功显示出phpinfo信息

相关文章

精彩推荐