标签:
说明:
操作系统:CentOS 6.x
准备篇:
一、配置防火墙,开启80端口、3306端口
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT(允许80端口通过防火墙)
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT(允许3306端口通过防火墙)
:wq! #保存
/etc/init.d/iptables restart #重启防火墙使配置生效
四 、系统约定
软件源代码包存放位置:/usr/local/src
源码包编译安装位置:/usr/local/软件名字
五、下载软件包
libpng http://www.libpng.org/pub/png/
http://prdownloads.sourceforge.net/libpng/libpng-1.6.17.tar.gz?download
freetype http://sourceforge.net/projects/freetype/
http://download.savannah.gnu.org/releases/freetype/freetype-2.6.tar.gz
jpeg9 http://www.ijg.org/
http://www.ijg.org/files/jpegsrc.v9a.tar.gz
libgd http://libgd.bitbucket.org/
https://bitbucket.org/libgd/gd-libgd/downloads/libgd-2.1.1.tar.gz
pcre (支持nginx伪静态)
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz
openssl http://www.openssl.org/
http://www.openssl.org/source/openssl-1.0.2c.tar.gz
zlib http://zlib.net/
http://zlib.net/zlib-1.2.8.tar.gz
libxml2 http://xmlsoft.org/
ftp://xmlsoft.org/libxml2/libxml2-2.9.2.tar.gz
libmcrypt(PHP libmcrypt模块) http://mcrypt.hellug.gr/lib/
ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
nginx http://nginx.org/
http://nginx.org/download/nginx-1.9.2.tar.gz
php http://php.net/
http://cn2.php.net/distributions/php-5.4.42.tar.gz
cmake(MySQL编译工具) http://www.cmake.org/
http://www.cmake.org/files/v2.8/cmake-2.8.12.2.tar.gz
MySQL http://dev.mysql.com/downloads/mysql/
http://mysql.mirror.kangaroot.net/Downloads/MySQL-5.6/mysql-5.6.24.tar.gz
安装篇
安装编译工具及库文件(使用 CentOS yum 命令安装)
yum install make m4 autoconf automake curl curl-devel gcc gcc-c++ zlib-devel bison bison-devel openssl openssl-devel pcre-devel mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl libevent libcom_err-devel libsepol-devel libselinux-devel krb5-devel ncurses* bzip2-devel libxml2 libxml2-devel libpng libpng-devel freetype libfreetype-devel(改成freetype-devel) libjpeg libjpeg-devel gd gd-devel perl
安装依赖库
1、安装 pcre
cd /usr/local/src
mkdir /usr/local/pcre
tar zxvf pcre-8.36.tar.gz
cd pcre-8.36
./configure
make
make install
2、安装 zlib
cd /usr/local/src
tar xvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install
3、安装 openssl
cd /usr/local/src
mkdir /usr/local/openssl
tar zxvf openssl-1.0.2c.tar.gz
cd openssl-1.0.2c
./config
make
make install
二、安装cmake (MySQL5.5以上版本采用CMAKE编译)
cd /usr/local/src
tar xvf cmake-2.8.12.2.tar.gz
cd cmake-2.8.12.2
./bootstrap
gmake
gmake install
四、安装 MySQL
准备工作
groupadd mysql #添加 mysql 用户组
useradd -g mysql mysql -s /bin/false #创建用户 mysql 并加入到 mysql 组,不允许 mysql 用户直接登录系统
mkdir -p /data/mysql/data #创建 mysql 数据库存放目录
chown -R mysql:mysql /data/mysql/data #设置 mysql 数据库目录权限
#编译安装
cd /usr/local/src
tar zxvf mysql-5.6.42.tar.gz
cd mysql-5.6.42
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql/data \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_TCP_PORT=3306
make
make install
注:cmake 时如出现错误“Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH)”
执行以下操作再重新编译即可:
rm -rf CMakeCache.txt CMakeFiles/
yum install ncurses-devel
#配置
cd /usr/local/mysql
mv /etc/my.cnf /etc/my.cnf.bak
cp -f ./support-files/my-default.cnf ./my.cnf
cp ./support-files/mysql.server /etc/init.d/mysqld #加入系统启动
chmod a+x /etc/init.d/mysqld #增加执行权限
chkconfig --add mysqld
chkconfig mysqld on #加入开机启动
ln -sf /usr/local/mysql/bin/* /usr/local/bin/
#将 mysql/bin 目录加入环境变量
vi /etc/profile #在最后一行加入:
############################################################ PATH=$PATH之间不能有空格
PATH=$PATH:/usr/local/mysql/bin
export PATH
############################################################
:wq! 保存
source /etc/profile #使其生效
echo $PATH #查看是否添加成功
#初始化
./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql
#启动
service mysqld start
#设置 root 用户密码
方法1:
mysql -u root
mysql> SET PASSWORD = PASSWORD(‘123456‘);
方法2:
mysqladmin -u root -p password 123456
接下来会提示输入旧密码,直接回车即可。
注:方法2命令中的密码字符串外面不要加引号
#若要设置 root 用户可以远程访问,执行:
mysql> GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘password‘ WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
五、安装 Nginx
groupadd www #添加www用户组
useradd -g www www -s /bin/false #创建 nginx 运行用户 www 并加入到 www 组,不允许 www 用户直接登录系统
cd /usr/local/src
tar zxvf nginx-1.9.2.tar.gz
cd nginx-1.9.2
./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-pcre=/usr/local/src/pcre-8.36 \
--with-zlib=/usr/local/src/zlib-1.2.8 \
--with-openssl=/usr/local/src/openssl-1.0.2c \
--with-http_ssl_module
make
make install
注: --with-pcre --with-zlib --with-openssl 指向的是源码包解压后的路径,而不是安装的路径。
配置nginx开机启动
vi /etc/init.d/nginx #编辑启动文件并添加下面内容
############################################################
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it‘s not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
############################################################
:wq! 保存退出。
chmod +x /etc/init.d/nginx #赋予执行权限
/sbin/chkconfig nginx on #设置开机启动
#启动
service nginx start
六、安装 PHP
安装 libmcrypt
cd /usr/local/src
tar zxvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure
make
make install
注:编译时如提示 ‘Pyobject‘ undeclared... 错误,需先安装 python-devel 软件包(yum -y install python-devel),
安装 php
cd /usr/local/src
tar zxvf php-5.4.42.tar.gz
cd php-5.4.42
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib \
--with-gd \
--with-png-dir \
--with-jpeg-dir \
--with-freetype-dir \
--enable-gd-native-ttf \
--with-iconv \
--enable-xml \
--enable-bcmath \ #支持大整数计算的扩展
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curlwrappers \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext=shared \
--enable-session \
--with-mcrypt \
--with-curl
make #编译
make install #安装
cp php.ini-production /usr/local/php/etc/php.ini #复制php配置文件到安装目录
rm -rf /etc/php.ini #删除系统自带配置文件
ln -s /usr/local/php/etc/php.ini /etc/php.ini #添加软链接
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf #拷贝模板文件为php-fpm配置文件
vi /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid #取消前面的分号
user = www #设置php-fpm运行账号为www
group = www #设置php-fpm运行组为www
设置 php-fpm 开机启动
cp /usr/local/src/php-5.4.42/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm #拷贝php-fpm到启动目录
chmod +x /etc/init.d/php-fpm #添加执行权限
chkconfig php-fpm on #设置开机启动
PHP 配置
vi /usr/local/php/etc/php.ini #编辑配置文件
找到:;date.timezone =
修改为:date.timezone = PRC #设置时区
找到:expose_php = On
下面红色部分未做修改。
修改为:expose_php = OFF #禁止显示php版本的信息
找到:display_errors = On
修改为:display_errors = OFF #关闭错误提示
七、配置 nginx 支持 php
vi /usr/local/nginx/conf/nginx.conf
修改/usr/local/nginx/conf/nginx.conf 配置文件,做如下修改
user www www; #首行user去掉注释,修改 Nginx 运行组为 www www;尽量与 /usr/local/php5/etc/php-fpm.conf 中的 user, group 配置相同,否则php可能运行出错
index index.php index.html index.htm; #添加index.php
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
/etc/init.d/nginx restart #重启nginx
安装 php redis 扩展
cd /usr/local/src
wget http://pecl.php.net/get/redis-2.2.7.tgz (为什么后面还有一个redis3.0版本?)
tar zxvf redis-2.2.7.tgz
cd redis-2.2.7
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
修改 php.ini 文件:
vi /usr/local/php/etc/php.ini
增加:
extension=redis.so
/etc/init.d/php-fpm restart #重启 php-fpm 生效
安装 php memcached 扩展
安装依赖库 libmemcached
cd /usr/local/src
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure --with-memcached
make
make install
安装 memcached 扩展
cd /usr/local/src
wget http://pecl.php.net/get/memcached-2.2.0.tgz
tar zxvf memcache-2.2.0.tgz
cd memcache-2.2.0
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcached --disable-memcached-sasl
make
make install
修改 php.ini 文件:
vi /usr/local/php/etc/php.ini
增加:
extension=memcached.so
/etc/init.d/php-fpm restart #重启 php-fpm 生效
安装 php memcache 扩展
cd /usr/local/src
wget http://pecl.php.net/get/memcache-3.0.8.tgz
tar zxvf memcache-3.0.8.tgz
cd memcache-3.0.8
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
修改 php.ini 文件:
vi /usr/local/php/etc/php.ini
增加:
extension=memcache.so
/etc/init.d/php-fpm restart #重启 php-fpm 生效
安装 php Zend Opcache 扩展(即 Optimizer+,推荐加速器)
cd /usr/local/src
wget http://pecl.php.net/get/zendopcache-7.0.5.tgz
tar zxvf zendopcache-7.0.5.tgz
cd zendopcache-7.0.5
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-shared
make
make install
修改 php.ini 文件:
vi /usr/local/php/etc/php.ini
增加:
zend_extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/opcache.so
;配置参数(以下为官方推荐参数,详细参数:http://www.php.net/manual/zh/opcache.configuration.php)
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
/etc/init.d/php-fpm restart #重启 php-fpm 生效
注:Zend Opcache 与 eAccelerator 相冲突,安装使用 Zend Opcache 可能需要先卸载或禁用 eaccelerator 扩展。
六、安装 Redis
安装需要的支持环境
cd /usr/local/src
wget http://downloads.sourceforge.net/tcl/tcl8.6.4-src.tar.gz
tar zxvf tcl8.6.4-src.tar.gz
cd ?tcl8.6.3/unix/
./configure
make
make install
安装 Redis
cd /usr/local/src
tar zxvf redis-3.0.2.tar.gz
cd redis-3.0.2
make
make PREFIX=/usr/local/redis install
ln -sf /usr/local/redis/bin/* /usr/local/bin/ #增加软链接
cp ./redis.conf /usr/local/redis/
vi /usr/local/redis/redis.conf
daemonize yes #redis将以守护进程的方式运行,默认为no会暂用你的终端
timeout 300? #当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
设置自动启动
vi /etc/init.d/redis
############################################################
#!/bin/sh
#
# redis Startup script for Redis Server
#
# chkconfig: - 80 12
# description: Redis is an open source, advanced key-value store.
#
# processname: redis-server
# config: /etc/redis.conf
# pidfile: /var/run/redis.pid
source /etc/init.d/functions
BIN="/usr/local/redis/bin"
CONFIG="/usr/local/redis/redis.conf"
PIDFILE="/var/run/redis.pid"
### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis Server"
start() {
if [ -e $PIDFILE ];then
echo "$desc already running...."
exit 1
fi
echo -n $"Starting $desc: "
daemon $BIN/$prog $CONFIG
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Stop $desc: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
############################################################
:wq! 保存退出。
chmod +x /etc/init.d/redis #赋予执行权限
启动或关闭服务
service redis start
service redis stop
测试
cd /usr/local/redis/bin
./redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
安装到这里。
安装 git
cd /usr/local/src
wget https://codeload.github.com/git/git/tar.gz/v2.4.4
tar zxvf v2.4.4
cd git-2.4.4
make prefix=/usr/local/git all // 需要yum安装 openssl-devel
make prefix=/usr/local/git install
注:若出现错误 “错误:‘XML_Parser’未声明”,则需安装 expat-devel 开发包,然后重新执行 make 命令。
yum -y install expat-devel.x86_64
若出现错误 “asciidoc: command not found”,则需安装 asciidoc,然后重新执行 make 命令。
yum -y install asciidoc
git --version #查看版本
安装 Memcache
安装 libevent
cd /usr/local/src
tar zxvf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure --prefix=/usr/local/libevent
make
make install
安装 memcache
cd /usr/local/src
tar zxvf memcached-1.4.24.tar.gz
cd memcached-1.4.24
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
make
make install
ln -s /usr/local/memcached/bin/memcached /usr/local/bin/memcached
启动
memcached -d -m 512 -u root -p 11211 -c 1024 -P /tmp/memcached.pid
启动参数说明:
-d 选项是启动一个守护进程。
-u root 表示启动 memcached 的用户为 root
-m 是分配给 Memcache 使用的内存数量,单位是MB,默认64MB
-M return error on memory exhausted (rather than removing items)
-u 是运行 Memcache 的用户,如果当前为 root 的话,需要使用此参数指定用户
-p 是设置 Memcache 的 TCP 监听的端口,默认为11211
-c 选项是最大运行的并发连接数,默认是1024
-P 是设置保存 Memcache 的 pid 文件
停止
先查看进程 id
ps -ef|grep memcached
root 15144 1 0 08:43 ? 00:00:00 /usr/local/memcached/bin/memcached -d -m 512 -u root -p 11211 -c 1024 -P /tmp/memcached.pid
15144 为pid
则停止命令为:kill -9 15144
测试篇
cd /usr/local/nginx/html
rm -rf /usr/local/nginx/html/* #删除默认测试页
echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php
chown -R www.www /usr/local/nginx/html/ #设置目录所有者
chmod -R 700 /usr/local/nginx/html/ #设置目录权限
在客户端浏览器输入服务器 IP 地址,可以看到相关的配置信息
service nginx restart #重启nginx
service mysqld restart #重启mysql
service php-fpm start #启动php-fpm
service php-fpm restart #重启php-fpm
service php-fpm stop #停止php-fpm
service php-fpm start #启动php-fpm
##############################################
备注:
nginx 默认站点目录是:/usr/local/nginx/html/
权限设置:chown www.www /usr/local/nginx/html/ -R
MySQL 数据库目录是:/data/mysql/data
权限设置:
chwon -R www:www /data/www
chown -R mysql.mysql /data/mysql/data
到此,运行环境搭建完成。
标签:
原文地址:http://www.cnblogs.com/sylar-liang/p/4619637.html