天天看点

源码构建LNMP (Linux+Nginx+MySQL+PHP)

LNMP 简介:

Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。

Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。

Mysql是一个小型关系型数据库管理系统。

Php是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言。

这四种软件均为免费 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率。作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last/fm 描述了成功并且美妙的使用经验。Nginx对静态页面的支持相当出色,轻量且免费。Nginx不支持CGI,但是支持更灵活FastCGI。PHP5.2及之前的版本比较多的是使用 PHP-FPM来管理PHP FastCGI进程。PHP-FPM使用给PHP源码打补丁后编译的方式让新手多少有些难上手,但从PHP 5.3.2开始内置PHP-FPM,只需编译PHP时启用PHP-FPM。Nginx 安装非常的简单,配置文件 非常简洁(还能够支持perl语法),Bugs非常少的服务器: Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够在 不间断服务的情况下进行软件版本的升级。软件,组合到一起,成为一个免费、高效的网站服务系统PHP,是英文超级文本预处理语言Hypertext Preprocessor的缩写。PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,被广泛的运用。

环境:

Redhat 2.6.18-164.el5

nginx-1.1.18.tar.gz

mysql-5.0.95.tar.gz

php-5.4.0.tar.gz

nginx

安装编译环境

yum –y install gcc gcc-c++
Development Libraries
Development Tools
Legacy Software Development
X Software Development      

必要的库文件:

yum install pcre-devel (pcre 是一个正则表达式相关的包,支持nginx地址重写)  install zlib-devel  openssl-devel -y      
tar -zxvf libevent-2.0.16-stable.tar.gz -C /usr/src
./configure   
make && ake install      

vim /etc/ld.so.conf.d/libevent.conf

添加: /usr/local/lib/

ldconfig -v |grep libevent 确定是否调用成功      

tar -zxvf nginx-1.0.11 -C /usr/src

cd /usr/src/nginx-1.0.11

添加系统账户,用来运行nginx

groupadd -r nginx
useradd -r -g nginx -s /bin/false -M nginx   无家目录      

配置:

./configure \
--prefix=/usr/local \安装路径
--sbin-path=/usr/sbin/nginx \服务命令路径
--conf-path=/etc/nginx/nginx.conf \配置文件路径
--error-log-path=/var/log/nginx/error.log \错误日志路径
--http-log-path=/var/log/nginx/access.log \访问日志路径
--pid-path=/var/run/nginx/nginx.pid \pid路径
--lock-path=/var/lock/nginx.lock \锁定文件路径
--user=nginx \运行用户,就是刚才建立的那个
--group=nginx \组
--with-http_ssl_module \ssl模块
--with-http_flv_module \流媒体模块
--with-http_stub_status_module \获取Nginx自上次启动以来的工作状态
--with-http_gzip_static_module \压缩
--http-client-body-temp-path=/var/tmp/nginx/client/ \
    (mkdir -pv /var/tmp/nginx/client创建多级子目录 )
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--with-pcre      

编译安装:

make && make install      

nginx的启动脚本

下面这个脚本来自网络,测试可以使用:

cat /etc/rc.d/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      

这个函数的作用是测试修改过的Nginx的配置文件是否有问题

configtest() {

$nginx -t -c $NGINX_CONF_FILE

}

正常:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

如果脚本里没有,可以手动测一下:

/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf      

赋予脚本执行权限

chmod +x /etc/rc.d/init.d/nginx      

加入服务,开机启动

chkconfig --add nginx
chkconfig nginx on      

重启一下,看是否正常:

service nginx restart      
源码构建LNMP (Linux+Nginx+MySQL+PHP)

查看是否启动成功

netstat -tupln |grep nginx      

访问会出现如下页面:

源码构建LNMP (Linux+Nginx+MySQL+PHP)

mysql

yum -y install ncurses-devel

tar -zxvf mysql-5.0.95.tar.gz -C /usr/src

添加系统账号mysql:不允许登陆,无家目录

useradd -M -s /sbin/nologin mysql

cd /usr/src/mysql-5.0.95

./configure --prefix=/usr/local/mysql \
--without-debug \
--with-extra-charsets=utf8,gbk \中文支持
--enable-assembler \汇编模式
--with-mysqld-ldflags=-all-static \静态方式
--with-client-ldflags=-all-static \
--with-unix-socket-path=/tmp/mysql.sock \使用unix socket
--with-ssl      
make && make install      

复制配置文件

cp support-files/my-large.cnf /etc/my.cnf  可根据内存选择large,medium之类      

copy启动脚本

cp support-files/mysql.server /etc/rc.d/init.d/mysqld      
chmod +x /etc/rc.d/init.d/mysqld      

可执行文件和动态链接库文件的软连接:

ln -s /usr/local/mysql/bin/* /usr/local/bin/
ln -s /usr/local/mysql/lib/mysql/lib* /usr/lib/      

强制以mysql身份初始化数据库

mysql_install_db --user=mysql      

安装目录权限,注意这两条命令优先顺序:

chown -R root.mysql /usr/local/mysql/
chown -R mysql.mysql /usr/local/mysql/var/      

确保启动成功

服务项

chkconfig --add mysqld      

开机启动项

chkconfig mysqld on
service mysqld start      

netstat -tupln |grep mysql

php

apache是把php作为自己的模块来调用的,而Nginx+PHP需要PHP生成可执行文件才可以,nginx支持FastCGI技术,PHP-FPM是FastCGI的管理器,用来管理PHP FastCGI进程。作为PHP的插件存在。PHP 5.3.2开始内置PHP-FPM,只需编译PHP时启用PHP-FPM

安装库文件,依赖的软件包:

yum –y install libxml2-devel curl-devel libpng-devel openldap-devel      
tar -jxvf libmcrypt-2.5.8.tar.bz2
cd libmcrypt-2.5.8  ./configure  make && make install      
tar -jxvf mhash-0.9.9.9.tar.bz2
cd mhash-0.9.9.9  ./configure make && make install      
ln -s /usr/local/lib/libmcrypt* /usr/lib
ln -s /usr/local/lib/libmhash.* /usr/lib/      
tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8 ./configure make && make install      
源码构建LNMP (Linux+Nginx+MySQL+PHP)

解压缩:

tar -zxvf php-5.4.0.tar.gz -C /usr/local

cd/usr/src/ php-5.4.0

./configure \
--prefix=/usr/local/php \
--with-openssl \
--enable-fpm \
--with-mysql=/usr/local/mysql/ \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--with-curl \
--with-curlwrappers \
--enable-fastcgi \
--with-mcrypt \
--with-gd \
--with-mhash \
--enable-sockets \
--with-ldap \
--with-ldap-sasl \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-config-file-path=/etc/php \
--enable-mbstring=all \
--with-mysqli=/usr/local/mysql/bin/mysql_config      

make && make install

配置文件

cp php.ini-production /usr/local/php/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
ln -s /usr/local/php/bin/php /usr/bin/      

做nginx和php的关联,--enable-sockets,默认是通过9000端口把动态页面交给php处理的

vim /usr/local/php/etc/php-fpm.conf

将127.0.0.1:9000改为如下:

listen = /var/run/php-fpm/php-fpm.sock      

mkdir /var/run/php-fpm

添加index.php

vim /etc/nginx/nginx.conf

index  index.php index.html index.htm;      

打开如下几行,并做修改

location ~ \.php$ {
fastcgi_pass        unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index       index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
include fastcgi.conf;
}      

启动:

/usr/local/php/sbin/php-fpm start      

重启nginx

service nginx restart

创建php页面

cd /usr/html/     cp index.html index.php      

vim index.php 添加:<?php phpinfo(); ?>

源码构建LNMP (Linux+Nginx+MySQL+PHP)

访问:http://192.168.101.63

继续阅读