IT技术互动交流平台

初接触Linux LNMMP服务安装与构建

作者:王丶Blue  发布日期:2014-04-28 08:32:14

今天给大家带来的是LNMMP服务


一.LNMMP

什么是LNMMP?

LNMMP是相当于一个组合的名字,就像Beyond!每个人都是单独的个体,但组合起来就不是简单的1+1层次的问题。

LNMMP(Linux Nginx Memcached PHP/Perl/Python)的一套组合构架。主要是实现互联网WEB服务的一套组建,和之前说过的LAMP有着很大的相同性,且又有从根本上的区别!!

二.安装各个组件

安装Nginx

①安装前需要的程序开发组建

yum -y groupinstall "Development Tools" "Server Platform Deveopment"
yum -y install openssl-devel pcre-devel

安装前创建nginx用户和nginx组,因为创建用户时会直接以相同的名字创建一个组,所以不用再单独创建一个组。

 

useradd -r -g nginx nginx

③解压nginx包,并移动到解压的文件中

tar xf nginx-1.4.7.tar.gz
cd nginx-1.4.7

④执行编译命令

./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
--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 pid文件--lock-path=/var/lock/nginx.lock 锁文件 --user=nginx 运行程序所使用的用户--group=nginx 运行程序的所以用的组--with-http_ssl_module 启用SSL认证模块--with-http_flv_module 启用流媒体模块--with-http_gzip_static_module 启用静态页面压缩 --http-client-body-temp-path=/var/tmp/nginx/client/HTTP包体--http-proxy-temp-path=/var/tmp/nginx/proxy/临时文件--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/接收到FastCGI服务器数据,并临时存放的目录

⑤添加Nginx脚本

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

⑥加入自动开机启动列表和验证,并启动服务

chomd +x /etc/rc.d/init.d/nginx
chmod +x /etc/rc.d/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
chkconfig --list |grep nginx
nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off
service nginx start
Starting nginx:                                            [  OK  ]

安装MariaDB

①安装前依所需要的包组

yum install -y cmake

②添加mysql用户

useradd -r -g mysql mysql

③解压并安装

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mari -DMYSQL_DATADIR=/mariadb/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
make && make install

④创建数据库目录及改变权限

mkdir -p /mariadb/data
chown -R mysql.mysql /mariadb/ 
cd /usr/local/
chown -R mysql.mysql mari/

⑤添加MariaDB的服务脚本

cd /usr/local/maria/
cp support-files/mysql.server /etc/rc.d/init.d/maria
chmod +x /etc/rc.d/init.d/maria
chkconfig --add maria
chkconfig maria on
chkconfig --list |grep maria
maria            0:off   1:off   2:on    3:on    4:on    5:on    6:off

⑦修改其配置文件

cp support-files/my-large.cnf /etc/my.cnf
vim /etc/my.cnf
grep -v "#" /etc/my.cnf |grep -v "^$"
[client]
port        = 3306
socket      = /tmp/mysql.sock
[mysqld]
port        = 3306
socket      = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 4
datadir=/mariadb/data
log-bin=mysql-bin
binlog_format=mixed
server-id   = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout

⑧初始化,导出库文件,变量及帮助手册

scripts/mysql_install_db --user=mysql --datadir=/mariadb/data/ 
#echo "export PATH=/usr/local/maria/bin:$PATH" >/etc/profile.d/mysql.sh
#source /etc/profile.d/mysql.sh
#echo "MANPATH /usr/local/maria/man" >> /etc/man.config
#ln -s lib /usr/include/mysql

⑨启动MariaDB服务

service mari start
Starting MySQL....................                         [  OK  ]

安装PHP和Xcache

①安装前所需要解决的包组

yum install bzip2-devel -y

②解决依赖关系

wget http://xmlsoft.org/sources/libxml2-2.9.0.tar.gz
tar xf libxml2-2.9.0.tar.gz
cd libxml2-2.9.0
./configure --prefix=/usr/local/libxml
make && make install
wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz
tar xf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure
make && make install

③解压编译PHP

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

④添加脚本并设置开机自动启动

cp php.ini-production /etc/php.ini
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 --list |grep php-fpm
php-fpm         0:off   1:off   2:on    3:on    4:on    5:on    6:off

⑤编辑配置文件

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
#vim /usr/local/php/etc/php-fpm.conf
[root@essun php-5.4.26]# grep -v "^;" /usr/local/php/etc/php-fpm.conf |grep -v "^$"
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = log/php-fpm.log
process.max = 128
[www]
user = nobody
group = nobody
listen = 192.168.1.107:9000
listen.backlog = 128
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 6

⑥启动php-fpm程序

service php-fpm start
Starting php-fpm  done
ss -tnl |grep 9000
LISTEN     0      128           192.168.1.107:9000                     *:*

⑦安装Xcache

tar xf xcache-3.1.0.tar.bz2
cd xcache-3.1.0
 usr/local/php/bin/phpize
./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
 make && make install
#将最后一行生成的类似“/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/”复制到后面的xcache.ini文件中
mkdir /etc/php.d
cp xcache.ini /etc/php.d/
vim /etc/php.d/xcache.ini
[xcache-common]
;; non-Windows example:
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so
#如果有多个"extension =",请将复制的记录放在第一条!

⑧修改Nginx配置文件

vim /etc/nginx/nginx.conf
grep -v "#" /etc/nginx/nginx.conf |grep -v "^$"
worker_processes  1;
error_log  logs/error.log  notice;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.php  index.html index.htm;
        }
        error_page  404              /404.html;
        location = /404.html {
            root   html;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   192.168.1.107:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
         }
    }
}

⑨写一个验证的网页页面

#eradd -r -g nginx nginx
#dir -p /usr/html
chown -R nginx.nginx /usr/html
vim /usr/html/index.php
<?php
    phpinfo();
?>

⑩重启php-fpm程序

service php-fpm restart

①①测试与数据库的链接

vim /usr/html/index.php
<?php
     $link = mysql_connect('192.168.9.31','root','ctrlaltdel');
     if ($link)
       echo "Success...";
     else
       echo "Failure...";
     mysql_close();
   ?>

①②下载wordpress,解压并安装

 

tar xf wordpress-3.9-zh_CN.tar.gz -C /usr/html/
cd /usr/html/wordpress
cp wp-config-sample.php wp-config.php
vim wp-config.php
<?php
/**
 * WordPress基础配置文件。
 *
 * 本文件包含以下配置选项:MySQL设置、数据库表名前缀、密钥、
 * WordPress语言设定以及ABSPATH。如需更多信息,请访问
 * {@link http://codex.wordpress.org/zh-cn:%E7%BC%96%E8%BE%91_wp-config.php
 * 编辑wp-config.php}Codex页面。MySQL设置具体信息请咨询您的空间提供商。
 *
 * 这个文件被安装程序用于自动生成wp-config.php配置文件,
 * 您可以手动复制这个文件,并重命名为“wp-config.php”,然后填入相关信息。
 *
 * @package WordPress
 */
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');
/** MySQL数据库用户名 */
define('DB_USER', 'root');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'ctrlaltdel');
/** MySQL主机 */
define('DB_HOST', '192.168.9.31');
......省略中........

安装Memcached

①先安装所需要的包组

 

yum install -y libevent-devel

②编译安装

tar xf memcached-1.4.17.tar.gz
cd memcached-1.4.17
./configure --prefix=/usr/local/memcached
make && make install

③更新二进制,帮助文件及库文件

echo "export PATH=/usr/local/memcached/bin:$PATH" > /etc/profile.d/memcache.sh
cat /etc/profile.d/memcache.sh
export PATH=/usr/local/memcached/bin:/usr/local/mysql/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
. /etc/profile.d/memcache.sh
/share/man" >> /etc/man.config
ln -s include/memcached /usr/include/memcache

④启动Memcached

useradd -r memcached
memcached -u memcached

⑤为php安装扩展memcache模块

 

tar xf memcache-2.2.7.tgz
cd memcache-2.2.7
/usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
make && make install

⑥编辑测试页面

vim /usr/html/wordpress/testmemcached.php
<?php
$mem = new memcache;
$mem->connect("192.168.9.21", 11211)  or die("Could not connect");
$version = $mem->getVersion();
echo "Server's version: ".$version."<br/>\n";
$mem->set('hellokey', 'Hello World', 0, 600) or die("Failed to save data at the memcached server");
echo "Store data in the cache (data will expire in 600 seconds)<br/>\n";
$get_result = $mem->get('hellokey');
echo "$get_result is from memcached server.";
?>

⑦查看刚才的动作是否被缓存

telnet 127.0.0.1 11211

安装Memadmin

tar xf memadmin-1.0.12.tar.gz
cd memadmin
mv memadmin /usr/html/

打开页面,点击开始管理就能看到监控memcached的一切数据,而且还是中文界面的呦!!!

Tag标签: Linux   LNMMP  
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规