【博文推荐】搭建完全分离式LNMP平台的简单案例

系统 Linux
编者按:现在Nginx运用越来越广广泛,Nginx的优点也越来越突出,所以,在很多企业采用了LNMP架构,而且, 一些软件也运用在LNMP平台上比如ZABBIX。下面步入正题,看LNMP架构怎么搭建。

写在前面:如果此文有幸被某位朋友看见并发现有错的地方,希望批评指正。如有不明白的地方,愿可一起探讨。

博文地址:http://muluhe.blog.51cto.com/9152490/1564120


案例拓扑图

wKioL1Q9NNyDOYpoAADB-wCoxsg241.jpg

安装配置nginx服务器


编译安装nginx时,需要事先安装 开发包组"Development Tools"和"Server Platform Development",同时还需专门安装pcre-devel包。

  1. # yum -y groupinstall "Development Tools"   
  2. # yum -y groupinstall "Server Platform Development"   
  3. # yum -y install pcre-devel 

首先添加nginx用户组和nginx用户

  1. # groupadd -r nginx   
  2. # useradd -g nginx -r nginx 

创建编译安装是所需要的目录

  1. # mkdir -pv /var/tmp/nginx/client 

编译安装nginx

  1. # tar xf nginx-1.4.7.tar.gz   
  2. # cd nginx-1.4.7   
  3. # ./configure \   
  4.   --prefix=/usr/local/nginx \   
  5.   --sbin-path=/usr/local/nginx/sbin/nginx \   
  6.   --conf-path=/etc/nginx/nginx.conf \   
  7.   --error-log-path=/var/log/nginx/error.log \   
  8.   --http-log-path=/var/log/nginx/access.log \   
  9.   --pid-path=/var/run/nginx/nginx.pid  \   
  10.   --lock-path=/var/lock/nginx.lock \   
  11.   --user=nginx \   
  12.   --group=nginx \   
  13.   --with-http_ssl_module \   
  14.   --with-http_flv_module \   
  15.   --with-http_stub_status_module \   
  16.   --with-http_gzip_static_module \   
  17.   --http-client-body-temp-path=/var/tmp/nginx/client/ \   
  18.   --http-proxy-temp-path=/var/tmp/nginx/proxy/ \   
  19.   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \   
  20.   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \   
  21.   --http-scgi-temp-path=/var/tmp/nginx/scgi \   
  22.   --with-pcre   
  23. # make && make install 

为nginx提供SysV init脚本

  1. # vim /etc/rc.d/init.d/nginx 
  1. #!/bin/sh   
  2. #   
  3. # nginx - this script starts and stops the nginx daemon   
  4. #   
  5. # chkconfig:   - 85 15    
  6. # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \   
  7. #               proxy and IMAP/POP3 proxy server   
  8. # processname: nginx   
  9. # config:      /etc/nginx/nginx.conf   
  10. # config:      /etc/sysconfig/nginx   
  11. # pidfile:     /var/run/nginx.pid   
  12.      
  13. # Source function library.   
  14. . /etc/rc.d/init.d/functions  
  15.      
  16. # Source networking configuration.   
  17. . /etc/sysconfig/network  
  18.      
  19. # Check that networking is up.   
  20. [ "$NETWORKING" = "no" ] && exit 0   
  21.      
  22. nginx="/usr/local/nginx/sbin/nginx" 
  23. prog=$(basename $nginx)   
  24.      
  25. NGINX_CONF_FILE="/etc/nginx/nginx.conf" 
  26.      
  27. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx  
  28.      
  29. lockfile=/var/lock/subsys/nginx  
  30.      
  31. make_dirs() {   
  32.    # make required directories   
  33.    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`   
  34.    options=`$nginx -V 2>&1 | grep 'configure arguments:'`   
  35.    for opt in $options; do  
  36.        if [ `echo $opt | grep '.*-temp-path'` ]; then  
  37.            value=`echo $opt | cut -d "=" -f 2`   
  38.            if [ ! -d "$value" ]; then  
  39.                # echo "creating" $value   
  40.                mkdir -p $value && chown -R $user $value   
  41.            fi  
  42.        fi  
  43.    done  
  44. }   
  45.      
  46. start() {   
  47.     [ -x $nginx ] || exit 5   
  48.     [ -f $NGINX_CONF_FILE ] || exit 6   
  49.     make_dirs   
  50.     echo -n $"Starting $prog: "  
  51.     daemon $nginx -c $NGINX_CONF_FILE   
  52.     retval=$?   
  53.     echo  
  54.     [ $retval -eq 0 ] && touch $lockfile   
  55.     return $retval   
  56. }   
  57.      
  58. stop() {   
  59.     echo -n $"Stopping $prog: "  
  60.     killproc $prog -QUIT   
  61.     retval=$?   
  62.     echo  
  63.     [ $retval -eq 0 ] && rm -f $lockfile   
  64.     return $retval   
  65. }   
  66.      
  67. restart() {   
  68.     configtest || return $?   
  69.     stop   
  70.     sleep 1   
  71.     start   
  72. }   
  73.      
  74. reload() {   
  75.     configtest || return $?   
  76.     echo -n $"Reloading $prog: "  
  77.     killproc $nginx -HUP   
  78.     RETVAL=$?   
  79.     echo  
  80. }   
  81.      
  82. force_reload() {   
  83.     restart   
  84. }   
  85.      
  86. configtest() {   
  87.   $nginx -t -c $NGINX_CONF_FILE   
  88. }   
  89.      
  90. rh_status() {   
  91.     status $prog   
  92. }   
  93.      
  94. rh_status_q() {   
  95.     rh_status >/dev/null 2>&1   
  96. }   
  97.      
  98. case "$1" in  
  99.     start)   
  100.         rh_status_q && exit 0   
  101.         $1   
  102.         ;;   
  103.     stop)   
  104.         rh_status_q || exit 0   
  105.         $1   
  106.         ;;   
  107.     restart|configtest)   
  108.         $1   
  109.         ;;   
  110.     reload)   
  111.         rh_status_q || exit 7   
  112.         $1   
  113.         ;;   
  114.     force-reload)   
  115.         force_reload   
  116.         ;;   
  117.     status)   
  118.         rh_status   
  119.         ;;   
  120.     condrestart|try-restart)   
  121.         rh_status_q || exit 0   
  122.             ;;   
  123.     *)   
  124.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"  
  125.         exit 2   
  126. esac 

为此脚本赋予执行权限

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

将nginx服务添加至服务管理列表,并让其开机自动启动

  1. # chkconfig --add nginx   
  2. # chkconfig nginx on 

编辑配置文件/etc/nginx/nginx.conf,在server段内添加如下内容

  1. location ~ \.php$ {   
  2.     fastcgi_pass   10.170.2.90:9000;   
  3.     fastcgi_index  index.php;   
  4.     fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;   
  5.     include        fastcgi_params;   

启动nginx服务

  1. # vim /etc/init.d/nginx start 

测试nginx是否工作起来,在浏览器中键入10.170.2.80,可以得到如下页面

wKioL1Q9LSiw-gBgAAG9vqavcsc123.jpg

安装PHP服务器


编译安装php

  1. # tar xf php-5.4.26.tar.bz2   
  2. # cd php-5.4.26   
  3. # ./configure --prefix=/usr/local/php5 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2   
  4. # make && make install 

为php提供配置文件

  1. # cp php.ini-production /etc/php.ini 

配置php-fpm

  1. # cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm   
  2. # chmod +x /etc/rc.d/init.d/php-fpm   
  3. # chkconfig --add php-fpm   
  4. # chkconfig php-fpm on   
  5. # cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf 

编辑配置文件/usr/local/php5/etc/php-fpm.conf,将以下选项修改为相对应的值

  1. pm.max_children = 50   
  2. pm.start_servers = 5   
  3. pm.min_spare_servers = 2   
  4. pm.max_spare_servers = 8   
  5. pid = /usr/local/php5/var/run/php-fpm.pid 

在/var/www/html目录下提供测试页面index.php,其内容为

  1. <h1>hello,nginx</h1>   
  2. <?php   
  3.         $link = mysql_connect('10.170.2.36','testuser','******');   
  4.         if ($link)   
  5.                 echo "Success...";   
  6.         else  
  7.                 echo "Failure...";   
  8.     
  9.         mysql_close();   
  10.         phpinfo();   
  11. ?> 

启动php-fpm服务

  1. # /etc/init.d/php-fpm start 

测试nginx服务器与php服务器是否能够建立通信,在浏览器中键入10.170.2.80/index.php,可以得到如下页面

wKioL1Q9M63ijdb-AAI2nNDl8qs444.jpg

页面中显示Failure...,是因为后端的数据库还没有进行相应的配置

安装MariaDB服务器


编译安装mariadb-5.5.36

  1. # tar xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local   
  2. # cd /usr/local/   
  3. # ln -sv mariadb-5.5.36-linux-x86_64/ mysql   
  4. # mkdir -pv /mysql/data   
  5. # groupadd -r mysql   
  6. # useradd -g mysql -s /sbin/nologin -M -d /mysql/data -r mysql   
  7. # chown -R mysql:mysql /mysql   
  8. # chown -R mysql:mysql /mysql/data 

为数据库提供配置文件:

  1. # cd mysql   
  2. # mkdir /etc/mysql   
  3. # chown -R root.mysql ./*   
  4. # cp support-files/my-large.cnf /etc/mysql/my.cnf   
  5. 修改文件/etc/mysql/my.cnf文件内容,在thread_concurrency = 8行下添加一行:   
  6. datadir = /mysql/data 

为数据库提供SysV启动脚本,并设置为开机启动:

  1. # cp support-files/mysql.server /etc/init.d/mysqld   
  2. # chkconfig --add mysqld   
  3. # chkconfig mysqld on 

初始化数据库并启动数据库:

  1. # echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh   
  2. # source /etc/profile.d/mysql.sh    
  3. # echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf   
  4. # ldconfig   
  5. # ln -sv /usr/local/mysql/include/ /usr/include/mysql   
  6. # scripts/mysql_install_db --user=mysql --datadir=/mysql/data/   
  7. # /etc/init.d/mysqld start 

创建数据库并授权:

  1. MariaDB [(none)]> CREATE DATABASE testdb;   
  2. MariaDB [(none)]> GRANT ALL ON testdb.* TO dscuser@'10.170.2.%' IDENTIFIED BY '******';   
  3. MariaDB [(none)]> FLUSH PRIVILEGES; 

整体测试LNMP平台


在浏览器中键入10.170.2.80/index.php,可以得到如下页面

wKioL1Q9ND6RPJbOAAI3E1x54NM883.jpg

这次可以看到页面中显示Success...信息。

责任编辑:林师授 来源: 51CTO
相关推荐

2015-03-09 14:53:04

OracleOracle DGDataGuard F

2014-12-24 11:13:06

可用性集availabilitset

2018-11-08 11:12:09

存储分离式超融合

2014-11-25 11:33:35

2015-05-15 10:04:28

localhost

2015-02-27 10:14:33

2015-06-15 13:06:23

项目项目经验

2018-08-02 09:13:34

分离式超融合存储

2018-05-07 09:34:39

分离式超融合存储

2015-07-01 10:25:07

Docker开源项目容器

2015-06-17 09:34:09

软件定义存储 云存储

2015-09-29 10:26:51

pythonlogging模块

2015-12-10 10:13:22

2015-03-19 09:35:36

OpenStack平台性能测试Rally功能测试Tempest

2014-12-12 10:46:55

Azure地缘组affinitygro

2015-07-29 13:46:27

OpenStackIcehouse私有云实战部署

2011-12-08 16:31:43

新浪微博开放平台

2015-04-07 09:32:57

phpSocket通信php出现错误

2011-12-08 16:51:55

新浪微博开放平台

2014-12-22 11:04:30

Windows AzuiPhone虚拟机
点赞
收藏

51CTO技术栈公众号