源碼編譯實現企業級LNMP平臺

概念簡單瞭解:php

Nginx("enginex") 是一個高性能的 HTTP 和反向代理服務器,也是一個 IMAP/POP3/SMTP 代理服務器。html

在高併發鏈接的狀況下,Nginx是Apache服務器不錯的替代品。根據調查顯示Nginx+PHP(FastCGI)能夠承受3萬以上的併發鏈接數,至關於同等環境下Apache的10倍。mysql

下面咱們一塊兒來配置CentOS+Nginx+mysql+php組成的架構nginx


環境信息介紹:sql

系統版本:CentOS6.4x86_64 數據庫

Nginx版本:nginx-1.4.2.tar.gzvim

數據庫版本:mysql-5.5.33.tar.gzapi

數據庫管理工具:phpMyAdmin-4.0.5-all-languages.zipbash

加速器版本:xcache-3.0.3.tar.bz2服務器

PHP版本:php-5.4.19.tar.bz2


實現步驟:

1、安裝配置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服務進程:

[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、訪問網頁

201355554.png

二:編譯安裝配置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://pangge.blog.51cto.com/6013757/1059896
[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;            重讀受權表

3、編譯安裝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;

注:其實這些內容和原文件內容並無太大的差異,請讀者詳細查看並理解其內容。

三、從新載入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

201833932.png

5、安裝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

202126898.png

6、安裝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、登陸測試

202211835.png

4、管理mysql

202240827.png


至此,Linux+Nginx+Mysql+PHP配置就結束了,而且實現了PHP的加速功能和用phpMyAdmin管理數據庫功能。

如有錯誤或不明白就留言筆者,你們的支持是筆者貢獻的最大動力!!

相關文章
相關標籤/搜索