-
系统、软件版本
CentOS 6.9 Nginx 1.12.2 PHP 5.6.32 Mysql 5.7.20
-
安装准备
A、防火墙 编辑文件:vi /etc/sysconfig/iptables 添加规则(放行80端口):-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT :wq! #保存退出 service iptables restart #最后重启防火墙使配置生效 B、关闭Selinux vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINUX=disabled #增加 :wq! #保存退出 setenforce 0 #使配置立即生效 getenforce #检查配置是否正确 C、安装约定 软件包存放位置: /usr/local/src 安装位置: /usr/local/ D、软件包下载 1、下载nginx wget http://nginx.org/download/nginx-1.9.4.tar.gz 2、下载MySQL wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.20.tar.gz -P /usr/local/src/ 3、下载php wget http://cn2.php.net/distributions/php-5.6.33.tar.gz 4、下载pcre wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz 5、下载openssl(nginx扩展) wget https://www.openssl.org/source/openssl-1.0.2n.tar.gz 6、下载zlib(nginx扩展) wget http://zlib.net/zlib-1.2.11.tar.gz 7、下载cmake(MySQL编译工具)wget https://cmake.org/files/v3.9/cmake-3.9.6.tar.gz 8、下载libmcrypt(php扩展) wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz 9、下载yasm(php扩展) wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz 10、t1lib(php扩展) wget ftp://sunsite.unc.edu/pub/Linux/libs/graphics/t1lib-5.1.2.tar.gz 11、下载gd库安装包 wget https://github.com/libgd/libgd/releases/download/gd-2.2.5/libgd-2.2.5.tar.gz 12、libvpx(gd库需要) wget http://www.loongnix.org/cgit/libvpx/snapshot/libvpx-1.5.0.tar.gz 13、tiff(gd库需要) wget http://download.osgeo.org/libtiff/tiff-4.0.9.tar.gz 14、libpng(gd库需要) wget ftp://ftp.simplesystems.org/pub/png/src/libpng16/libpng-1.6.34.tar.gz 15、freetype(gd库需要) wget https://download.savannah.gnu.org/releases/freetype/freetype-2.9.tar.gz 16、jpegsrc(gd库需要) wget http://www.ijg.org/files/jpegsrc.v9a.tar.gz
E、安装系统环境包
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 libxml libXaw-devel libXmu-devel libtiff libtiff make mpfr ncurses ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib nasm nasm wget zlib-devel openldap openldap-devel -
mysql安装
此处仅安装mysql程序用于后续PHP编译使用,详细mysql安装可以参考《https://blog.51cto.com/66585/2065899》 需要使用cmake进行编译,首先安装cmake. tar zxvf cmake-3.9.6.tar.gz mkdir /usr/local/cmake cd cmake-3.9.6 ./bootstrap --prefix=/usr/local/cmake make make install 配置环境变量 vim /etc/profile.d/cmake.sh 增加一行:export PATH=$PATH:/usr/local/cmake/bin source /etc/profile 使配置生效。 创建mysql账号、安装目录 groupadd mysql useradd -g mysql mysql -s /bin/false mkdir -p /usr/local/mysql 解压缩 cd /usr/local/src tar zxvf mysql-5.7.20.tar.gz cd /usr/local/src/mysql-5.7.20 编译, cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc 报错:缺少boost包 CMake Error at cmake/boost.cmake:81 (MESSAGE): You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory> This CMake script will look for boost in <directory>. If it is not there, it will download and unpack it (in that directory) for you. 创建boost文件目录 mkdir /usr/local/boost cd /usr/local/boost/ wget http://192.168.1.99/software/mysql/boost_1_59_0.tar.gz #只需要把源码包放进去即可,无需解压缩 删除came缓存文件 rm CMakeCache.txt 再次编译: cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DWITH_BOOST=/usr/local/boost/ #指明boost目录位置 -- Configuring done -- Generating done -- Build files have been written to: /usr/local/src/mysql-5.7.20 make make install 配置环境变量 vim /etc/profile.d/mysql.sh export PATH=$PATH:/usr/local/mysql/bin source /etc/profile #使配置生效
-
Nginx安装
安装pcre cd /usr/local/src mkdir /usr/local/pcre tar zxvf pcre-8.41.tar.gz cd pcre-8.41 ./configure --prefix=/usr/local/pcre make make install 安装openssl cd /usr/local/src mkdir /usr/local/openssl tar zxvf openssl-1.0.2n.tar.gz cd openssl-1.0.2n ./config --prefix=/usr/local/openssl make make install mv /usr/bin/openssl /usr/bin/openssl.bak mv /usr/include/openssl /usr/include/openssl.bak ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl ln -s /usr/local/openssl/include/openssl /usr/include/openssl echo "/usr/local/openssl/lib" >> /etc/ld.so.conf openssl version -a
安装zlib
cd /usr/local/src
mkdir /usr/local/zlib
tar zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/usr/local/zlib
make
make install
安装Nginx
tar xf nginx-1.12.2.tar.gz
cd nginx-1.12.2
新建nginx用户
groupadd -g 118 -r nginx
useradd -u 118 -g nginx nginx -s /bin/false
mkdir /usr/local/nginx
./configure --user=nginx --group=nginx --with-pcre=/usr/local/src/pcre-8.41 --with-openssl=/usr/local/src/openssl-1.0.2n --with-zlib=/usr/local/src/zlib-1.2.11 --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-file-aio
make && make install
chown -R root:nginx /usr/local/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="/usr/local/nginx/sbin/nginx" #注意nginx文件位置
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
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 +x /etc/init.d/nginx
添加至系统服务并配置开机启动
chkconfig --add nginx
chkconfig nginx on
chkconfig --list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
启动nginx
service nginx start
警告,权限不够:
chown: missing operand after `/var/cache/nginx/client_temp'
Try `chown --help' for more information.
chown: missing operand after `/var/cache/nginx/proxy_temp'
Try `chown --help' for more information.
chown: missing operand after `/var/cache/nginx/fastcgi_temp'
Try `chown --help' for more information.
chown: missing operand after `/var/cache/nginx/uwsgi_temp'
Try `chown --help' for more information.
chown: missing operand after `/var/cache/nginx/scgi_temp'
Try `chown --help' for more information.
service nginx stop
#修改/var/cache/nginx 目录权限:
chown -R nginx:nginx /var/cache/nginx
再次启动;
service nginx start
Starting nginx: [ OK ]
检查端口信息
netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1503/rpcbind
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 860/nginx
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 32556/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1581/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 9527/master
tcp 0 0 0.0.0.0:58940 0.0.0.0:* LISTEN 1525/rpc.statd
tcp 0 0 :::45128 :::* LISTEN 1525/rpc.statd
tcp 0 0 :::111 :::* LISTEN 1503/rpcbind
tcp 0 0 :::22 :::* LISTEN 32556/sshd
tcp 0 0 ::1:631 :::* LISTEN 1581/cupsd
tcp 0 0 ::1:25 :::* LISTEN 9527/master
udp 0 0 127.0.0.1:853 0.0.0.0:* 1525/rpc.statd
udp 0 0 0.0.0.0:111 0.0.0.0:* 1503/rpcbind
udp 0 0 0.0.0.0:631 0.0.0.0:* 1581/cupsd
udp 0 0 0.0.0.0:50942 0.0.0.0:* 1525/rpc.statd
udp 0 0 0.0.0.0:830 0.0.0.0:* 1503/rpcbind
udp 0 0 :::111 :::* 1503/rpcbind
udp 0 0 :::36005 :::* 1525/rpc.statd
udp 0 0 :::830 :::* 1503/rpcbind
测试
http://192.168.1.112/
-
PHP安装
安装yasm
cd /usr/local/src tar zxvf yasm-1.3.0.tar.gz cd yasm-1.3.0 ./configure # 不需要指定路径; make make install
安装libmcrypt
cd /usr/local/src tar zxvf libmcrypt-2.5.8.tar.gz cd libmcrypt-2.5.8 ./configure make make install
安装libvpx
cd /usr/local/src tar xvf libvpx-v1.5.0.tar.bz2 cd libvpx-v1.5.0 ./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9 make make install
安装tiff
cd /usr/local/src tar zxvf tiff-4.0.9.tar.gz cd tiff-4.0.9 ./configure --prefix=/usr/local/tiff --enable-shared make make install
安装libpng
cd /usr/local/src tar xvf libpng-1.6.34.tar.gz cd libpng-1.6.34 ./configure --prefix=/usr/local/libpng --enable-shared make make install #报 zlib not found错误时: export LDFLAGS="-L /usr/local/zlib/lib" export CPPFLAGS="-I /usr/local/zlib/include"
安装freetype
cd /usr/local/src tar zxvf freetype-2.9.tar.gz cd freetype-2.9 ./configure --prefix=/usr/local/freetype --enable-shared make make install
安装jpeg
cd /usr/local/src tar zxvf jpegsrc.v9a.tar.gz cd jpeg-9a ./configure --prefix=/usr/local/jpeg --enable-shared make make install
安装libgd
cd /usr/local/src tar zxvf libgd-2.2.5.tar.gz cd libgd-2.2.5 ./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 --with-zlib=/usr/local/zlib make make install
安装t1lib
cd /usr/local/src tar zxvf t1lib-5.1.2.tar.gz cd t1lib-5.1.2 ./configure --prefix=/usr/local/t1lib --enable-shared make without_doc make install
安装php
cd /usr/local/src tar zxvf php-5.6.33.tar.gz cd php-5.6.33 ./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-vpx-dir=/usr/local/libvpx/ --with-zlib-dir=/usr/local/zlib --with-t1lib=/usr/local/t1lib --with-iconv --with-ldap --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-ctype configure: error: Cannot find ldap libraries in /usr/lib 解决办法: yum install php-ldap cp -frp /usr/lib64/libldap* /usr/lib/ make make install
为php提供配置文件
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 #添加软链接到 /etc目录为php-fpm提供配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf #拷贝模板文件为php-fpm配置文件
ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf #添加软连接到 /etc目录vi /usr/local/php/etc/php-fpm.conf #编辑 user = nginx #设置php-fpm运行账号 group = nginx #设置php-fpm运行组 pid = run/php-fpm.pid #取消前面的分号 :wq! #保存退出 egrep -v "(;|^$)" /usr/local/php/etc/php-fpm.conf 设置 php-fpm开机启动 cp /usr/local/src/php-5.6.33/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm #拷贝php-fpm到启动目录 chmod +x /etc/rc.d/init.d/php-fpm #添加执行权限 chkconfig php-fpm on #设置开机启动 chkconfig |grep php-fpm 优化配置 vi /usr/local/php/etc/php.ini #编辑配置文件 找到:disable_functions = 修改为: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 #列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。 找到:;date.timezone = 修改为:date.timezone = PRC #设置时区 找到:expose_php = On 修改为:expose_php = Off #禁止显示php版本的信息 找到:short_open_tag = Off 修改为:short_open_tag = ON #支持php短标签 找到opcache.enable=0 修改为opcache.enable=1 #php支持opcode缓存 找到:;opcache.enable_cli=1 #php支持opcode缓存 修改为:opcache.enable_cli=0 在最后一行添加:zend_extension=opcache.so #开启opcode缓存功能 :wq! #保存退出 sed -i "s#expose_php = On#expose_php = Off#g" /usr/local/php/etc/php.ini sed -i "s#short_open_tag = Off#short_open_tag = ON#g" /usr/local/php/etc/php.ini sed -i "s#opcache.enable=0#opcache.enable=1#g" /usr/local/php/etc/php.ini sed -i "s#;opcache.enable_cli=1#opcache.enable_cli=0#g" /usr/local/php/etc/php.ini cat zend_extension=opcache.so >>/usr/local/php/etc/php.ini egrep -v "(;|^$)" /usr/local/php/etc/php.ini 启动 php-fpm service php-fpm start ps aux |grep php-fpm netstats -ntulp
整合Nginx与PHP (FastCGI)
vim /etc/nginx/nginx.conf #配置修改 user nginx nginx; #首行user去掉注释,修改Nginx运行组;必须与/usr/local/php/etc/php-fpm.conf中的user,group配置相同,否则php运行出错 添加 index.php主页: index index.html index.htm index.php; #添加index.php 修改nginx.conf ,启用fastcgi # 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; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; # 特别注意此文件 } #取消FastCGI server部分location的注释,注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name, 由: fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 修改为: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 编辑 /etc/nginx/fastcgi_params 添加: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; /etc/init.d/nginx restart #重启nginx service php-fpm start #启动php-fpm
测试篇
cd /usr/local/nginx/html/ #进入nginx默认网站根目录,根据实际情况修改; rm -rf /usr/local/nginx/html/* #删除默认测试页 vi index.php #新建index.php文件 <?php phpinfo(); ?> :wq! #保存退出 在浏览器中打开服务器IP地址,会看到PHP的状态信息页面。
配置PHP连接Mysql测试
提供测试文件
vim index.php
<?php
$conn=mysql_connect('localhost','root','');
if ($conn)
echo "Success";
else
echo "Failure";
?>从其它机器上连接数据库: <?php $conn=mysql_connect('192.168.1.217','root','123456'); if ($conn) echo "Success"; else echo "Failure"; ?>
转载于:https://blog.51cto.com/66585/2068620