6tl4 发表于 2013-9-16 09:46:27

源码编译实现企业级LNMP平台

概念简单了解:
Nginx("enginex") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
在高并发连接的情况下,Nginx是Apache服务器不错的替代品。根据调查显示Nginx+PHP(FastCGI)可以承受3万以上的并发连接数,相当于同等环境下Apache的10倍。
下面我们一起来配置CentOS+Nginx+mysql+php组成的架构。
环境信息介绍:
系统版本:CentOS6.4x86_64
Nginx版本:nginx-1.4.2.tar.gz
数据库版本:mysql-5.5.33.tar.gz
数据库管理工具:phpMyAdmin-4.0.5-all-languages.zip
加速器版本:xcache-3.0.3.tar.bz2
PHP版本:php-5.4.19.tar.bz2
实现步骤:
一、安装配置Nginx:
1、解决依赖关系
编译安装nginx需要事先需要安装开发包组"DevelopmentTools"和"Server Platform Development"。同时,还需要专门安装pcre-devel包
# yum groupinstall "Development tools" "Server Platform Development"# yum -y install pcre-devel2、编译安装Nginx首先添加用户nginx,实现以之运行nginx服务进程:
# useradd -r nginx其次解压缩nginx查看介绍编译安装选项:
# tar xf nginx-1.4.2.tar.gz            #解压nginx# cd nginx-1.4.2                     #切换目录# ./configure --help | less   #查看编译选项--help                               print this message--prefix=PATH                      set installation prefix                     #目录--sbin-path=PATH                   set nginx binary pathname                     #二进制程序的安装目录--conf-path=PATH                   set nginx.conf pathname                     #配置文件路径--error-log-path=PATH            set error log pathname                        #错误路径--pid-path=PATH                  set nginx.pid pathname                        #pid路径--lock-path=PATH                   set nginx.lock pathname                     lock路径--user=USER               set non-privileged user for worker processes               #以哪个身份运行--group=GROUP               set non-privileged group for worker processes               #以哪个组的身份运行--builddir=DIR                     set build directory--with-rtsig_module                enable rtsig module                            #启用实时信号模块--with-select_module               enable select module                           #支持select机制模块--without-select_module            disable select module--with-poll_module               enable poll module--without-poll_module            disable poll module--with-file-aio                  enable file AIO support                        #支持文件的AIO机制的,重要的特性增强--with-ipv6                        enable IPv6 support--with-http_ssl_module             enable ngx_http_ssl_module                     #支持SSL功能--with-http_flv_module             enable ngx_http_flv_module                      #支持flv流媒体###############更多选项笔者就不在介绍了###########################我们这里用到的选项如下###############./configure   --prefix=/usr   --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    --lock-path=/var/lock/nginx.lock   --user=nginx   --group=nginx   --with-http_ssl_module   --with-http_flv_module   --with-http_stub_status_module   --with-http_gzip_static_module   --http-client-body-temp-path=/var/tmp/nginx/client/   --http-proxy-temp-path=/var/tmp/nginx/proxy/   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi   --http-scgi-temp-path=/var/tmp/nginx/scgi   --with-pcre##############安装####################### make && make install3、查看Nginx的配置文件Nginx的配置有着几个不同的上下文:main、http、server、upstream和location(还有实现邮件服务反向代理的mail)。配置语法的格式和定义方式遵循所谓的C风格,因此支持嵌套,还有着逻辑清晰并易于创建、阅读和维护等优势。
# vim /etc/nginx/nginx.conf#usernobody;worker_processes1;            #启动多少个进程(和CPU的核心个数相关)#error_loglogs/error.log;       #错误日志定义#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#由于我们此前编译时已经指定了错误日志位置所以这里默认注释掉了。#pid      logs/nginx.pid;events {    worker_connections1024;               #一个进程允许多少个连接进来(受限于当前用户所能够打开的最大数(ulimit –n 查看最大数))}#################以上这些为全局配置文件##############http {                     #####这些为http服务相关的配置信息####    include       mime.types;    default_typeapplication/octet-stream;         #定义默认类型    #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '    #日志格式    #                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#                  '"$http_user_agent" "$http_x_forwarded_for"';    #access_loglogs/access.logmain;    sendfile      on;            #支持sendfile(sendfile:提升文件传输速率)    #tcp_nopush   on;    #keepalive_timeout0;    keepalive_timeout65;         #是否支持长连接    #gzipon;    server {                      ###在nginx中至少有一个虚拟主机,它不支持中心主机      listen       80;      server_namelocalhost;      #charset koi8-r;      #access_loglogs/host.access.logmain;      location / {             #匹配的是URL文件,location=…表示精确匹配            root   html;            indexindex.html index.htm;      }##############下面就不在一一介绍了############4、Nginx启动首先确保本台服务器上httpd进程已关闭,因为端口一致会征用同一套接字。提供SysVinit脚本:
# vim /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=([^ ]*).*//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 2esac#################为此脚本赋予执行权限########################### cd /etc/rc.d/init.d/# chmod +x nginx#################添加之服务管理列表,开机自启动################# chkconfig --add nginx# chkconfig nginx on#################启动服务器##################################### service nginx startStarting nginx:                                          #################查看进程启动状况############################## ps aux | grep nginx5、访问网页

二:编译安装配置mysql

###################安装cmake###################若想编译安装mysql必须借助跨平台编译器cmake。# yum -y install cmake###################解压缩mysql################## tar xf mysql-5.5.33.tar.gz###################创建程序运行用户############# groupadd -r mysql# useradd -g mysql -r mysql###################创建数据存放目录############建议:真实环境下尽量使用逻辑卷存放数据!!# mkdir -pv /mydata/data# chown -R mysql.mysql /mydata/data###################编译mysql###################编译选项了解参考:http://pangge.blog./6013757/1059896# cd mysql-5.5.33# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata/data -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci####################安装mysql################# make && make install####################更改属组################## cd /usr/local/mysql/# chown -R :mysql *###################初始化数据库############### scripts/mysql_install_db --user=mysql --datadir=/mydata/data/###################创建配置文件############### cp support-files/my-large.cnf /etc/my.cnf###################编辑配置文件############### cd /etc/# vim my.cnfdatadir = /mydata/data                  #指定mysql数据文件的存放位置###################创建执行脚本############### cp support-files/mysql.server /etc/rc.d/init.d/mysqld# chmod +x /etc/rc.d/init.d/mysqld   #执行权限##################添加服务################### chkconfig --add mysqld#################启动服务#################### service mysqld start#################设置环境变量################ vim /etc/profile.d/mysql.shexport PATH=/usr/local/mysql/bin:$PATH            #添加# . /etc/profile.d/mysql.sh################创建登录密码################ mysqladmin -u root password mypass# mysql -uroot –pmypass###############指定访问权限################mysql> grant all privileges on *.* to root@'172.16.%.%' identified by 'mypass';mysql> flush privileges;            重读授权表三、编译安装php

##############解压php##################### tar xf php-5.4.19.tar.bz2#############可能需要安装的依赖包########### yum -y install libxml2-devel# yum -y install curl-devel# yum -y install bzip2-devel# yum -y install libmcrypt# yum -y install libmcrypt-devel##############编译php##################### cd php-5.4.19# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm--with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml--with-mhash --with-mcrypt--with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl#############安装php################### make && make install注意:php官方要求用户尽量在使用make之后使用make test测试一下再进行安装make install。##############为php提供配置文件####### cp php.ini-production /etc/php.ini##############为php提供SysV脚本###### cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm# chmod +x /etc/rc.d/init.d/php-fpm##############添加服务################ chkconfig --add php-fpm# chkconfigphp-fpm on##############为php-fpm提供配置文件### cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf注:php-fpm是一个服务器软件需要以服务器进程运行的所以也需要一个配置文件。#############启动php-fpm############# service php-fpm start四:整合nginx和php1、编辑配置文件/etc/nginx/nginx.conf1# vim /etc/nginx/nginx.conf################启动php状态############location ~ .php$ {            root         html;                #从哪里找php页面            fastcgi_pass   127.0.0.1:9000;      #用户请求转发给谁(php-fpm默认监听在9000端口上)            fastcgi_indexindex.php;         #默认页面(基于php-fpm下)            fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;    #传递的参数            include      fastcgi_params;      }################添加php格式的主页###########location / {            root   html;            indexindex.php index.html index.htm;      }


2、编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容:(以下这些内容参数不可以随便更改,它们主要是建立对应关系的)



fastcgi_paramGATEWAY_INTERFACECGI/1.1;fastcgi_paramSERVER_SOFTWARE    nginx;fastcgi_paramQUERY_STRING       $query_string;fastcgi_paramREQUEST_METHOD   $request_method;fastcgi_paramCONTENT_TYPE       $content_type;fastcgi_paramCONTENT_LENGTH   $content_length;fastcgi_paramSCRIPT_FILENAME    $document_root$fastcgi_script_name;fastcgi_paramSCRIPT_NAME      $fastcgi_script_name;fastcgi_paramREQUEST_URI      $request_uri;fastcgi_paramDOCUMENT_URI       $document_uri;fastcgi_paramDOCUMENT_ROOT      $document_root;fastcgi_paramSERVER_PROTOCOL    $server_protocol;fastcgi_paramREMOTE_ADDR      $remote_addr;fastcgi_paramREMOTE_PORT      $remote_port;fastcgi_paramSERVER_ADDR      $server_addr;fastcgi_paramSERVER_PORT      $server_port;fastcgi_paramSERVER_NAME      $server_name;注:其实这些内容和原文件内容并没有太大的差别,请读者详细查看并理解其内容。
3、重新载入nginx的配置文件



# service nginx reload4、测试
在/usr/html新建index.php的测试页面,测试php是否能正常工作:

# cat > /usr/html/index.php << EOFEOF






五、安装xcache,为php加速:
1、安装
############解压xcache############# tar xf xcache-3.0.3.tar.bz2############编译模块准备########### cd xcache-3.0.3# /usr/local/php/bin/phpize############编译安装模块############### ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config# make && make install2、编辑php.ini,整合php和xcache:将xcache提供的样例配置导入php.ini
# mkdir /etc/php.d# cp xcache.ini /etc/php.d3、重启php-fpm
[root@yongxcache-3.0.3]# service php-fpm restart
六、安装phpMyAdmin
1、解压

1
# unzip phpMyAdmin-4.0.5-all-languages.zip




2、配置phpMyAdmin
1
2
# cd phpMyAdmin-4.0.5-all-languages
[root@yongphpMyAdmin-4.0.5-all-languages]# mv * /usr/html/




3、登录测试4、管理mysql
至此,Linux+Nginx+Mysql+PHP配置就结束了,并且实现了PHP的加速功能和用phpMyAdmin管理数据库功能。若有错误或不明白就留言笔者,大家的支持是笔者贡献的最大动力!!

xyzjr 发表于 2013-11-29 12:00:25

时间让我们认识了自己也肯定了对方//

老爷子88 发表于 2013-12-2 02:25:21

爱情里没有谁对谁错,因为没有一个标准可以衡量

爱她吗 发表于 2013-12-5 01:23:50

虽然我不知道前方的路会怎样,但我会加油的╰つ

圣凤凌霜 发表于 2013-12-8 03:22:34

"对你无处释放的爱,到最后得到的却是无限的失望。

xuanhao 发表于 2013-12-10 07:57:01

请不要拥抱我、我的心是凉的,别再说对不起。

285728934 发表于 2013-12-13 02:48:36

沵是不是也像别人一样,得到了就不会去珍惜︶ ̄
页: [1]
查看完整版本: 源码编译实现企业级LNMP平台