码迷,mamicode.com
首页 > 其他好文 > 详细

LNMP的的安装

时间:2015-07-03 01:38:37      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

1、先对系统进行更新

yum update -y
lsb_release -a

 2、禁用SELINUX

sed -i ‘/SELINUX/s/enforcing/disabled/‘ /etc/selinux/config

 这一步需要重启服务器

3、安装系统软件以及安装编译器

yum install -y gcc  gcc-c++  autoconf  libjpeg  libjpeg-devel  libpng  libpng-devel  freetype  freetype-devel  libxml2  libxml2-devel  zlib  zlib-devel  glibc  glibc-devel  glib2  glib2-devel bzip2  bzip2-devel  ncurses  ncurses-devel  curl  curl-devel  e2fsprogs  e2fsprogs-devel  krb5  krb5-devel  libidn  libidn-devel  openssl  openssl-devel  openldap  openldap-devel  nss_ldap  openldap-clients  openldap-servers  make  bison  cmake  lsof  rsync  vixie-cron  subversion  pcre  pcre-devel  lrzsz  wget  vim-common  vim-enhanced  ntp  sudo  chkconfig  openssh*   gd gd2 gd-devel gd2-devel

 4、安装Google-perftools (使用tcmalloc 加速 mysql 和 nginx)

centos 》 nginx 》gperftools》libunwind 依赖顺序, 先安装libunwind

cd /usr/local/src

wget  -c http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz >libunwind-1.1.tar.gz
tar zxf libunwind-1.1.tar.gz 
cd libunwind-1.1
./configure --enable-shared
make && make install

 安装google-perftools

cd /usr/local/src

wget -c ftp://ftp.tw.freebsd.org/pub/ports/distfiles/gperftools-2.1.tar.gz
#wget -c http://gperftools.googlecode.com/files/google-perftools-2.1.tar.gz(现在googlecode.com被封了)

tar -vxzf gperftools-2.1.tar.gz
cd gperftools-2.1

./configure --prefix=/usr/local/gperftools --disable-cpu-profiler --enable-shared --disable-heap-profiler --disable-heap-checker --disable-dependency-tracking --enable-frame-pointers
make && make install

 更新,使动态链接库能够被系统共享

/sbin/ldconfig
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
ldconfig

 5、安装mysql (需要 cmake ncurses-devel bison库)

安装ncurses

cd /usr/local/src
wget -c ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.9.tar.gz
tar zxf ncurses-5.9.tar.gz
cd ncurses-5.9
./configure
make && make install

 安装cmake

cd /usr/local/src/
wget -c http://www.cmake.org/files/v3.2/cmake-3.2.1.tar.gz
tar zxf cmake-3.2.1.tar.gz
cd cmake-3.2.1
./bootstrap
make && make install

 安装bison

cd /usr/local/src/
wget -c http://ftp.gnu.org/gnu/bison/bison-3.0.1.tar.gz
tar zvxf bison-3.0.1.tar.gz
cd bison-3.0.1
./configure
make && make install

 创建mysql需要的目录、配置用户和用户组

groupadd mysql
useradd -g mysql mysql
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql

 安装msyql

wget -c http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.25.tar.gz
tar zxf mysql-5.6.25.tar.gz
cd mysql-5.6.25

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DSYSCONFDIR=/etc/ -DWITH_SSL=yes -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=on

make && make install

 配置mysql

ln -s /usr/local/mysql/lib/lib* /usr/lib/
cd /usr/local/mysql
./scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql
cp ./support-files/my-default.cnf /etc/my.cnf

vim /etc/my.cnf
在[client]下添加一行
default-character-set = utf8
在[mysqld]下添加一行
character-set-server = utf8

 

 启动配置

设置mysql开机自动启动服务
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 345 mysqld on

修改服务配置文件
vim /etc/rc.d/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql

启动mysqld服务
service mysqld start

数据库初始化及修改root密码(root初始密码为空)

./bin/mysql_secure_installation

软连接mysql
ln -s /opt/mysql/bin/mysql /bin 

重启centos后,尝试用root连接mysql

mysql -u root -p
status;

使用tcmalloc优化mysql ( 需要安装google-perftools)
修改MySQL启动脚本(根据你的MySQL安装位置而定)

vim /usr/local/mysql/bin/mysqld_safe
在#executing mysqld_safe的下一行,加上:
export LD_PRELOAD=/usr/local/gperftools/lib/libtcmalloc.so

重启服务,查看tcmalloc是否生效 (第二条命令显示即生效)
service mysqld restart
lsof -n | grep tcmalloc

如果显示以下类似的信息,即表示tcmalloc生效

lsof -n|grep tcmalloc
mysqld     1824   mysql  mem       REG      253,0  1853008     801969 /usr/local/gperftools/lib/libtcmalloc.so.4.1.2

 

 

 6、安装Nginx

创建账户

cd /usr/local/src
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
ulimit -SHn 65535

 安装pcre

cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure --prefix=/usr/local/
make && make install

 安装nginx

wget http://nginx.org/download/nginx-1.7.1.tar.gz
tar zxvf nginx-1.7.1.tar.gz
cd nginx-1.7.1

vim ./src/core/nginx.h

修改NGINX_VERSION为你希望显示的版号
修改NGINX_VER为你希望显示的名称
修改NGINX_VAR 为你希望显示的名称

 编译安装

 ./configure --user=www \ 
--group=www \ 
--prefix=/usr/local/nginx \ 
--conf-path=/etc/nginx/nginx.conf \ 
--with-http_ssl_module \ 
--with-http_realip_module \ 
--with-http_addition_module --with-http_gzip_static_module \ 
--with-http_stub_status_module \ 
--with-google_perftools_module \ 
--with-http_secure_link_module \ 
--with-pcre=/usr/local/src/pcre-8.35 \ 
--with-http_realip_module \ 
--with-http_image_filter_module 
make && make install

 配置\修改 nginx.conf ,令nginx可以google-perftools实现加速

vim /etc/nginx/nginx.conf

修改前面几行为:

user www www;
worker_processes 8;#这个要看你的处理器的数量了
error_log logs/error.log crit;
pid logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc/;
events{
  use epoll;
  worker_connections 65535;
}

测试和运行

cd /usr/local/nginx/
./sbin/nginx -t

 如果显示下面信息,即表示配置没问题
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful


输入代码运行nginx服务

./sbin/nginx
ps au|grep nginx

 如果显以类似下面的信息,即表示nginx已经启动
root     30038  0.0  0.0   5980   740 pts/0    S+   00:12   0:00 grep nginx

输入代码检测是否支持加速

lsof -n | grep tcmalloc

如果显示类似下面的信息,即表示支持tcmalloc加速 (mysqld和nginx两个线程都支持)

mysqld    8297 mysql  mem       REG      253,0  1853334     274527 /usr/local/gperftools/lib/libtcmalloc.so.4.1.2
nginx     8353   www    9w      REG      253,0        0     278727 /tmp/tcmalloc/.8353
nginx     8354   www   11w      REG      253,0        0     278703 /tmp/tcmalloc/.8354
nginx     8355   www   13w      REG      253,0        0     278675 /tmp/tcmalloc/.8355
nginx     8356   www   15w      REG      253,0        0     278701 /tmp/tcmalloc/.8356
nginx     8357   www   17w      REG      253,0        0     278702 /tmp/tcmalloc/.8357
nginx     8358   www   19w      REG      253,0        0     278737 /tmp/tcmalloc/.8358
nginx     8359   www   21w      REG      253,0        0     278736 /tmp/tcmalloc/.8359
nginx     8360   www   23w      REG      253,0        0     278713 /tmp/tcmalloc/.8360

打开防火墙80端口、写入规则,保存并重启服务

iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/etc/rc.d/init.d/iptables save
/etc/init.d/iptables restart

 查看防火墙信息

/etc/init.d/iptables status
如果显示以下类似信息,即表示已经打开了80端口
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80

 编写nginx 启动服务
vim /etc/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="/opt/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    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
killall -9 nginx
}

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

 保存后,设置权限,并添加到启动服务列表中

chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig --level 345 nginx on
service nginx start

 7、安装PHP

安装php依赖库jpeg

cd /usr/local/src
wget http://www.ijg.org/files/jpegsrc.v9.tar.gz    
tar zxvf jpegsrc.v9.tar.gz
cd jpeg-9/
./configure --prefix=/data/app/libs --enable-shared --enable-static --prefix=/usr/local
make && make install

 

 png

wget http://prdownloads.sourceforge.net/libpng/libpng-1.6.2.tar.gz
tar zxvf libpng-1.6.2.tar.gz
cd libpng-1.6.2/
./configure --prefix=/usr/local
make && make install

 freetype

wget http://download.savannah.gnu.org/releases/freetype/freetype-2.4.12.tar.gz
tar zxvf freetype-2.4.12.tar.gz
cd freetype-2.4.12/
./configure --prefix=/usr/local
make && make install

 libmcrypt

wget http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure --prefix=/usr/local
make && make install

 libtool

wget http://ftpmirror.gnu.org/libtool/libtool-2.4.6.tar.gz
tar zxf libtool-2.4.6.tar.gz
cd libtool-2.4.6
./configure --prefix=/usr/local --enable-ltdl-install
make && make install

 mhash

cd /usr/local/src
wget http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure --prefix=/usr/local
make && make install

 mcrypt

cd /usr/local/src
wget http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
export LDFLAGS="-L/usr/local/lib -L/usr/lib"
export CFLAGS="-I/usr/local/include -I/usr/include"
touch malloc.h
./configure --prefix=/usr/local --with-libmcrypt-prefix=/usr/local
make && make install

 编译安装PHP 5.5

cd /usr/local/src
wget http://cn2.php.net/get/php-5.5.23.tar.gz
tar zxvf php-5.5.23.tar.gz
cd php-5.5.23/
export LIBS="-lm -ltermcap -lresolv"
export DYLD_LIBRARY_PATH="/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"
export LD_LIBRARY_PATH="/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"

 编译

./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir --with-freetype-dir=/usr/local/lib  --with-jpeg-dir=/usr/local/lib  --with-png-dir=/usr/local/lib --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt=/usr/local/lib --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl 
--enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-opcache --with-pdo-mysql --enable-embed=shared --enable-debug --enable-dtrace 

 安装&&配置

make &&& make install
cp php.ini-development /etc/php.ini

 配置php-fpm

ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

 

vim /usr/local/php/etc/php-fpm.conf
#修改以下地方
[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 5s
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_children = 35

[www]
user = www
group = www

 修改nginx,支持php

vim /etc/nginx/nginx.conf
找到并修改以下代码

location ~ \.php$ {
            root           /data/www;你的文档目录
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

 将php-fpm

cd  /usr/local/src/php-5.5.23
cp ./sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm
chmod 700 /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig --level 345 php-fpm on
服务方式启动php-fpm
service php-fpm restart

编写测试页面

vim /data/www/index.php
输入代码
<html>
  <head><title>hello php</title></head>
  <body>
    <?php phpinfo();?>
  </body>
</html>

 编译安装PHP扩展

autoconf

cd /usr/local/src
wget http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
tar zxvf autoconf-latest.tar.gz
cd autoconf-2.69/
./configure --prefix=/usr/local
make && make install

 memcache

cd /usr/local/src
wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar zxvf memcache-2.2.7.tgz
cd memcache-2.2.7/
export PHP_AUTOCONF=" /usr/local/bin/autoconf"
export PHP_AUTOHEADER=" /usr/local/bin/autoheader"
 /usr/local/php/bin/phpize
./configure --with-php-config= /usr/local/php/bin/php-config
make && make install

 

vim /etc/php.ini 

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20121212"
extension = "memcache.so"

开启opcache扩展
[opcache]
zend_extension = "opcache.so"
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

 

8、Linux内核优化&&Nginx缓存优化




 

LNMP的的安装

标签:

原文地址:http://www.cnblogs.com/chenpingzhao/p/4603437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!