nawawa001 发表于 2018-5-24 11:40:50

Linux入门之LNMMP

  一、概述
  前面的博客中,使用了apache+php+mysql实现了在多个机器上部署LNMP。这里,才用nginx+fascgi+xcache+mysql的方式进行部署。
  二、编译安装nginx
  1、首先获取nginx代码进行安装,目前的稳定版本有两个,推荐大家尝试下淘宝的定制开发的nginx,已经开源了。这里以nginx1.4做示例。
  确认配置好编译安装环境,主要需要安装"Development Tools"和 "Development Libraries" “Desktop Platform Development”。如果需要实现地址重写还需要安装pcre-deve包。
  2、编译安装
  # ./configure \   
--prefix=/usr/local/nginx \   
--sbin-path=/usr/local/nginx/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 install
  注意这里安装的路径,在后面的应用中会经常去访问,所以不要忘记,必要时可以保存下来。
  3、为nginx提供启动脚本
  在/etc/rc.d/init.d/下创建服务启动脚本nginx(这里没有用京城使用的xxd,是因为实在是不好看啊 )
  #!/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   
添加服务到开机自动启动。给脚本添加执行权限。然后可以测试使用了。

  可以看到默认的测试页
  三、安装mysql
  1、获取二进制包,解压到/usr/loca/目录下,然后创建软链接mysql
  这里需要特别注意的是:软件包一定要自己机器和系统相匹配,否则是无法使用的。
  tar xf mariadb-10.0.10-linux-x86_64# cd /usr/local/   
# ln -sv mariadb-10.0.10-linux-x86_64# cd mysql
  2、创建mylsql用户组和用户
  二者都创建为系统用户,确保安全,将所有的文件的属组属主都修改为mysql
  # groupadd -r mysql   
# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql   
# chown -R mysql:mysql /mydata/data
  3、创建数据目录
  由于数据库最重要的是数据,因此创建新的逻辑卷,挂载至/mydata/data目录下,以方便后期扩容使用。
  在/mydata目录下创建目录binlogs,存放数据库二进制日志,确保二者的属主和属组都为mysql
  4、安装并初始化mariadb
  # scripts/mysql_install_db --user=mysql --datadir=/mydata/data
  这里要写好数据目录
  5、提供数据库配置文件
  # cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
  如果不想覆盖原来的配置文件,可以创建目录/etc/mysql,将配置文件放置在这个路径下   
    #vim   
    thread_concurrency = 4/改为cpu核心数X2   
    datadir =/mydata/data   
    innodb_file_per_table = ON   
    log-bin=/mydata/binlogs/master-bin
  以上都是必须修改的,十分重要
  6、提供服务脚本
  # cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld   
# chkconfig --add mysqld
  7、导出二进制程序
  vim /etc/profile.d/mysqld.sh   
export PATH=/usr/local/mysql/bin:$PATH   
. /usr/local/mysql/bin:$PATH   

  四、安装php及与数据库相关组件
  php的安装稍显复杂,主要是前面要解决不少依赖关系。这里目前已知需要安装的包有ibcurl-devel bzip2-devel gd-devel libxml2-devel mhash-devel libmcrypt-devel
  1、获取源码包,编译安装
  ./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
  #make && make install   
这里要注意:—enable-fpm,如果没有这一项,后期要使用fpm,需要重新编译,因此要填写此项
  2、为php提供配置文件
  # cp php.ini-production /etc/php.ini
  3、提供服务脚本
  # 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   
# chkconfig php-fpm on
  服务脚本源码包中提供的有,直接使用即可
  4、安装配置fpm
  fpm是fastcgi的具体实现,作为一个服务在系统中运行,因此需要有独立服务脚本,并且要与php结合起来工作。
  为fpm提供服务脚本
  # cp sapi/fpm/init.d.php-fpm/etc/rc.d/init.d/php-fpm
  将其添加到系统服务,开机启动。
  为php-fpm提供配置文件:   
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
  编辑php-fpm的配置文件:   
# vim /usr/local/php/etc/php-fpm.conf   
配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):   
pm.max_children = 150   
pm.start_servers = 8   
pm.min_spare_servers = 5   
pm.max_spare_servers = 10   
pid = /usr/local/php/var/run/php-fpm.pid   
pm.status_path = /status    该选项如果不开启就不能显示状态信息
  这项很重要,忘了选这项,浪费了1个小时排查错误。
  5、启动fpm
  可以通过查看fpm的工作状态,运行在9000端口上
  五、配置xcache
  xcache是给php加速的工具,缓存php编译后的xcode。因此可以大幅度提高速度
  5.1 获取源码,生成configure脚本
# tar xf xcache-3.0.3.tar.bz2
# cd xcache-3.0.3
#xcache原先是不存在的,需要执行php的phpize来生成
# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
  5.2 编译安装

#检查编译环境,开启xcache功能,制定php-config所在路径
# ./configure --enable-xcache -with-php-config=/usr/local/php/bin/php-config
#编译安装
# make && make install
  在最后会有提示信息,生成的模块文件的路径

Installing shared extensions:   /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
  5.3 配置文件
  xcache的配置文件需要php程序来读取,然后cgi进程启动后才会调用相关模块。因此,xcache配置文件其实是在php配置文件中添加内容。这里为了清晰,建立php.d作为php的扩展配置文件目录,拷贝源码包中已经存在的配置文件xcache.ini 然后将生成的xcache模块的绝对路径添加进去,这样就建立起了xcache与php的关联。

# mkdir /etc/php.d
# cp xcache.ini /etc/php.d/
# vim /etc/php.d/xcache.ini
#修改行extension内容为
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so
  配置完成。
  六、整合php和nginx
  1、配置nginx,使其通过fastcgi访问php
  listen       80;
       server_namewww.test1.com;


  location ~ \.php$ {
            root         html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_indexindex.php;
            fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
            include      fastcgi_params;
      }

  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、添加主页支持
  location / {
            root   html;
            indexindex.php index.html index.htm;
      }
  4、在/usr/html新建index.php的测试页面,测试php是否能正常工作:
#vim index.php
  <?php
phpinfo();
?>

  七、配置mamched
  mamched是一个缓存服务器,高性能、分布式内存对象缓存系统,可应用各种需要缓存的场景,其主要目的是通过降低对Database的访问来加速web应用程序。它是一个基于内存的“键值对”存储,用于存储数据库调用、API调用或页面引用结果的直接数据,如字符串、对象等。
  这里,我们将mamched放到单独的服务器上进行部署172.16.37.250服务器上,这里使用编译安装

  1、编译安装
  # tar xf memcache-2.2.7.tgz
  # cd memcache-2.2.7.tgz
  # ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
# make && make install
  2、提供服务脚本
  v im/etc/init.d/memcached
  #!/bin/bash
#
# Init file for memcached
#
# chkconfig: - 86 14
# description: Distributed memory caching daemon
#
# processname: memcached
# config: /etc/sysconfig/memcached
  . /etc/rc.d/init.d/functions
  ## Default variables
PORT=&quot;11211&quot;
USER=&quot;nobody&quot;
MAXCONN=&quot;1024&quot;
CACHESIZE=&quot;64&quot;
OPTIONS=&quot;&quot;
  RETVAL=0
prog=&quot;/usr/local/memcached/bin/memcached&quot;
desc=&quot;Distributed memory caching&quot;
lockfile=&quot;/var/lock/subsys/memcached&quot;
  start() {
      echo -n $&quot;Starting $desc (memcached): &quot;
      daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE -o &quot;$OPTIONS&quot;
      RETVAL=$?
      [ $RETVAL -eq 0 ] && success && touch $lockfile || failure
      echo
      return $RETVAL
}
  stop() {
      echo -n $&quot;Shutting down $desc (memcached): &quot;
      killproc $prog
      RETVAL=$?
      [ $RETVAL -eq 0 ] && success && rm -f $lockfile || failure
      echo
      return $RETVAL
}
  restart() {
      stop
      start
}
  reload() {
      echo -n $&quot;Reloading $desc ($prog): &quot;
      killproc $prog -HUP
      RETVAL=$?
      [ $RETVAL -eq 0 ] && success || failure
      echo
      return $RETVAL
}
  case &quot;$1&quot; in
start)
      start
      ;;
stop)
      stop
      ;;
restart)
      restart
      ;;
condrestart)
      [ -e $lockfile ] && restart
      RETVAL=$?
      ;;      
reload)
      reload
      ;;
status)
      status $prog
      RETVAL=$?
      ;;
   *)
      echo $&quot;Usage: $0 {start|stop|restart|condrestart|status}&quot;
      RETVAL=1
esac
  exit $RETVAL
添加系统自动启动。
  # chkconfig --add memcached
  启动服务
# service memcached start
  通过phpinfo()查看是否启动成功。

  如果有上述显示模块,表示成功。
  八、mamcached的php组件
  mamche是php的组件,安装在php所在的服务器上,与mamcache不在一个服务器上。
  1、编译安装
  # tar xf memcache-2.2.5.tgz
# cd memcache-2.2.5
/usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
# make && make install
  2、配置php配置文件,结合二者
  将上步骤最后显示的内容Inst alling shared extensio ns:   /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
  添加到php.ini的扩展内容中
  extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
  3、验证是否配置成功
  使用测试页测试
  <?php
$mem = new Memcache;
$mem->connect(&quot;172.16.37.250&quot;, 11211)or die(&quot;Could not connect&quot;);
  $version = $mem->getVersion();
echo &quot;Server's version: &quot;.$version.&quot;<br/>\n&quot;;
  $mem->set('hellokey', 'Hello World', 0, 600) or die(&quot;Failed to save data at the memcached server&quot;);
echo &quot;Store data in the cache (data will expire in 600 seconds)<br/>\n&quot;;
  $get_result = $mem->get('hellokey');
echo &quot;$get_result is from memcached server.&quot;;         
?>
  红色部分为memcache所在服务器IP地址。

  如果出现上述页面,说明配置成功。
  总结:使用该方式,服务器能够接受更多的并发请求内容不完善,需后续修改。有误请指出。谢谢
页: [1]
查看完整版本: Linux入门之LNMMP