文章转载来源:http://www.178linux.com/75185
系统:CentOS 7.3
IP: 172.16.0.11
版本:nginx-1.10.3 php-5.6.30 mysql-5.6.30
一. 安装开发包组
yum -y groupinstall "Development Tools" "Server Platform Development"
二.编译安装nginx-1.10.3
(1) 安装依赖包
yum -y install openssl-devel pcre-devel zlib-devel
(2) 创建nginx用户和组
useradd -r nginx
(3) 编译安装
tar xf nginx-1.10.3.tar.gz
cd nginx-1.10.3/
./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --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/lock/nginx.lock --with-http_ssl_module --with-http_gzip_static_module --with-debug --with-http_stub_status_module
make && make install
(4) 启动nginx
/usr/local/nginx/sbin/nginx
(5) nginx开机启动(启动脚本)
vim /etc/rc.d/init.d/nginx
#! /bin/bash
#
# 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
# 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"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/nginx.lock
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
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
(6)授权
chmod +x /etc/rc.d/init.d/nginx
(7)设置开机自启动
chkconfig --add nginx chkconfig nginx on
(8)启动nginx
service nginx start
三.安装mysql-5.6.30
(1) 下载mysql(建议安装mysql二进制包,源码包安装很慢)
cd /usr/local/src
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.30.tar.gz
(2) 安装编译源码所需的工具和库
yum -y install cmake ncurses-devel
(3) 设置MySQL用户和组
useradd -r mysql
(4) 编译安装
cd /usr/local/src
tar xf mysql-5.6.30.tar.gz
cd mysql-5.6.30/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/data/mysqldb -DMYSQL_TCP_PORT=3306 -DENABLE_DOWNLOADS=1 -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1
(注意是使用cmake)
make -j 4 && make install
(5) 初始化数据库
mkdir -p /data/mysqldb #创建数据库数据目录
chown -R mysql.mysql /usr/local/mysql # 给程序目录授权,否则socket文件无法写入到该目录
chown -R mysql.mysql /data/mysqldb/ #给数据目录授权
cd /usr/local/mysql/
./scripts/mysql_install_db --user=mysql --datadir=/data/mysqldb
参考: /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
(6) 准备mysql配置文件
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf (注:如果/etc/my.cnf文件存在,则覆盖。)
vim /etc/my.cnf
[mysqld]
...
datadir = /data/mysqldb
innodb_file_per_table = ON
skip_name_resolve = ON
...
(7) 复制mysql启动脚本
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
sed -i 's@^basedir=@basedir=/usr/local/mysql@' /etc/init.d/mysqld
sed -i 's@^datadir=@datadir=/data/mysqldb@' /etc/init.d/mysqld
(8) 启动mysql服务并设置开机自启动
service mysqld start
chkconfig --add mysqld chkconfig --level 35 mysqld on
(9) 设置环境变量
echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
(10) 输出mysql的头文件至系统头文件路径/usr/include(非必须)
ln -s /usr/local/mysql/include/ /usr/include/mysql
(11) 输出mysql的库文件给系统库查找路径(非必须)
echo "/usr/local/mysql/lib/" >/etc/ld.so.conf.d/mysql.conf
ldconfig
四. php安装
(1) PHP添加libmcrypt拓展
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install libxml2-devel libmcrypt-devel bzip2-devel curl-devel
(2) 解决依赖
yum -y install libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype freetype-devel php-pear
(3) 编译安装php-5.6
tar xf php-5.6.30.tar.bz2
cd php-5.6.30/
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-gd --enable-gd-native-ttf --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-mcrypt=/usr/local/libmcrypt/ --with-gettext
make && make install
(4) 复制php配置文件
cp php.ini-production /usr/local/php/etc/php.ini
(5) 复制php-fpm配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
(6) 复制php-fpm启动脚本到init.d,设置开机启动
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cd
chmod +x /etc/rc.d/init.d/php-fpm
chkconfig --add php-fpm chkconfig php-fpm on
(7) 启动php-fpm
service php-fpm start
(8) 修改nginx配置文件使之支持php
vim /etc/nginx/nginx.conf
解除注释:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
备注: #取消FastCGI server部分location的注释,并要注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径
测试一下是否有错:
/usr/local/nginx/sbin/nginx -t
平滑重启nginx
/usr/local/nginx/sbin/nginx -s reload
五. 测试
vim /usr/local/nginx/html/index.php
<?php
phpinfo()
?>
<?php
\$conn = mysql_connect('127.0.0.1','root','123456');
if (\$conn)
echo "OK";
else
echo "Failure";
?>
浏览器测试即可