源码编译实现企业级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包
[root@yong ~]# yum groupinstall “Development tools” “Server Platform Development” 
[root@yong ~]# yum -y install pcre-devel

2、编译安装Nginx
首先添加用户nginx,实现以之运行nginx服务进程:
1 [root@yong ~]# useradd -r nginx

其次解压缩nginx查看介绍编译安装选项:

[root@yong ~]# tar xf nginx-1.4.2.tar.gz            #解压nginx 
[root@yong ~]# cd nginx-1.4.2                      #切换目录 
[root@yong 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流媒体 
###############更多选项笔者就不在介绍了############ 
###############我们这里用到的选项如下############## 
[root@yong nginx-1.4.2]#  ./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 
##############安装###################### 
[root@yong nginx-1.4.2]# make && make install

3、查看Nginx的配置文件 
Nginx的配置有着几个不同的上下文:main、http、server、upstream和location(还有实现邮件服务反向代理的mail)。配置语法的格式和定义方式遵循所谓的C风格,因此支持嵌套,还有着逻辑清晰并易于创建、阅读和维护等优势。 
[root  @yong  ~]# vim /etc/nginx/nginx.conf 
#user  nobody; 
worker_processes  1;              #启动多少个进程(和CPU的核心个数相关) 
#error_log  logs/error.log;      #错误日志定义 
#error_log  logs/error.log  notice; 
#error_log  logs/error.log  info; 
#由于我们此前编译时已经指定了错误日志位置所以这里默认注释掉了。 
#pid        logs/nginx.pid; 
events { 
    worker_connections  1024;              #一个进程允许多少个连接进来(受限于当前用户所能够打开的最大数(ulimit –n 查看最大数)) 

#################以上这些为全局配置文件############## 
http {                    #####这些为http服务相关的配置信息#### 
    include      mime.types; 
    default_type  application/octet-stream;        #定义默认类型 
    #log_format  main  ‘$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_log  logs/access.log  main; 
    sendfile        on;            #支持sendfile(sendfile:提升文件传输速率) 
    #tcp_nopush    on; 
    #keepalive_timeout  0; 
    keepalive_timeout  65;        #是否支持长连接 
    #gzip  on; 
    server {                      ###在nginx中至少有一个虚拟主机,它不支持中心主机 
        listen      80; 
        server_name  localhost; 
        #charset koi8-r; 
        #access_log  logs/host.access.log  main; 
        location / {            #匹配的是URL文件,location=…表示精确匹配 
            root  html; 
            index  index.html index.htm; 

        }


4、Nginx启动
首先确保本台服务器上httpd进程已关闭,因为端口一致会征用同一套接字。
提供SysVinit脚本:
[root@yong ~]# 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=\([^ ]*\).*/\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
#################为此脚本赋予执行权限########################## 
[root@yong ~]# cd /etc/rc.d/init.d/ 
[root@yong init.d]# chmod +x nginx 
#################添加之服务管理列表,开机自启动################ 
[root@yong init.d]# chkconfig --add nginx 
[root@yong init.d]# chkconfig nginx on 
#################启动服务器#################################### 
[root@yong init.d]# service nginx start 
Starting nginx:                                            [  OK  ] 
#################查看进程启动状况############################# 
[root@yong ~]# ps aux | grep nginx


5、访问网页

二:编译安装配置mysql

###################安装cmake################### 
若想编译安装mysql必须借助跨平台编译器cmake。 
[root@yong ~]# yum -y install cmake 
###################解压缩mysql################# 
[root@yong ~]# tar xf mysql-5.5.33.tar.gz 
###################创建程序运行用户############ 
[root@yong ~]# groupadd -r mysql 
[root@yong ~]# useradd -g mysql -r mysql 
###################创建数据存放目录############ 
建议:真实环境下尽量使用逻辑卷存放数据!! 
[root@yong ~]# mkdir -pv /mydata/data 
[root@yong ~]# chown -R mysql.mysql /mydata/data 
###################编译mysql###################

编译选项了解参考:http://www.linuxidc.com/Linux/2013-09/90349.htm

[root@yong ~]# cd mysql-5.5.33 
[root@yong 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################ 
[root@yong mysql-5.5.33]# make && make install 
####################更改属组################# 
[root@yong ~]# cd /usr/local/mysql/ 
[root@yong mysql]# chown -R :mysql * 
###################初始化数据库############## 
[root@yong mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data/ 
###################创建配置文件############## 
[root@yong mysql]# cp support-files/my-large.cnf /etc/my.cnf 
###################编辑配置文件############## 
[root@yong mysql]# cd /etc/ 
[root@yong etc]# vim my.cnf 
datadir = /mydata/data                  #指定mysql数据文件的存放位置 
###################创建执行脚本############## 
[root@yong mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld 
[root@yong mysql]# chmod +x /etc/rc.d/init.d/mysqld    #执行权限 
##################添加服务################## 
[root@yong mysql]# chkconfig --add mysqld 
#################启动服务################### 
[root@yong mysql]# service mysqld start 
#################设置环境变量############### 
[root@yong mysql]# vim /etc/profile.d/mysql.sh 
export PATH=/usr/local/mysql/bin:$PATH              #添加 
[root@yong mysql]# . /etc/profile.d/mysql.sh 
################创建登录密码############### 
[root@yong mysql]# mysqladmin -u root password mypass 
[root@yong mysql]# mysql -uroot –pmypass 
###############指定访问权限################ 
mysql> grant all privileges on *.* to root@'172.16.%.%' identified by 'mypass'; 
mysql> flush privileges;            重读授权表

三、编译安装php

##############解压php#################### 
[root@yong ~]# tar xf php-5.4.19.tar.bz2 
#############可能需要安装的依赖包########## 
[root@yong php-5.4.19]# yum -y install libxml2-devel 
[root@yong php-5.4.19]# yum -y install curl-devel 
[root@yong php-5.4.19]# yum -y install bzip2-devel 
[root@yong php-5.4.19]# yum -y install libmcrypt 
[root@yong php-5.4.19]# yum -y install libmcrypt-devel 
##############编译php#################### 
[root@yong ~]# cd php-5.4.19 
[root@yong 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################## 
[root@yong php-5.4.19]# make && make install 
注意:php官方要求用户尽量在使用make之后使用make test测试一下再进行安装make install。 
##############为php提供配置文件###### 
[root@yong php-5.4.19]# cp php.ini-production /etc/php.ini 
##############为php提供SysV脚本##### 
[root@yong php-5.4.19]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm 
[root@yong php-5.4.19]# chmod +x /etc/rc.d/init.d/php-fpm 
##############添加服务############### 
[root@yong php-5.4.19]# chkconfig --add php-fpm 
[root@yong php-5.4.19]# chkconfig  php-fpm on 
##############为php-fpm提供配置文件## 
[root@yong php-5.4.19]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 
注:php-fpm是一个服务器软件需要以服务器进程运行的所以也需要一个配置文件。 
#############启动php-fpm############ 
[root@yong etc]# service php-fpm start

四:整合nginx和php
1、编辑配置文件/etc/nginx/nginx.conf
[root@yong ~]# vim /etc/nginx/nginx.conf 
################启动php状态############ 
location ~ \.php$ { 
            root          html;                #从哪里找php页面 
            fastcgi_pass  127.0.0.1:9000;      #用户请求转发给谁(php-fpm默认监听在9000端口上) 
            fastcgi_index  index.php;          #默认页面(基于php-fpm下) 
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;    #传递的参数 
            include        fastcgi_params; 
        } 
################添加php格式的主页########### 
location / { 
            root  html; 
            index  index.php index.html index.htm; 
        }

2、编辑/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;

注:其实这些内容和原文件内容并没有太大的差别,请读者详细查看并理解其内容。
3、重新载入nginx的配置文件
[root@yong html]# service nginx reload

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

[root@yong html]# cat > /usr/html/index.php << EOF 
<?php 
phpinfo(); 
?> 
EOF

五、安装xcache,为php加速:
1、安装
############解压xcache############ 
[root@yong ~]# tar xf xcache-3.0.3.tar.bz2 
############编译模块准备########## 
[root@yong ~]# cd xcache-3.0.3 
[root@yong xcache-3.0.3]# /usr/local/php/bin/phpize 
############编译安装模块############## 
[root@yong xcache-3.0.3]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config 
[root@yong xcache-3.0.3]# make && make install

2、编辑php.ini,整合php和xcache:
将xcache提供的样例配置导入php.ini
[root@yong xcache-3.0.3]# mkdir /etc/php.d 
[root@yong xcache-3.0.3]# cp xcache.ini /etc/php.d

3、重启php-fpm
[root@yongxcache-3.0.3]# service php-fpm restart

六、安装phpMyAdmin 
1、解压 
[root@yong~]# unzip phpMyAdmin-4.0.5-all-languages.zip

2、配置phpMyAdmin
[root@yong~]# 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管理数据库功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值