centos7搭建nginx+mysql(mariadb)+php+memcache
首先更新系统
yum makecache && yum -y update |
安装必要的编译组件
yum -y install bash-completion vim net-tools bind-utils wget unzip |
重启
reboot |
下载必要的软件
cd /usr/local/src/ && |
wget -ct 5 http://smallive-soft.stor.sinaapp.com/nginx-1.7.8.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/mariadb-10.0.15.tar.zip && wget -ct 5 http://smallive-soft.stor.sinaapp.com/php-5.6.3.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/pcre-8.36.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/memcache-2.2.7.tgz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/openssl-1.0.1j.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/cmake-2.8.12.2.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/zlib-1.2.8.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/libmcrypt-2.5.8.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/yasm-1.3.0.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/t1lib-5.1.2.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/libgd-2.1.0.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/libpng-1.6.15.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/freetype-2.5.3.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/jpegsrc.v9a.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/libevent-2.0.21-stable.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/memcached-1.4.21.tar.gz && wget -ct 5 http://smallive-soft.stor.sinaapp.com/tiff-4.0.3.tar.gz && echo ‘download complete’ |
安装扩展等
yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* zlib-devel |
开始编译安装
cd /usr/local/src && tar -zxvf cmake* && cd cmake* && ./configure && make && make install |
cd /usr/local/src && tar -zxvf libevent* && cd libevent* && ./configure –prefix=/usr && make && make install && cd /usr/local/src && tar -zxvf memcached* && cd memcached* && ./configure –prefix=/usr/local/memcache –with-libevent=/usr && make && make install |
为mysql添加启动用户
groupadd mysql && useradd -g mysql mysql -s /bin/false && mkdir -p /data/mysql && chown -R mysql:mysql /data/mysql |
mkdir -p /usr/local/mysql && cd /usr/local/src && unzip mariadb* tar -zxvf mariadb-10.0.15.tar.gz* && cd mariadb* && cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc && make && make install |
备份配置
mv /etc/my.cnf /etc/my.cnf.bak |
配置mysql 设置自启动等
cd /usr/local/mysql && ./scripts/mysql_install_db –user=mysql –basedir=/usr/local/mysql –datadir=/data/mysql && ln -s /usr/local/mysql/my.cnf /etc/my.cnf && cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld && chmod 755 /etc/init.d/mysqld && chkconfig mysqld on vi /etc/rc.d/init.d/mysqld basedir=/usr/local/mysql #MySQL程序安装路径 datadir=/data/mysql #MySQl数据库存放目录 service mysqld start |
把MariaDB加入系统环境变量:
echo ‘export PATH=$PATH:/usr/local/mysql/bin’>>/etc/profile && source /etc/profile |
把MariaDB的库文件链接到系统默认的位置,在编译PHP等软件时可以不用指定其库文件地址:
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql && ln -s /usr/local/mysql/include/mysql /usr/include/mysql && mkdir /var/lib/mysql && ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock mysql_secure_installation |
安装Nginx
创建nginx的用户和用户组:
groupadd www && useradd -g www www -s /bin/false |
安装PCRE:
cd /usr/local/src && mkdir /usr/local/pcre && tar -zxvf pcre* && cd pcre* && ./configure –prefix=/usr/local/pcre && make && make install && echo ‘安装OpenSSL:’ cd /usr/local/src && mkdir /usr/local/openssl && tar -zxvf openssl* && cd openssl* && ./config –prefix=/usr/local/openssl && make && make install && echo ‘export PATH=$PATH:/usr/local/openssl/bin’>>/etc/profile && source /etc/profile && echo ‘安装zlib:’ cd /usr/local/src && mkdir /usr/local/zlib && tar zxvf zlib* && cd zlib* && ./configure –prefix=/usr/local/zlib && make && make install |
安装第三方模块ngx_http_accesskey_module以实现Nginx防盗链
cd /usr/local/src && wget http://smallive-soft.stor.sinaapp.com/Nginx-accesskey-2.0.3.tar.gz && tar vxzf Nginx-accesskey-2.0.3.tar.gz && cd nginx-accesskey-2.0.3/ |
修改config文件,替换其中 的”$HTTP_ACCESSKEY_MODULE”为”ngx_http_accesskey_module”.
我们可以在官方文档查看更多信息(http://nginx.org/en/docs/)。下面安装Nginx:
cd /usr/local/src && tar -zxvf nginx-1* && cd nginx-1* && ./configure –prefix=/usr/local/nginx \ –without-http_memcached_module \ –user=www \ –group=www \ –with-http_stub_status_module \ –with-http_ssl_module \ –with-http_gzip_static_module \ –with-openssl=/usr/local/src/openssl-1.0.1j \ –with-zlib=/usr/local/src/zlib-1.2.8 \ –with-pcre=/usr/local/src/pcre-8.36 \ –with-http_stub_status_module \ –with-http_sub_module \ –add-module=/usr/local/src/nginx-accesskey-2.0.3/ && make && make install && echo “安装NGINX完毕!” vi /usr/local/nginx/conf/nginx.conf user www www; error_log logs/error.log; pid logs/nginx.pid; cd /etc/rc.d/init.d/ && wget http://smallive-soft.stor.sinaapp.com/nginx |
设置启动脚本、启动Nginx:
chmod 775 /etc/rc.d/init.d/nginx && chkconfig nginx on && /etc/rc.d/init.d/nginx start |
把Nginx加入系统变量:
echo ‘export PATH=$PATH:/usr/local/nginx/sbin’>>/etc/profile && source /etc/profile |
安装yasm:
cd /usr/local/src && tar -zxvf yasm-* && cd yasm-* && ./configure && make && make install && echo ‘安装mcrypt:’ && cd /usr/local/src && tar -zxvf libmcrypt-* && cd libmcrypt-* && ./configure && make && make install && echo ‘安装tiff:’ && cd /usr/local/src && tar -zxvf tiff-* && cd tiff-* && ./configure –prefix=/usr/local/tiff –enable-shared && make && make install && echo ‘安装libpng:’ && cd /usr/local/src && tar -zxvf libpng-* && cd libpng-* && ./configure –prefix=/usr/local/libpng –enable-shared && make && make install && echo ‘安装freetype:’ && cd /usr/local/src && tar -zxvf freetype-* && cd freetype-* && ./configure –prefix=/usr/local/freetype –enable-shared && make && make install && echo ‘安装jpeg:’ && cd /usr/local/src && tar -zxvf jpegsrc.v9a.tar.gz && cd jpeg-9a && ./configure –prefix=/usr/local/jpeg –enable-shared && make && make install && echo ‘安装libgd:’ && cd /usr/local/src && tar -zxvf libgd-* && cd libgd-* && ./configure –prefix=/usr/local/libgd –enable-shared –with-jpeg=/usr/local/jpeg –with-png=/usr/local/libpng –with-freetype=/usr/local/freetype –with-fontconfig=/usr/local/freetype –with-xpm=/usr/ –with-tiff=/usr/local/tiff && make && make install && echo ‘安装t1lib:’ && cd /usr/local/src && tar -zxvf t1lib-* && cd t1lib-* && ./configure –prefix=/usr/local/t1lib –enable-shared && make without_doc && make install |
安装php:
注意:如果系统是64位,安装前请执行以下两条命令:
ln -s /usr/lib64/libltdl.so /usr/lib/libltdl.so && \cp -frp /usr/lib64/libXpm.so* /usr/lib/ |
编译并安装PHP:
cd /usr/local/src && tar -zvxf php-5.* && cd php-5.* && export LD_LIBRARY_PATH=/usr/local/libgd/lib && ./configure –prefix=/usr/local/php \ –with-config-file-path=/usr/local/php/etc \ –with-mysql=/usr/local/mysql \ –with-mysqli=/usr/local/mysql/bin/mysql_config \ –with-mysql-sock=/tmp/mysql.sock \ –with-pdo-mysql=/usr/local/mysql \ –with-gd \ –with-png-dir=/usr/local/libpng \ –with-jpeg-dir=/usr/local/jpeg \ –with-freetype-dir=/usr/local/freetype \ –with-xpm-dir=/usr/ \ –with-zlib-dir=/usr/local/zlib \ –with-t1lib=/usr/local/t1lib \ –with-iconv \ –enable-libxml \ –enable-xml \ –enable-bcmath \ –enable-shmop \ –enable-sysvsem \ –enable-inline-optimization \ –enable-opcache \ –enable-mbregex \ –enable-fpm \ –enable-mbstring \ –enable-ftp \ –enable-gd-native-ttf \ –with-openssl \ –enable-pcntl \ –enable-sockets \ –with-xmlrpc \ –enable-zip \ –enable-soap \ –without-pear \ –with-gettext \ –enable-session \ –with-mcrypt \ –with-curl \ –enable-exif \ –enable-ctype && make && make install |
复制php配置文件到安装目录:
cp php.ini-production /usr/local/php/etc/php.ini |
备份系统自带配置文件:
mv /etc/php.ini /etc/php.ini.bak |
添加软链接到 /etc目录:
ln -s /usr/local/php/etc/php.ini /etc/php.ini |
拷贝模板文件为php-fpm配置文件:
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf |
添加软连接到 /etc目录:
ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf |
修改PHP配置文件:
# vim /usr/local/php/etc/php.ini |
extension = “memcache.so”;
找到”disable_functions =” (禁用掉某些比较“危险”函数,大概在305行),改为
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname;
找到”;date.timezone =”(大概在913行),修改为
date.timezone = PRC;
找到”expose_php = On”(禁止显示php版本的信息,大概在366行),修改为
expose_php = Off;
找到”short_open_tag = Off”(支持php短标签,大概在202行),修改为
short_open_tag = On;
找到”;opcache.enable=0″(支持opcode缓存,大概在1838行),修改为
opcache.enable=1;
找到”;opcache.enable_cli=0″(支持opcode缓存,大概在1841行),修改为
opcache.enable_cli=0;
并在下面加入’zend_extension = “opcache.so”‘一行, 开启opcode缓存功能
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
配置php-fpm
#vim /usr/local/php/etc/php-fpm.conf</rpe> 大概在分别25、147行,修改内容如下: ;取消pid前面的分号 pid = run/php-fpm.pid;…; 设置php-fpm运行账号为www user = www; 设置php-fpm运行组为www group = www 拷贝php-fpm脚本到启动目录,给予执行权限, 设置开机启动: <pre lang="apache"> cp /usr/local/src/php-5.6.3/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm && chmod +x /etc/rc.d/init.d/php-fpm && chkconfig php-fpm on </rpe> 把php加入系统变量: <pre lang="apache"> echo ‘export PATH=$PATH:/usr/local/php/bin’>>/etc/profile && source /etc/profile |
配置nginx支持php
修改/usr/local/nginx/conf/nginx.conf 配置文件,需做如下修改
user www www;
index index.php index.html index.htm;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#取消FastCGI server部分location的注释,注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
测试
上述配置修改完毕,重启nginx,并启动php-fpm:
service nginx restart && service php-fpm start |
创建phpinfo文件,设置适当的目录权限。
cd /usr/local/nginx/html/ && echo ‘<!--?php phpinfo(); ?-->’>index.php && chown -R www:www /usr/local/nginx/html/ |
安装memcache、memcached
cd /usr/local/src && tar xvf memcache-2.2.7.tgz cd memcache-2.2.7/ ./configure –enable-memcache –with-zlib-dir=/url/local/zlib –with-php-config=/url/local/php/bin/php-config make make install cd /etc/init.d/ && wget http://smallive-soft.stor.sinaapp.com/memcached chmod +x /etc/init.d/memcached chkconfig –add memcached chkconfig memcached on cd .. && tar -zxvf && cd && ./configure && make && make install |