gcc 4.8 安装

curl -Lks http://www.hop5.in/yum/el6/hop5.repo > /etc/yum.repos.d/hop5.repo
yum install gcc gcc-g++
gcc --version

gcc 4.9 安装

yum install centos-release-scl
yum install devtoolset-3-toolchain
scl enable devtoolset-3 bash
gcc --version

gcc 5.2 安装

yum install centos-release-scl
yum install devtoolset-4-toolchain
scl enable devtoolset-4 bash
gcc --version

scl enable devtoolset-3 bash只是临时覆盖系统原有的GCC引用,如果想永久覆盖,可在root/.bashrc文件中添加source /opt/rh/devtoolset-3/enable

文章来自参考:https://www.dwhd.org/20160724_085212.html

gcc 4.8 安装

curl -Lks http://www.hop5.in/yum/el6/hop5.repo > /etc/yum.repos.d/hop5.repo
yum install gcc gcc-g++
gcc --version

gcc 4.9 安装

yum install centos-release-scl
yum install devtoolset-3-toolchain
scl enable devtoolset-3 bash
gcc --version

gcc 5.2 安装

yum install centos-release-scl
yum install devtoolset-4-toolchain
scl enable devtoolset-4 bash
gcc --version

scl enable devtoolset-3 bash只是临时覆盖系统原有的GCC引用,如果想永久覆盖,可在root/.bashrc文件中添加source /opt/rh/devtoolset-3/enable

文章来自参考:https://www.dwhd.org/20160724_085212.html

实测有效方法:压缩你的镜像

方法:试着用命令或工具压缩你的镜像。

docker 自带的一些命令还能协助压缩镜像,比如 export 和 import

$ docker run -d redis:lab-3
$ docker export 71b1c0ad0a2b | docker import - redis:lab-4

但麻烦的是需要先将容器运行起来,而且这个过程中你会丢失镜像原有的一些信息,比如:导出端口,环境变量,默认指令。

 

20161027203227

介绍

前段时间网易蜂巢曾经推出蜂巢 Logo T恤,用的正是 Docker 镜像制作,最神奇的是,它最终的镜像大小只有 585字节。

$ docker images | grep hub.c.163.com/public/logo

REPOSITORY                          TAG     IMAGE ID           CREATED      SIZE

hub.c.163.com/public/logo  latest  6fbdd13cd204  11 days ago  585 B

这其中就用到了不少精简镜像的技术,尤其是针对 C 程序的优化和精简。但我们平常开发肯定不止用 C 语言,甚至有些镜像都不是我们自己来打包的(比如下载公共镜像),那是否有一些通用的精简 Docker 镜像的手段呢? 答案是肯定的 ,甚至有的镜像可以精简 98%。精简镜像大小的好处不言而喻,既节省了存储空间,又能节省带宽,加快传输。那好,接下来就请跟随我来学习怎么一步步精简 Docker 镜像吧。

镜像层(Layers)

在开始制作镜像之前,首先了解下镜像的原理,而这其中最重要的概念就是镜像层(Layers)。镜像层依赖于一系列的底层技术,比如文件系统(filesystems)、写时复制(copy-on-write)、联合挂载(union mounts)等,幸运的是你可以在很多地方学习到这些技术,这里就不再赘述技术细节。

 

20161027203243

 

总的来说,你最需要记住这点:

在 Dockerfile 中, 每一条指令都会创建一个镜像层,继而会增加整体镜像的大小。

举例来说:

FROM busybox

RUN mkdir /tmp/foo

RUN dd if=/dev/zero of=/tmp/foo/bar bs=1048576 count=100

RUN rm /tmp/foo/bar

以上 Dockerfile 干了这几件事:

  • 基于一个官方的基础镜像 busybox(只有1M多)
  • 创建一个文件夹(/tmp/foo)和一个文件(bar)
  • 为该文件分配了100M大小
  • 再把这个大文件删除

事实上,它最终什么也没做,我们把它构建成镜像看看(构建可以参考一期):

docker build -t busybox:test .

再让我们来对比下原生的 busybox 镜像大小和我们生成的镜像大小:

$ docker images | grep

busyboxbusybox    test     896c63dbdb96    2 seconds ago    106 MB

busybox    latest   2b8fd9751c4c    9 weeks ago      1.093 MB

出乎意料的是,却生成了 106 MB 的镜像。

多出了 100 M,这是为何?这点和 git 类似(都用到了Copy-On-Write技术),我用 git 做了如下两次提交(添加了又删除),请问 A_VERY_LARGE_FILE 还在 git 仓库中吗?

$ git add  A_VERY_LARGE_FILE

$ git commit

$ git rm  A_VERY_LARGE_FILE

$ git commit

答案是: 在的 ,并且会占用仓库的大小。Git 会保存每一次提交的文件版本,而 Dockerfile 中每一条指令都可能增加整体镜像的大小,即使它最终什么事情都没做。

精简步骤

了解了镜像层知识,有助于我们接下来制作精简镜像。这里开始,以最常用的开源缓存软件 Redis 为例,从一步步试验,来介绍如何制作更精简的 Docker 镜像

步骤 1:初始化构建 Redis 镜像

直接上 Dockerfile :

FROM ubuntu:trusty

ENV VER     3.0.0

ENV TARBALL http://download.redis.io/releases/redis-$VER.tar.gz

# ==> Install curl and helper tools...

RUN apt-get update

RUN apt-get install -curl make gcc

# ==> Download, compile, and install...

RUN curl -L $TARBALL | tar zxv

WORKDIR  redis-$VER

RUN make

RUN make install

#...

# ==> Clean up...

WORKDIR /

RUN apt-get remove -y --auto-remove curl make gcc

RUN apt-get clean

RUN rm -rf /var/lib/apt/lists/*  /redis-$VER

#...

CMD ["redis-server"]

结合注释,读起来并不困难,用到的都是常规的几个命令,简要介绍如下:

  • FROM:顶头写,指定一个基础镜像,此处基于 ubuntu:trusty
  • ENV:设置环境变量,这里设置了 VER 和 TARBALL 两个环境变量
  • RUN:最常用的 Dockerfile 指令,用于运行各种命令,这里调用了 8 次 RUN 指令
  • WORKDIR:指定工作目录,相当于指令 cd
  • CMD:指定镜像默认执行的命令,此处默认执行 redis-server 命令来启动 redis

执行构建:

$ docker build  -t redis:lab-1  .

注:国内网络,更新下载可能会较慢

查看大小:

 

20161027203303

 

动辄就有 300多 M 的大小,不能忍,下面我们开始一步步优化。

步骤 2: 优化基础镜像

方法:选用更小的基础镜像。

常用的 Linux 系统镜像一般有 ubuntu、centos、debian,其中debian 更轻量,而且够用,对比如下:

REPOSITORY     TAG        IMAGE ID           VIRTUAL SIZE

---------------           ------          ------------                ------------

centos              7               214a4932132a     215.7 MB

centos              6               f6808a3e4d9e      202.6 MB

ubuntu              trusty       d0955f21bf24      188.3 MB

ubuntu              precise    9c5e4be642b7     131.9 MB

debian              jessie       65688f7c61c4      122.8 MB

debian              wheezy    1265e16d0c28      84.96 MB

替换 debian:jessie 作为我们的基础镜像。

优化 Dockerfile:

FROM debian:jessie

#...

执行构建:

$ docker build  -t redis:lab-2  .

查看大小:

 

20161027203254

 

减少了42M,稍有成效,但并不明显。细心的同学应该发现,只有 122 MB 的 debian 基础镜像,构建后增加到了 305 MB,看来这里面肯定有优化的空间,如何优化就要用到我们开头说到的 Image Layer 知识了。

步骤 3:串联 Dockerfile 指令

方法: 串联你的 Dockerfile 指令(一般是 RUN 指令)。

Dockerfile 中的 RUN 指令通过 && 和 / 支持将命令串联在一起,有时能达到意想不到的精简效果。

优化 Dockerfile:

FROM debian:jessie

ENV VER     3.0.0

ENV TARBALL http://download.redis.io/releases/redis-$VER.tar.gz




RUN echo "==> Install curl and helper tools..."  && \     apt-get update                      && \   

  apt-get install -curl make gcc   && \  

  \   

  echo "==> Download, compile, and install..."  && \  

  curl -L $TARBALL | tar zxv  && \   

  cd redis-$VER               && \   

  make                        && \   

  make install                && \   

  ...   

  echo "==> Clean up..."  && \   

  apt-get remove -y --auto-remove curl make gcc  && \

  apt-get clean                                  && \   

  rm -rf /var/lib/apt/lists/*  /redis-$VER




#...

CMD ["redis-server"]

构建:

$ docker build  -t redis:lab-3  .

查看大小:

 

20161027203318

 

哇!一下子减少了 50%,效果明显啊!这是最常用的一个精简手段了。

步骤 4:压缩你的镜像

方法:试着用命令或工具压缩你的镜像。

docker 自带的一些命令还能协助压缩镜像,比如 export 和 import

$ docker run -d redis:lab-3

$ docker export 71b1c0ad0a2b | docker import - redis:lab-4

但麻烦的是需要先将容器运行起来,而且这个过程中你会丢失镜像原有的一些信息,比如:导出端口,环境变量,默认指令。

所以一般通过命令行来精简镜像都是实验性的,那么这里再推荐一个小工具: docker-squash。用起来更简单方便,并且不会丢失原有镜像的自带信息。

下载安装:

https://github.com/jwilder/docker-squash#installation(复制此链接到浏览器打开)

压缩操作:

$ docker save redis:lab-3 \ 

  | sudo docker-squash -verbose -t redis:lab-4  \ 

  | docker load

注: 该工具在 Mac 下并不好使,请在 Linux 下使用

对比大小:

 

20161027203328

 

好吧,从这里看起来并没有太大作用,所以我只能说试着,而不要报太大期望。

总结

本期我们介绍了镜像层的知识,并且通过实验,介绍三种如何精简镜像的技巧(下期还有更强大的技巧)。这里主要介绍了三种精简方法:选用更精小的镜像,串联 Dockerfile 运行指令,以及试着压缩你的镜像。通过这几个技巧,已经可以将 300M 大小的镜像压缩到 150M,压缩率50%,效果还是不错。但这还远远不够,下篇我们将介绍一些终极手段,压缩效果可以达到 98%哦!

来自官方手册:https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora

Including Red Hat® Enterprise Linux® / RHEL, CentOS and Fedora.

Node.js is available from the NodeSource Enterprise Linux and Fedora binary distributions repository. Support for this repository, along with its scripts, can be found on GitHub at nodesource/distributions.

Note that the Node.js packages for EL 5 (RHEL5 and CentOS 5) depend on the EPEL repository being available. The setup script will check and provide instructions if it is not installed.

Run as root on RHEL, CentOS or Fedora, for Node.js v6 LTS:

curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -

Alternatively for Node.js v7:

curl --silent --location https://rpm.nodesource.com/setup_7.x | bash -

Alternatively for Node.js 0.10:

curl --silent --location https://rpm.nodesource.com/setup | bash -

Then install, as root:

yum -y install nodejs

转:http://www.cnblogs.com/vicowong/archive/2011/12/01/2116212.html

一、CentOS 6安装

1.1 使用VMware 虚拟机进行安装,进行安装界面

(分配内存必须大于1G,否则不会显示图型安装界面,网络设置使用“桥接模式” 即"Bridged"模式)

1.2 选择 Install or upgrade an existing system

1.3 在"Disc found" 框 选择 "skip"

1.4 next 选择 "chinese(simplified)(中文(简介 )) next

1.5 选择 "美国英语式" 下一步

1.6 选择 "基本 存储设备" 下一步

1.7 弹出"警告"框时,选择"重新初始化所有"

1.8 主机名可以保留默认,点击”配置网络“ 弹出“网络连接” 双击“System eth0"

1.9 弹出“正在编辑 System eth0" 选择"自动连接" 点击"应用“ ,点击”关闭“ 关闭”网络连接“框 下一步

1.10 不要选择“系统时钟使用UTC时间” 下一步

1.11 输入并确认 ”根密码“ 下一步

1.12 选择"替换现有Linux系统 " 下一步 “将修改写入磁盘"

1.13 选择"Basic Server" 下一步

1.14 大概一共590个软件包,复制安装完成后,点击“重新引导”,重新启动计算机

1.15 (安装完成后,可以将虚拟机内存由1G,改为512M)

 

二、更新centos6

2.1 更新

yum update -y

2.2 查看版本,确认为"CentOS Linux release 6.0 (Final)"

lsb_release -a

2.2 安装编译器

yum install -y gcc gcc-c++

 

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

3.1下载需要的文件

下载 libunwind-1.0.1.tar.gz 到 /usr/local/data-original

3.2 安装libunwind

cd /usr/local/data-original/

tar zvxf libunwind-1.0.1.tar.gz

cd libunwind-1.0.1

./configure --enable-shared

make && make install

3.3 安装google-perftools

cd /usr/local/data-original/

tar zvxf google-perftools-1.8.3.tar.gz

cd google-perftools-1.8.3

./configure --enable-shared --enable-frame-pointers

make && make install

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

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

ldconfig

 

四、安装mysql

4.1.下载文件

下载 ncurses-5.9.tar.gz到/usr/local/data-original
下载 bison-2.5.tar.gz到/usr/local/data-original
下载 cmake-2.8.6.tar.gz到/usr/local/data-original
下载 mysql-5.5.18.tar.gz到/usr/local/data-original

4.2 安装ncurses

yum install ncurses-devel -y

cd /usr/local/data-original/

tar zvxf ncurses-5.9

./configure

make && make install

4.3 安装cmake

cd /usr/local/data-original/

tar zvxf cmake-2.8.6.tar.gz

cd cmake-2.8.6

./bootstrap

make && make install

4.4 安装bison

cd /usr/local/data-original/

tar zvxf bison-2.5.tar.gz

cd bison-2.5

./configure

make && make install

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

groupadd mysql

useradd -g mysql mysql

mkdir -p /data/mysql

chown -R mysql:mysql /data/mysql

4.6.安装mysql (需要 cmake ncurses-devel bison 库)

4.6.1 安装

cd /usr/local/data-original/

tar zvxf mysql-5.5.18.tar.gz

cd mysql-5.5.18

cmake -DCMAKE_INSTALL_PREFIX=/opt/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

4.6.2 创建软连接

ln -s /opt/mysql/lib/lib* /usr/lib/

4.6.3 配置mysql数据库

cd /opt/mysql
./scripts/mysql_install_db --basedir=/opt/mysql/ --datadir=/data/mysql/ --user=mysql

4.6.4 复制配置文件

cp ./support-files/my-large.cnf /etc/my.cnf

4.6.5 修改配置文件,设置默认使用utf8编码

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

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

4.6.7 修改服务配置文件
vim /etc/rc.d/init.d/mysqld

根据设定需要,修改mysqld文件中的下面两项
basedir=/opt/mysql
datadir=/data/mysql

4.6.8 启动mysqld服务
service mysqld start

4.6.9 数据库初始化及修改root密码(root初始密码为空)
./bin/mysql_secure_installation
根据提示操作

4.6.10 软连接mysql

ln -s /opt/mysql/bin/mysql /bin

4.6.11 重启centos后,尝试用root连接mysql
mysql -u root -p

成功登录后查看状态

status;

4.6.12 使用tcmalloc优化mysql ( 需要安装google-perftools)

修改MySQL启动脚本(根据你的MySQL安装位置而定)
vim /opt/mysql/bin/mysqld_safe

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

4.6.13 重启服务,查看tcmalloc是否生效 (第二条命令显示即生效)

service mysqld restart

lsof -n | grep tcmalloc

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

[root@localhost mysql]# lsof -n|grep tcmalloc
mysqld 30347 mysql mem REG 253,0 2177570 544322 /usr/local/lib/libtcmalloc.so.0.2.2

 

五、安装Nginx

5.1.准备安装

下载 pcre-8.20.tar.gz到/usr/local/data-original

下载 nginx-1.0.10.tar.gz到/usr/local/data-original

5.2 更新包

yum install zlib* openssl* -y

 

5.3 安装Pcre

cd /usr/local/data-original/

tar zvxf pcre-8.20.tar.gz

cd pcre-8.20

./configure

make && make install

5.4 创建www用户和组,创建www虚拟主机使用的目录,以及Nginx使用的日志目录,并且赋予他们适当的权限

groupadd www
useradd -g www www

mkdir -p /data/www
chmod +w /data/www
chown -R www:www /data/www

为tcmalloc添加目录,并且赋予适当权限

mkdir -p /tmp/tcmalloc/

chown -R www:www /tmp/tcmalloc/

 

5.1 安装Nginx (需要 pcre google-perftools 库)

5.5.1 安装

cd /usr/local/data-original/

tar zvxf nginx-1.0.10.tar.gz

伪装服务器信息(可以不修改)

cd nginx-1.0.10/data-original/core

vim ./data-original/core/nginx.h

修改NGINX_VERSION为你希望显示的版号

修改NGINX_VER为你希望显示的名称

修改NGINX_VAR 为你希望显示的名称

保存

开始安装

./configure --user=www --group=www --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-google_perftools_module

make && make install

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

vim /opt/nginx/conf/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;
}

 

5.4.3 测试和运行

cd /opt/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 2013 0.0 0.0 103156 856 pts/0 S+ 03:22 0:00 grep nginx

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

lsof -n | grep tcmalloc

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

mysqld 20818 mysql mem REG 253,0 2177570 281050 /usr/local/lib/libtcmalloc.so.0.2.2

nginx 29454 www 25w REG 253,0 0 288399 /tmp/tcmalloc/.29454

nginx 29455 www 27w REG 253,0 0 288403 /tmp/tcmalloc/.29455

 

5.5.4 打开防火墙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

5.5.5 编写nginx 启动服务

cd /etc/init.d

vim 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



六、安装PHP

6.1 准备安装

下载php-5.3.6.tar.gz到/usr/local/data-original
下载 libiconv-1.14.tar.gz到/usr/local/data-original
下载 libmcrypt-2.5.8.tar.gz到/usr/local/data-original
下载mcrypt-2.6.8.tar.gz到/usr/local/data-original
下载mhash-0.9.9.9.tar.gz到/usr/local/data-original
下载libmcrypt-2.5.8.tar.gz到/usr/local/data-original

yum -y install 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 libXpm* gcc gcc-c++

 

5.2 安装libiconv (加强系统对支持字符编码转换的功能)

cd /usr/local/data-original/

tar zvxf libiconv-1.14.tar.gz

cd libiconv-1.14

./configure --prefix=/usr/local

make && make install

5.3 安装libmcrypt(加密算法库,PHP扩展mcrypt功能对此库有依耐关系,要使用mcrypt必须先安装此库)

5.3.1 安装libmcrypt

cd /usr/local/data-original/

tar zvxf libmcrypt-2.5.8.tar.gz

cd libmcrypt-2.5.8

./configure

make && make install

5.3.2安装libltdl

cd libltdl/

./configure --enable-ltdl-install

make && make install

5.3.3 更新共享

ln -sf /usr/local/lib/libmcrypt.* /usr/lib64/
ln -sf /usr/local/bin/libmcrypt-config /usr/lib64/
#ln -sf /usr/local/lib/libiconv.so.2 /usr/lib64/

ldconfig

5.4 安装mhash(hash加密算法库)

5.4.1 安装mhash

cd /usr/local/data-original/

tar zvxf mhash-0.9.9.9.tar.gz

cd mhash-0.9.9.9

./configure

make && make install

5.4.2更新共享

ln -sf /usr/local/lib/libmhash.* /usr/lib64/

ldconfig

5.5 安装mcrypt

cd /usr/local/data-original/

tar zvxf mcrypt-2.6.8.tar.gz

cd mcrypt-2.6.8

ldconfig

./configure

make && make install

5.6 安装php

5.6.1 创建mysql软连接、ldap软连接

mkdir -p /opt/mysql/include/mysql
ln -s /opt/mysql/include/* /opt/mysql/include/mysql/

ln -s /usr/lib64/libldap* /usr/lib

5.6.2 安装

cd /usr/local/data-original/

tar zvxf php-5.3.8.tar.gz

cd php-5.3.8

./configure --prefix=/opt/php --with-config-file-path=/opt/php/etc --with-mysql=/opt/mysql --with-mysqli=/opt/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --disable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap

make ZEND_EXTRA_LIBS='-liconv'

make install

5.6.3 复制配置文件

cp php.ini-production /opt/php/etc/php.ini

5.6.4 安装memcache扩展(已经安装PHP)

cd /usr/local/data-original/

tar zvxf memcache-2.2.6.tar.gz

cd memcache-2.2.6

/opt/php/bin/phpize

ldconfig

./configure --with-php-config=/opt/php/bin/php-config

make && make install

修改php配置文件,支持memcache

vim /opt/php/etc/php.ini

在文件中搜索extension_dir、extension ,在相应位置添加下面两行
extension_dir = "/opt/php/lib/php/extensions/no-debug-non-zts-20090626/"
extension = "memcache.so"

5.6.5 安装eaccelerator扩展(已经安装PHP)

cd /usr/local/data-original/

tar jxvf eaccelerator-0.9.6.1.tar.bz2

cd eaccelerator-0.9.6.1

/opt/php/bin/phpize

./configure --enable-eaccelerator=shared --with-php-config=/opt/php/bin/php-config

make && make install

修改php配置文件,支持eaccelerator

vim /opt/php/etc/php.ini

在文件尾加入以下代码

[eaccelerator]
zend_extension="/opt/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so"
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.log_file = "/opt/php/var/log/eaccelerator_log"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

增加eaccelerator目录
mkdir -p /tmp/eaccelerator
5.6.5 安装php-fpm

cp /opt/php/etc/php-fpm.conf.default /opt/php/etc/php-fpm.conf
vim /opt/php/etc/php-fpm.conf

修改以下地方
[global]
pid = run/php-fpm.pid-p
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

[www]
user = www
group = www

5.6.6 修改nginx,支持php

vim /opt/nginx/conf/nginx.conf

找到并修改以下代码

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
include fastcgi_params;
}

 

5.6.7将php-fpm 作为服务运行

cd /usr/local/data-original/php-5.3.8
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

 

5.6.8 编写测试页面

vim /data/www/index.php

输入代码

<html>
<head><title>hello php</title></head>
<body>
<?php phpinfo();?>
</body>
</html>

5.6.8 打开浏览器进行测试

转:http://www.myhack58.com/Article/sort099/sort0102/2011/29472.htm

一、更改yum源为网易的源加快速度

vi /etc/yum.repos.d/CentOS-Base.repo
更改内容如下

# CentOS-Base.repo
#
# This file uses a new mirrorlist system developed by Lance Davis for CentOS.
# The mirror system uses the connecting IP address of the client and the

# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

#released updates
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

#packages used/produced in the build but not released
[addons]
name=CentOS-$releasever - Addons
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=addons
#baseurl=http://mirror.centos.org/centos/$releasever/addons/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/addons/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

二、update yum

yum -y update

三、利用CentOS Linux系统自带的yum命令安装、升级所需的程序库

LANG=C
yum -y install 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

四、安装php和mysql

yum -y install php mysql mysql-server mysql-devel php-mysql php-cgi php-mbstring php-gd php-fastcgi

五、安装nginx
由于centos没有默认的nginx软件包,需要启用REHL的附件包

rpm -Uvh http://download.Fedora.RedHat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
yum -y install nginx

设置开机启动

chkconfig nginx on

六、安装spawn-fcgi来运行php-cgi

yum install spawn-fcgi

七、下载spawn-fcgi 的启动脚本

wget http://bash.cyberciti.biz/dl/419.sh.zip
unzip 419.sh.zip
mv 419.sh /etc/init.d/php_cgi
chmod +x /etc/init.d/php_cgi

启动php_cgi

/etc/init.d/php_cgi start

查看进程

netstat -tulpn | grep :9000

若出现如下代表一切正常

tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 4352/php-cgi

八、配置nginx(详细配置见nginx.conf详细说明)

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}

九、查看phpinfo
编写脚本

phpinfo();

十、安装phpmyadmin
修改/var/lib/php/session的权限和nginx和php_cgi一致

chown -R www.www /var/lib/php/session

转:http://apps.hi.baidu.com/share/detail/14819035

如果你打算使用Apache httpd Web服务器,请看这里。

一、系统约定

软件源代码包存放位置 /usr/local/data-original

源码包编译安装位置(prefix) /usr/local/software_name

脚本以及维护程序存放位置 /usr/local/sbin

MySQL 数据库位置 /var/lib/mysql(可按情况设置)

网站根目录 /home/www/wwwroot(可按情况设置)

虚拟主机日志根目录 /home/www/logs(可按情况设置)

运行账户 www:www

二、系统环境部署及调整

1、检查系统是否正常

# more /var/log/messages (检查有无系统级错误信息)

# dmesg (检查硬件设备是否有错误信息)

# ifconfig(检查网卡设置是否正确)

# ping www.163.com (检查网络是否正常)

# cat /proc/cpuinfo (检查CPU频率是否正常)

# top (按1检测CPU核数是否正常,内存大小是否正常)

2、关闭不需要的服务

# ntsysv

以下仅列出需要启动的服务,未列出的服务一律推荐关闭:

atd

crond

irqbalance

microcode_ctl

network

sendmail

sshd

syslog

关闭SElinux:修改/etc/selinux/config文件中的SELINUX= 为 disabled

3、更换yum国内源

# cd /etc/yum.repos.d

# mv CentOS-Base.repo CentOS-Base.repo.save

# wget http://centos.ustc.edu.cn/CentOS-Base.repo.5

# mv CentOS-Base.repo.5 CentOS-Base.repo

# yum clean all

4、服务器时间检查和设置

#data (检查时间是否正确,是否是中国时间CST)

#cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime (如果时区不对,则执行,时间正常的跳过)

#yum -y install ntp (安装ntp对时工具)

#chkconfig ntpd on (让对时服务开机启动)

5、使用 yum 对系统进行更新并且安装必要软件包

#yum update –y

  1. #yum -y install make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel

复制代码

6. 重新启动系统

# init 6

三、编译安装L.A.M.P环境

1、下载软件(截止到09年10月的最新版本)

# cd /usr/local/data-original

#wget http://sysoev.ru/nginx/nginx-0.7.63.tar.gz

#wget http://download.scientificlinux.net/nginx

#wget http://download.scientificlinux.net/php-fpm.conf

#wget http://download.scientificlinux.net/nginx.conf

#wget http://download.scientificlinux.net/fcgi.conf

#wget http://download.scientificlinux.net/php-5.2.10.tar.gz

#wget http://download.scientificlinux.net/php-5.2.10-fpm-0.5.13.diff.gz

#wget http://download.scientificlinux. ... glibc23-i386.tar.gz (32位系统)

#wget http://download.scientificlinux. ... ibc23-x86_64.tar.gz (64位系统)

#wget http://download.scientificlinux. ... i686-glibc23.tar.gz (32位系统)

#wget http://download.scientificlinux. ... 6_64-glibc23.tar.gz (64位系统)

2、安装MySQL

cd /usr/local/data-original

tar zxvf mysql-5.1.39-linux-i686-glibc23.tar.gz

mv mysql-5.1.39-linux-i686-glibc23 /usr/local/

ln -s /usr/local/mysql-5.1.39-linux-i686-glibc23/ /usr/local/mysql

groupadd mysql

useradd -g mysql mysql

chown -R mysql:mysql /usr/local/mysql

chown -R mysql:mysql /usr/local/mysql-5.1.39-linux-i686-glibc23/

cd /usr/local/mysql

./scripts/mysql_install_db --user=mysql

cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld

chmod 755 /etc/rc.d/init.d/mysqld

chkconfig --add mysqld

chkconfig --level 3 mysqld on

cp ./support-files/my-huge.cnf /etc/my.cnf

mv /usr/local/mysql/data /var/lib/mysql

chown -R mysql:mysql /var/lib/mysql

编辑/etc/my.cnf

在 [mysqld] 段增加

  1. datadir = /var/lib/mysql
  2. skip-innodb
  3. wait-timeout = 10
  4. max_connections = 512
  5. max_connect_errors = 10000000

复制代码

在 [mysqld] 段修改

  1. max_allowed_packet = 16M
  2. thread_cache_size = CPU个数*2

复制代码

将 log-bin 注释

service mysqld start

bin/mysqladmin -u root password 'password_for_root'

其中引号内的password_for_root是要设置的root密码

3、安装Nginx

cd /usr/local/data-original/

tar zxvf nginx-0.7.63.tar.gz

cd nginx-0.7.63

./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_realip_module --with-http_addition_module --with-http_gzip_static_module --with-http_random_index_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module

make

make install

cp /usr/local/data-original/nginx /etc/init.d/nginx

chmod 755 /etc/init.d/nginx

chkconfig --add nginx

chkconfig nginx on

4、安装PHP和Zend

cd /usr/local/data-original

tar zxvf php-5.2.10.tar.gz

gzip -cd php-5.2.10-fpm-0.5.13.diff.gz | patch -d php-5.2.10 -p1

cd php-5.2.10

./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/etc/cgi --enable-mbstring --enable-ftp --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --enable-magic-quotes --with-mysql=/usr/local/mysql --with-pear --enable-sockets --with-ttf --with-freetype-dir=/usr --enable-gd-native-ttf --with-zlib --enable-sysvsem --enable-sysvshm --with-libxml-dir=/usr --enable-force-cgi-redirect --enable-fastcgi --with-xmlrpc --enable-zip --enable-fpm

make

make install

mkdir -p /usr/local/etc/cgi/

cp php.ini-dist /usr/local/etc/cgi/php.ini

mv -f /usr/local/data-original/php-fpm.conf /usr/local/php5/etc/php-fpm.conf

groupadd www

useradd -g www www

echo 'ulimit -SHn 65535' >> /etc/rc.local

echo '/usr/local/php5/sbin/php-fpm start' >> /etc/rc.local

cd /usr/local/data-original

tar zxvf ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz

cd ZendOptimizer-3.3.3-linux-glibc23-i386

./install

(注意第一个要填的路径是Zend安装路径,第二个是php.ini所在的路径,即/usr/local/etc/cgi)

(不要选重启apache)

5、启动Nginx和php

mv -f /usr/local/data-original/fcgi.conf /usr/local/nginx/conf/

cp -f /usr/local/data-original/nginx.conf /usr/local/nginx/conf/nginx.conf

mkdir -p /home/www/wwwroot

ulimit -SHn 65535

/usr/local/php5/sbin/php-fpm start

service nginx start

在/home/www/wwwroot放入一个index.php,内容为

打开浏览器访问,即可看到phpinfo页面

6、设置系统防火墙

编辑/usr/local/sbin/fw.sh

复制以下内容进去

  1. #!/bin/bash
  2. # Stop iptables service first
  3. service iptables stop
  4. # Load FTP Kernel modules
  5. /sbin/modprobe ip_conntrack_ftp
  6. /sbin/modprobe ip_nat_ftp
  7. # Inital chains default policy
  8. /sbin/iptables -F -t filter
  9. /sbin/iptables -P INPUT DROP
  10. /sbin/iptables -P OUTPUT ACCEPT
  11. # Enable Native Network Transfer
  12. /sbin/iptables -A INPUT -i lo -j ACCEPT
  13. # Accept Established Connections
  14. /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  15. # ICMP Control
  16. /sbin/iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
  17. # WWW Service
  18. /sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
  19. # FTP Service
  20. /sbin/iptables -A INPUT -p tcp --dport 21 -j ACCEPT
  21. # SSH Service
  22. /sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT

复制代码

退出编辑,执行以下命令

# chmod 755 /usr/local/sbin/fw.sh
# echo '/usr/local/sbin/fw.sh' >> /etc/rc.local
# /usr/local/sbin/fw.sh