Linux(6.4)+Nginx(1.4.1)+Mysql(5.6.12)+Php(5.5.0)源码编译安装 [Shell] 纯文本查看 复制代码 环境介绍
内核2.6.32,系统为CentOS6.4的64为系统,ip地址为192.168.2.103。如下:
[iyunv@ASANGE ~]# uname -r
2.6.32-358.el6.x86_64
[iyunv@ASANGE ~]# cat /etc/redhat-release
CentOS release 6.4 (Final)
[iyunv@ASANGE ~]# ifconfig | grep addr: |awk '{print $2}' |awk -F: '{print $2}'
192.168.2.103
127.0.0.1
配置本地yum源:
创建本地yum源 ,挂载光盘并创建repo文件 :
[iyunv@ASANGE ~]# mount /dev/cdrom /media/cdrom/
[iyunv@ASANGE ~]# vim /etc/yum.repos.d/local.repo //添加如下内容
[LOCAL]
name=local
baseurl=file:///media/cdrom
enabled=1
gpgcheck=0
[iyunv@ASANGE ~]# yum clean all
解决依赖关系:
在编译安装nginx、mysql、和php时依赖的包提前安装:
1
[iyunv@ASANGE ~]# yum -y install gcc pcre pcre-devel gcc-c++ autoconf libxml2 libxml2-devel zlib zlib-devel glibc libjepg libjepg-devel libpng libpng-devel glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers make
简单介绍一下:
GCC(GNU Compiler Collection,GNU编译器套装),是一套由GNU开发的编程语言编译器。
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正规表达式库.
autoconf是一个用于生成可以自动地配置软件源代码包以适应多种Unix类系统的 shell脚本的工具.
libxml2是一个用来解析XML文档的函数库,它用C语言写成, 并且能为多种语言所调用.
zlib是提供资料压缩之用的函式库.
glibc:GNU C 库(GNU C Library,又称为glibc)是一种按照LGPL许可协议发布的,公开源代码的,免费的,方便从网络下载的C的编译程序.
ncurses(new curses)是一个程序库,它提供了API,可以允许程序员编写独立于终端的基于文本的用户界面。
cURL是一个利用URL语法在命令行下工作的文件传输工具,它支持文件上传和下载,所以是综合传输工具.
e2fsprogs(又称为e2fs programs)是用以维护ext2,ext3和ext4文件系统的工具程序集。
OpenSSL是套开放源代码的SSL套件,其函式库是以C语言所写成,实作了基本的传输层资料加密功能。
OpenLDAP是轻型目录访问协议(Lightweight Directory Access Protocol,LDAP)的自由和开源的实现.
make编译工具。
gd:graphic device,图像工具库,gd库是php处理图形的扩展库,gd库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。 在网站上GD库通常用来生成缩略图或者用来对图片加水印或者对网站数据生成报表。(TTP image filter module requires the GD library.)
一、编译安装nginx
1、 为nginx创建用户和组:
1
2
[iyunv@ASANGE ~]# groupadd -r nginx
[iyunv@ASANGE ~]# useradd -r -g nginx -s /bin/false -M nginx
2、下载nginx源码包,并安装:nginx-1.4.1.tar.gz
[iyunv@ASANGE ~]# tar xf nginx-1.4.1.tar.gz
[iyunv@ASANGE ~]# cd nginx-1.4.1
[iyunv@ASANGE nginx-1.4.1]#./configure --user=nginx --group=nginx --prefix=/usr --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-http_realip_module --with-http_gzip_static_module --with-file-aio
[iyunv@ASANGE nginx-1.4.1]# make && make install
3、 为nginx提供启动脚本
[iyunv@ASANGE ~]# vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# 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: /etc/sysconfig/nginx
# pidfile: /var/run/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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/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' -`
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
为启动脚本赋予执行权限、添加至服务管理列表并开机自动启动:
1
2
3
[iyunv@ASANGE ~]# chmod +x /etc/init.d/nginx
[iyunv@ASANGE ~]# chkconfig --add nginx
[iyunv@ASANGE ~]# chkconfig nginx on
4、 启动服务并测试,(注意iptables规则可能阻止访问):
1
2
[iyunv@ASANGE ~]# service nginx start
正在启动 nginx: [确定]
nginx配置成功!
[Shell] 纯文本查看 复制代码
二、安装mysql:
1、首先下载文件:mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz
2、为mysql创建数据目录、用户和组:
1
2
3
[iyunv@ASANGE mysql]# mkdir -pv /mysql/data
[iyunv@ASANGE mysql]# groupadd -r mysql
[iyunv@ASANGE mysql]# useradd -g mysql -r -s /bin/false -M -d /mysql/data mysql
3、 解压并初始化安装
[iyunv@ASANGE ~]# tar vxf mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz
[iyunv@ASANGE ~]# mv mysql-5.6.12-linux-glibc2.5-x86_64 /usr/local/mysql
[iyunv@ASANGE ~]# cd /usr/local/mysql
[iyunv@ASANGE mysql]# ls
bin data include lib mysql-test scripts sql-bench
COPYING docs INSTALL-BINARY man README share support-files
[iyunv@ASANGE mysql]# chown mysql:mysql /mysql/data/
[iyunv@ASANGE mysql]# chown mysql:mysql ./*
[iyunv@ASANGE mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/mysql/data
[iyunv@ASANGE mysql]# chown -R root ./*
安装完成之后,主配置文件为/usr/local/mysql/my.cnf,可根据需要进行文件配置,这里添加如下项:
[iyunv@ASANGE mysql]# mkdir /var/run/mysqld
[iyunv@ASANGE mysql]# vim my.cnf
datadir = /mysql/data
basedir = /usr/local/mysql
port = 3306
socket = /tmp/mysql.sock
pid_file = /var/run/mysqld/mysqld.pid
4、将mysql的头文件连接进系统头文件路径:
1
[iyunv@ASANGE mysql]# ln -sv /usr/local/mysql/include /usr/include/mysql
5、输出mysql库文件:
[iyunv@ASANGE mysql]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[iyunv@ASANGE mysql]# ldconfig
6、添加mysql命令的环境变量
[iyunv@ASANGE mysql]# vim /etc/profile
PATH=$PATH:/usr/local/mysql/bin
[iyunv@ASANGE mysql]# . /etc/profile
7、为mysql添加启动脚本 ,添加进服务列表并开机自动启动
[iyunv@ASANGE mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[iyunv@ASANGE mysql]# chkconfig --add mysqld
[iyunv@ASANGE mysql]# chkconfig mysqld on
至此mysql安装完成,启动mysql服务
1
[iyunv@ASANGE mysql]# service mysqld start
三:编译安装php
php依赖很多库,这里将php依赖的库编译安装进/usr/local/phpextend文件夹里
1
[iyunv@ASANGE ~]# mkdir /usr/local/phpextend
1、编译安装jpegsrc,增强对jpeg格式图片的处理功能。jpegsrc.v9.tar.gz
[iyunv@ASANGE ~]# tar xf jpegsrc.v9.tar.gz
[iyunv@ASANGE ~]# cd jpeg-9/
[iyunv@ASANGE jpeg-9]# ./configure --prefix=/usr/local/phpextend --enable-shared --enable-static
[iyunv@ASANGE jpeg-9]# make && make install
2、编译安装libpng,增强对png格式图片的处理功能。libpng-1.6.2.tar.gz
[iyunv@ASANGE ~]# tar xf libpng-1.6.2.tar.gz
[iyunv@ASANGE ~]# cd libpng-1.6.2
[iyunv@ASANGE libpng-1.6.2]# ./configure --prefix=/usr/local/phpextend
[iyunv@ASANGE libpng-1.6.2]# make && make install
3、编译安装freetype,增强php对字体的处理功能。 freetype-2.4.12.tar.gz
[iyunv@ASANGE ~]# tar xf freetype-2.4.12.tar.gz
[iyunv@ASANGE ~]# cd freetype-2.4.12
[iyunv@ASANGE freetype-2.4.12]# ./configure --prefix=/usr/local/phpextend
[iyunv@ASANGE freetype-2.4.12]# make && make install
4、编译安装mhash,使php支持多种哈稀演算法。 mhash-0.9.9.9.tar.gz
[iyunv@ASANGE ~]# tar xf mhash-0.9.9.9.tar.gz
[iyunv@ASANGE ~]# cd mhash-0.9.9.9
[iyunv@ASANGE mhash-0.9.9.9]# ./configure --prefix=/usr/local/phpextend
[iyunv@ASANGE mhash-0.9.9.9]# make && make install
5、编译安装libmcrypt,使php支持多种加密算法。libmcrypt-2.5.8.tar.gz
[iyunv@ASANGE ~]# tar xf libmcrypt-2.5.8.tar.gz
[iyunv@ASANGE ~]# cd libmcrypt-2.5.8
[iyunv@ASANGE libmcrypt-2.5.8]# ./configure --prefix=/usr/local/phpextend
[iyunv@ASANGE libmcrypt-2.5.8]# make && make install
6、编译安装libevent,支持select、epoll、kqueue等事件调度机制。 libevent-2.0.21-stable.tar.gz
[iyunv@ASANGE ~]# tar xf libevent-2.0.21-stable.tar.gz
[iyunv@ASANGE ~]# cd libevent-2.0.21-stable
[iyunv@ASANGE libevent-2.0.21-stable]# ./configure --prefix=/usr/local/phpextend
[iyunv@ASANGE libevent-2.0.21-stable]# make && make install
7、编译安装libiconv,实现一个字符编码到另一个字符编码的转换。 libiconv-1.14.tar.gz
[iyunv@ASANGE ~]# tar xf libiconv-1.14.tar.gz
[iyunv@ASANGE ~]# cd libiconv-1.14
[iyunv@ASANGE libiconv-1.14]# ./configure --prefix=/usr/local/phpextend
[iyunv@ASANGE libiconv-1.14]# make && make install
8、输出库文件
[iyunv@ASANGE ~]# echo "/usr/local/phpextend/lib" >> /etc/ld.so.conf.d/phpextend.conf
[iyunv@ASANGE ~]# ldconfig
9、编译安装php。 php-5.5.0.tar.bz2
[iyunv@ASANGE php-5.5.0]# tar xf php-5.5.0.tar.bz2
[iyunv@ASANGE php-5.5.0]# cd php-5.5.0
[iyunv@ASANGE php-5.5.0]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fastcgi --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --disable-debug --disable-ipv6 --with-iconv-dir --with-freetype-dir=/usr/local/phpextend --with-jpeg-dir=/usr/local/phpextend --with-png-dir=/usr/local/phpextend --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap
[iyunv@ASANGE php-5.5.0]# make // 如果报错“make: *** [sapi/cli/php] Error 1” 使用命令“make ZEND_EXTRA_LIBS='-liconv' ”进行编译
[iyunv@ASANGE php-5.5.0]# make install
提供php主配置文件:
1
[iyunv@ASANGE php-5.5.0]# cp php.ini-development /usr/local/php/etc/php.ini
为php-fpm提供启动脚本,添加至服务列表并开机启动:
[iyunv@ASANGE php-5.5.0]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[iyunv@ASANGE php-5.5.0]# chmod +x /etc/init.d/php-fpm
[iyunv@ASANGE php-5.5.0]# chkconfig --add php-fpm
[iyunv@ASANGE php-5.5.0]# chkconfig php-fpm on
10、启用fastcgi:
fastcgi比较重要,这里介绍一下:fastcgi是http server和动态脚本语言间通信的接口,nginx支持fastcgi;同时fastcgi也被许多脚本语言支持,包括php。传统的cgi接口性能很差,每次http server遇到动态程序时,都需要重启脚本解释器来执行解析,然后将结果返回给http server。fastcgi采用C/S架构,将http server和脚本解析器分开,同时在脚本解析器上启动一个或多个脚本解析守护进程,当httt server遇到动态程序时,可以直接交付给fastcgi进程执行,然后将得到的结果返回给浏览器,这样http server只处理静态请求或将动态服务器的结果返回给客户端,从而提高性能。
nginx通过fastcgi来调用php,而php-fpm是fastcgi的进程管理器。fastcgi的主要作用就是将动态语言和http server分离开来,所以nginx和php/php-fpm经常配置在不同服务器上,各司其职,从而提高性能。
# cd /usr/local/php5/etc/
# cp php-fpm.conf.default php-fpm.conf
# vi php-fpm.conf //一般配置的依据如下
内存小于4G服务器(值可逐级递减):
修改如下参数:
pm=dynamic
pm.max_children=40
pm.start_servers=10
pm.min_spare_servers=10
pm.max_spare_servers=40
******************************
内存大于4G服务器(值可逐级递增):
修改如下参数:
pm=static
pm.max_children=100
11、配置ngin支持php:
编辑nginx的主配置文件:
location / {
root html;
index index.html index.htm index.php; //添加index.php
}
//启用下面的配置
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
编辑fastcgi_params参数,将里面的内容全部替换为以下内容:
#vim /etc/nginx/fastcgi_params
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
启动php-fpm,重启nginx
[iyunv@ASANGE ~]# service php-fpm start
Starting php-fpm done
[iyunv@ASANGE ~]# service nginx restart
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
停止 nginx: [确定]
正在启动 nginx:tig
添加php测试页面:
[iyunv@ASANGE ~]# vim /usr/html/index.php
<?php
phpinfo();
?>
浏览器上访问:
[Shell] 纯文本查看 复制代码
12、安装php加速器。
eaccelerator是一个开源的php加速器,将编译后的php代码opcode缓存进共享内存,下次访问直接调用opcode从而提高速度。
[iyunv@ASANGE ~]# unzip eaccelerator-0.9.6.1.zip
[iyunv@ASANGE ~]# cd eaccelerator-0.9.6.1
[iyunv@ASANGE eaccelerator-0.9.6.1]# ls
AUTHORS control.php doc ea_dasm.c ea_restore.h Makefile.in opcodes.h win32
bugreport.php COPYING eaccelerator.c ea_dasm.h ea_store.c mm.c optimize.c
ChangeLog dasm.php eaccelerator.h ea_info.c ea_store.h mm.h PHP_Highlight.php
config.m4 debug.c eaccelerator.ini ea_info.h fnmatch.c NEWS README
config.w32 debug.h eaccelerator_version.h ea_restore.c fnmatch.h opcodes.c README.win32
[iyunv@ASANGE eaccelerator-0.9.6.1]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20121113
Zend Module Api No: 20121212
Zend Extension Api No: 220121212
[iyunv@ASANGE eaccelerator-0.9.6.1]# ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
[iyunv@ASANGE eaccelerator-0.9.6.1]# make && make install
到此lnmp的环境搭建完成。
|