centos7安装nginx+mysql+php+yaf扩展

2016-03-14

准备工作

  • centos 服务器一台,6.x-7.0都可以安装,会针对7.0做特别说明
  • 一些需要的安装文件先通过工具传到服务器指定目录下,建议目录是 "/usr/local/src"当然你也可以在安装的过程中通过wget来下载这些文件。我在百度云里共享了这些文件点击下载,最新的版本请自行下载。

安装Nginx

我这里用的淘宝版的Tengine,它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。

  • 先安装一些依赖包
yum -y install gcc
yum -y install gcc-c++
yum install -y ncurses-devel bison
yum install wget setuptool
yum install make
yum install openssl-devel
  • 安装pcre ,支持Tengine伪静态
cd /usr/local/src
tar zxvf pcre-8.21.tar.gz
cd pcre-8.21
mkdir /usr/local/pcre #创建安装目录
./configure --prefix=/usr/local/pcre #配置
make
make install
cd ../    #回到src目录
  • 安装Tengine
yum install -y zlib-devel
cd /usr/local/src
wget http://tengine.taobao.org/download/tengine-1.2.3.tar.gz  #准备工作中已经上传了直接跳过
mkdir /usr/local/nginx
tar zxvf tengine-1.2.3.tar.gz
mv tengine-1.2.3 tengine   #重命名下
cd tengine
./configure  --prefix=/usr/local/nginx --with-http_stub_status_module  --with-http_ssl_module  --with-pcre=/usr/local/src/pcre-8.21
make
make install
  • 设置Tengine开机启动
vi  /etc/rc.d/init.d/nginx
#!/bin/bash
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

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
}

restart() {
    configtest || return $?
    stop
    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
:wq!保存退出
chmod 775 /etc/rc.d/init.d/nginx  #赋予文件执行权限
chkconfig --add nginx
chkconfig nginx on    #设置开机启动
/etc/rc.d/init.d/nginx restart
service nginx restart

安装MySql

  • 新建运行mysql的系统用户以及一些文件目录
groupadd mysql
useradd mysql -g mysql -M -s /sbin/nologin
mkdir -p /usr/local/mysql/data
mkdir -p /usr/local/mysql/log
chmod +w /usr/local/mysql
  • 安装cmake
cd /usr/local/src
tar zxvf cmake-2.8.6.tar.gz
cd cmake-2.8.6
./bootstrap
make
make install

cmake --version #检测是否安装成功
cd ../
  • 解压并安装mysql
tar zxvf  mysql-5.6.4-m7.tar.gz
cd mysql-5.6.4-m7

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=utf8,gbk -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_USER=mysql -DWITH_DEBUG=0

make
make install

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

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

ln -s /usr/local/mysql/lib/libmysqlclient.so.16 /usr/lib/libmysqlclient.so.16
ln -s /usr/local/mysql/bin/mysql /sbin/mysql
ln -s /usr/local/mysql/bin/mysql /bin/mysql
ln -s /usr/local/mysql/bin/mysqladmin /sbin/mysqladmin
ln -s /usr/local/mysql/bin/mysqladmin /bin/mysqladmin
ln -s /usr/local/mysql/bin/mysqldump /sbin/mysqldump
ln -s /usr/local/mysql/bin/mysqldump /bin/mysqldump
cp support-files/my-huge.cnf /etc/my.cnf -f

/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
chkconfig --add mysqld
chkconfig --level 2345 mysqld on
chmod +x /etc/init.d/mysqld
service mysqld start
mysqladmin -u root password  abcd12345  #设置密码
mysql -u root -p
  • 获取远程连接数据库的权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'abcd12345' WITH GRANT OPTION;
FLUSH PRIVILEGES;

安装PHP

  • 更新并安装依赖库和开发工具
yum update –y

yum -y install autoconf make libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib 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 libxml2 libxml2-devel patch pcre pcre-devel ImageMagick mhash mhash-devel libmcrypt libmcrypt-devel libevent libevent-devel libxslt libxslt-devel libtool libtool-ltdl libtool-ltdl-devel bison bison-devel
  • 安装libiconv
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure
make
make install

如果centos系统是7.0的版本可能会报错 ‘gets’未声明(不在函数。。。下面是解决方案。否则请跳过这段。

vi srclib/stdio.h

:set nu  #显示行号
:1010    #跳转到第几行

到1010行,注释掉该行即可:
_GL_CXXALIASWARN (gets);
/* It is very rare that the developer ever has full control of stdin,
  so any use of gets warrants an unconditional warning.  Assume it is always declared, since it is required by C89.  */

//_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

cd ../  # 返回src

tar zxvf php-5.4.5.tar.gz
cd php-5.4.5
mkdir /usr/local/php
ln -s /usr/lib64/libjpeg.so /usr/lib/libjpeg.so
ln -s /usr/lib64/libpng.so /usr/lib/libpng.so

cd ../   # 返回src

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum update

这里如果报错 Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again 参照下面的解决方案,否则请跳过这段

vi /etc/yum.repos.d/epel.repo

编辑[epel]下的baseurl前的#号去掉,mirrorlist前添加#号。正确配置如下:

[epel] 
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch 
failovermethod=priority 
enabled=1 
gpgcheck=1 
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

再运行

yum makecache
  • 安装php-mcrypt,mcrypt,libmcrypt
yum install php-mcrypt
yum install mcrypt

tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make
make install
  • 进入php的目录
cd ../
cd php-5.4.5

./configure -prefix=/usr/local/php -with-config-file-path=/usr/local/php/etc -with-mysql=mysqlnd -with-mysqli=mysqlnd -with-freetype-dir -with-jpeg-dir -with-png-dir -with-zlib -with-libxml-dir=/usr/local -enable-xml -disable-rpath -enable-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 -enable-soap -with-xmlrpc -enable-zip -disable-ipv6 -without-pear -with-iconv-dir=/usr/local/  -with-pdo-mysql=/usr/local/mysql   -with-fpm-user=www  -with-fpm-group=www

make ZEND_EXTRA_LIBS='-liconv'
make install

请注意,make出错提示缺少扩展的,请自行安装,自己项目用不到的扩展请直接去掉 如果是centos7 出错 提示 make: *** [ext/dom/node.lo] Error 1执行下面的代码

cd ../
cd php-5.4.5
patch -p0 -b < ../php-5.x.x.patch
  • 创建用户
/usr/sbin/groupadd www && /usr/sbin/useradd -g www www
  • 复制配置文件并修改php-fpm.conf 和 php.ini
cp php.ini-development php.ini-production /usr/local/php/etc/
cd /usr/local/php/etc/
mv php-fpm.conf.default    php-fpm.conf

vim php-fpm.conf

#去掉/更改 配置文件中的;
pid = /usr/local/php/var/run/php-fpm.pid
pm.max_children = 64
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 1024
user = www
group = www

#检查语法是否正确
/usr/local/php/sbin/php-fpm -t
#出现NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful 测试成功

#设置开机启动
vi /etc/rc.d/rc.local

#在行末加入
/usr/local/php/sbin/php-fpm &
cd /usr/local/php/etc/
cp php.ini-development /usr/local/php/etc/php.ini

#找到并修改
date.timezone = “Asia/Shanghai”
display_errors = On
error_reporting = E_ALL & ~E_NOTICE

安装Yaf扩展

  • 安装扩展并修改php配置
cd /usr/local/src
tar xvf yaf-2.3.5.tar
cd yaf-2.3.5
/usr/local/php/bin/phpize

./configure  --with-php-config=/usr/local/php/bin/php-config
make
make install

#修改php配置文件
vi /usr/local/php/etc/php.ini
:1890
[yaf]
extension=yaf.so
  • 重启php-fpm
kill php-fpm
/usr/local/php/sbin/php-fpm &