以下软件nginx版本:1.8.1 mysql:5.6.30 php:5.6.27
一、安装nginx
1、安装pcre-devel yum install pcre pcre-devel (rpm -qa pcre pcre-devel 检查是否安装)[ubuntu用这个命令:apt-get install libpcre3 libpcre3-dev]
2、安装openssl-devel yum install openssl openssl-devel (rpm -qa openssl openssl-devel 检查是否安装)
3、安装nginx
(1)、./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
(2)、 make
(3)、 make install
(4)、 groupadd nginx(添加用户组 --group=nginx指定的) useradd -g nginx nginx(添加用户 --user=nginx指定的)
(5)、 /usr/local/nginx/sbin/nginx -t (检查nginx的配置文件是否正确)
(6)、 /usr/local/nginx/sbin/nginx (启动nginx)
(7)、 curl 127.0.0.1 (查看nginx是否正常启动)
【备注】
//启动nginx
**第一种方式 指定--sbin-path=/usr/sbin/nginx** (安装nginx的时候 指定 如:./configure --sbin-path=/usr/sbin/nginx ......)
nginx //启动
nginx -s stop// 停止
nginx -s reload // 重新加载
**第二种方式 不指定--sbin-path**
cd /usr/local/nginx
./sbin/nginx
重启nginx /usr/local/nginx/sbin/nginx -s reload
**第三种方式**
配置开机启动
首先写一个shell脚本,脚本名称:nginx
vi /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"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/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' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
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
【备注】需要自己手动配置以下两项:
nginx="/usr/sbin/nginx" 修改成nginx执行程序的路径。
NGINX_CONF_FILE="/etc/nginx/nginx.conf" 修改成配置文件的路径。
chmod +x /etc/init.d/nginx (设置可执行权限)
chkconfig --add nginx 或 chkconfig --add /etc/init.d/nginx (添加系统服务)
chkconfig nginx on (设置终端模式开机启动)
service nginx start
service nginx stop
service nginx restart
service nginx reload
浏览器访问:http://localhost如能出现nginx页面则表示成功
// 查看nginx进程
ps -ef | grep nginx
// 查看进程个数 去掉首位的
ps -ef | grep nginx | wc -l
// 查看80端口
netstat -anpt
二、安装php(FastCGI方式安装)
1、检查安装php所需的lib库
rpm -qa zlib-devel (如果没有安装 yum install zlib-devel)【ubuntu用: sudo apt-get install zlib1g 和 sudo apt-get install zlib1g.dev】
rpm -qa libxml2-devel (如果没有安装 yum install libxml2-devel ) 【ubuntu用:sudo apt-get install libxml2-dev】
rpm -qa libpng-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel gd-devel libcurl-devel libxslt-devel
(如果没有安装相应的lib库 用yum 安装)
【备注】一般每个lib都会存在对应的以 “*-devel*” 命名的包,安装lib对应的 -devel包后,对应的lib包就会自动安装好。如:yum install gd-devel,就会安装gd。
2、编译安装yum无法安装的 libiconv 库
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
./configure --prefix=/usr/local/libiconv
make
make install
3、安装 libmcrypt 库
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum install libmcrypt-devel
【备注】也可以用源码包编译安装
4、检查安装 mhash 加密扩展库、
rpm -qa mhash (检查是否安装)
yum install mhash
5、检查安装 mcrypt 加密扩展库
rpm -qa mcrypt
rpm -qa mcrypt-devel
yum install mcrypt
6、编译安装php
(1)、 安装php
./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --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 --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=nginx --with-fpm-group=nginx --enable-ftp
make
make install
【备注】安装php的时候也可以用 --with-mysql=mysql安装路径 --with-mysqli=mysql-mysqli路径 --with-pdo-mysql=mysql—pdo路径 ,
但是不推荐使用这种方法。推荐使用 mysqlnd 的方法。
(2)、 拷贝php配置文件
cp php.ini-development /usr/local/php/lib/php.ini
【备注】php.ini-development 为源码包中的文件
(3)、 配置php-fpm
cd /usr/local/php/etc
cp php-fpm-default.conf php-fpm.conf
(4)、 拷贝php-fpm脚本至/etc/init.d目录
cp /home/xiaoge/tools/php-5.6.27/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
【备注】/home/xiaoge/tools/ 为php源码包所在目录
(5)、 设置权限并启动php-fpm:
chmod 755 /etc/init.d/php-fpm
/etc/init.d/php-fpm start
chkconfig --add php-fpm
vim /usr/local/php/etc/php-fpm.conf
大约25行左右将 ;pid = run/php-fpm.pid 注释去掉
在 /usr/local/php/var/run 目录新建 php-fpm.pid 文件
如果不执行去掉php-fpm.conf注释和新建php-fpm.pid文件的话,第六步的操作会报错
(6)、 最后,给出php-fpm以服务的方式启动、停止和重启:
service php-fpm start
service php-fpm stop
service php-fpm reload
(7)、 正常模式,不以服务的方式启动
/usr/local/php/sbin/php-fpm 启动
(8)、 配置nginx支持php(修改nginx.conf)
user nginx nginx; #修改nginx运行账号为:nginx组的nginx用户(--with-fpm-user=nginx --with-fpm-group=nginx 指定的)
【备注】大约第2行
index index.php index.html index.htm; #增加index.php
【备注】大约第45行
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,或者使用绝对路径
(9)、虚拟主机设置
(a、打开 /usr/local/nginx/conf/nginx.conf
在最后加入如下一行
# 包含所有的虚拟主机的配置文件 在conf目录下新建vhosts目录 添加相应的.conf配置文件
include vhosts/*.conf;
(b、新建vhosts目录 mkdir vhosts
在vhosts目录下新建配置文件 如:test.conf
在test.conf 文件中加入如下内容:
server {
listen 80;
server_name mytestnginx.com www.mytestnginx.com;
#access_log log/test.log;
location / {
root html/test;
index index.php index.html index.htm;
}
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# root /usr/share/nginx/html;
# }
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ .php$ {
# 这里的值对应的是$document_root
root html/test;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#注意这里的路径
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
(c、查看配置文件是否正确
/usr/local/nginx/sbin/nginx -t
(d、重启nginx
/usr/local/nginx/sbin/nginx -s reload
(e、配置hosts文件
vim /etc/hosts 添加 127.0.0.1 mytestnginx.com www.mytestnginx.com
(f、访问查看是否能正常访问
curl mytestnginx.com
curl www.mytestnginx.com
【nginx】 关闭防火墙
centos 7:
systemctl stop firewalld.service #停止
systemctl disable firewalld.service #禁用
之前的版本:
service iptables stop #停止
chkconfig iptables off #禁用
三、安装mysql(源码包编译安装,也可以选择二进制包安装)
1、检查是否安装 cmake
rpm -qa cmake (若没有安装 yum install cmake)
2、设置MySQL用户和组
新增mysql用户组 groupadd mysql
新增mysql用户 useradd -r -g mysql mysql
3、新建MySQL所需要的目录
新建mysql安装目录 mkdir -p /usr/local/mysql
新建mysql数据库数据文件目录 mkdir -p /data/mysqldb
4、检查是否安装 ncurses-devel ncurses
rpm -qa ncurses-devel ncurses
yum install ncurses-devel (ubuntu 需要执行 apt-get install libncurses5-dev)
【备注: 从MySQL 5.7.5开始Boost库是必需的 , 所以如果安装mysql5.7及以上版本的话需要安装boost】
wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
tar xzvf boost_1_59_0.tar.gz
cmake时加上参数 -DWITH_BOOST=解压后boost所在的目录
5、编译安装mysql
cd /home/xiaoge/tools/mysql-5.6.30 切换到mysql源码包目录
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
(注:重新运行配置,需要删除CMakeCache.txt文件 rm CMakeCache.txt )
make
make install
编译选项说明:
指定安装文件的安装路径时常用的选项
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql ----指定残可安装路径(默认的就是/usr/local/mysql)
-DMYSQL_DATADIR=/data/mysql ----mysql的数据文件路径
-DSYSCONFDIR=/etc ----配置文件路径
编译过程中启用其他存储引擎时指令介绍
-DWITH_INNOBASE_STORAGE_ENGINE=1 ----使用INNOBASE存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 ----常应用于日志记录和聚合分析,不支持索引
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 ----黑洞存储引擎
编译过程中取消一些存储引擎指令介绍
-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1
示例如下:
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1
-DWITHOUT_PARTITION_STORAGE_ENGINE=1
编译进过程中功能启用的指令介绍
-DWITH_READLINE=1 ----支持批量导入mysql数据
-DWITH_SSL=system ----mysql支持ssl会话,实现基于ssl的数据复
-DWITH_ZLIB=system ----压缩库
-DWITH_LIBWRAP=0 ----是否可以基于WRAP实现访问控制
其他功能指令
-DMYSQL_TCP_PORT=3306 ----默认端口
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock ----默认套接字文件路径
-DENABLED_LOCAL_INFILE=1 ----是否启用LOCAL_INFILE功能
-DEXTRA_CHARSETS=all ----是否支持额外的字符集
-DDEFAULT_CHARSET=utf8 ----默认编码机制
-DDEFAULT_COLLATION=utf8_general_ci ----设定默认语言的排序规则
-DWITH_DEBUG=0 ----DEBUG功能设置
-DENABLE_PROFILING=1 ----性能分析功能是否启用
6、修改mysql目录所有者和组
修改mysql安装目录:
cd /usr/local/mysql
chown -R mysql:mysql . (注意后面的.指的是当前目录)
修改mysql数据库文件目录:
cd /data/mysqldb
chown -R mysql:mysql .(注意后面的.指的是当前目录)
7、初始化mysql数据库
cd /usr/local/mysql
./scripts/mysql_install_db –user=mysql –datadir=/data/mysqldb
./scripts/mysql_install_db ––user=mysql ––basedir=/usr/local/mysql5.6_1 ––datadir=/usr/local/mysql5.6_1/data ––skip-name-resolve–user=mysql ––defaults-file=/usr/local/mysql5.6_1/conf/my.cnf ––pid-file=/usr/local/mysql5.6_1/var/mysql.pid ––socket=/usr/local/mysql5.6_1/var/mysql.sock
8、复制mysql服务启动配置文件
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
(注:如果/etc/my.cnf文件存在,则覆盖。)
9、添加mysql到服务,设置开机自启动
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld #增加可执行权限
chkconfig mysqld on #开机自启动
service mysqld start 启动mysql
chkconfig –level 35 mysqld on
10、检查mysql服务是否启动,把mysql加入环境变量
netstat -tulnp | grep 3306 检查是否启动
把mysql加入环境变量:
vim /etc/profile
在最后加入如下命令
MYSQL_HOME=/usr/local/mysql
PATH=$MYSQL_HOME/bin:$PATH
export PATH MYSQL_HOME
source /etc/profile 重新读取环境变量
mysql -u root -p (密码为空,如果能登陆上,则安装成功)
11、修改MySQL用户root的密码
update user set password=password('自己设置的密码') where user='root'
flush privileges(刷新权限)
【备注】安装mysql和php步骤可以互换
四、redis的安装 (参考 http://blog.sina.com.cn/s/blog_5f66526e0102vypm.html)
1、下载redis:
wget http://download.redis.io/releases/redis-3.2.1.tar.gz
2、解压并安装
(1、 cd redis-3.2.5
(2、 make
(3、 make PREFIX=/usr/local/redis install
3、配置redis
(1、mkdir -p /usr/local/redis/etc
将源码包中的redis.conf 复制到redis的安装目录中(/usr/local/redis/etc) cp redis.conf /usr/local/redis/etc/
(2、设置redis为服务
mkdir /etc/redis
ln -s /usr/local/redis/etc/redis.conf /etc/redis/6379.conf
cp .utils/unredis_init_script /etc/init.d/redis
vi /etc/init.d/redis
在第二行加
#chkconfig: 2345 80 90
修改 EXEC和CLIEXEC选项为自己的redis安装目录
(3、添加设置redis为开机自启动
chkconfig --add redis
chkconfig redis on
卸载redis:
把redis的安装目录 和 源码包目录一块删除
五、安装php7,在nginx环境中同时运行多个php(php5.6和php7)
1、下载php7 wget http://hk2.php.net/get/php-7.1.3.tar.gz/from/this/mirror
2、解压安装(因为之前装过php5.6,所以相关的库都已安装好)
(1)、 安装php
./configure --prefix=/usr/local/php7 --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --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 --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=nginx --with-fpm-group=nginx --enable-ftp
make
make install
【备注】--prefix=/usr/local/php7(这里不能和已安装的php目录冲突,否则会覆盖掉已安装的php)
安装php的时候也可以用 --with-mysql=mysql安装路径 --with-mysqli=mysql-mysqli路径 --with-pdo-mysql=mysql—pdo路径 ,
但是不推荐使用这种方法。推荐使用 mysqlnd 的方法。
(2)、 拷贝php配置文件
cp php.ini-development /usr/local/php7/etc/php.ini
【备注】php.ini-development 为源码包中的文件
(3)、 配置php-fpm
a、
cd /usr/local/php7/etc
cp php-fpm.conf.default php-fpm.conf
b、
cd /usr/local/php7/etc/php-fpm.d/
cp www.conf.default www.conf
将www.conf中的 listen = 127.0.0.1:9000 改为 listen = 127.0.0.1:9001 (9000端口被php5.6占用)
(4)、 拷贝php-fpm脚本至/etc/init.d目录
cp /home/xiaoge/tools/php-7.1.3/sapi/fpm/init.d.php-fpm /etc/init.d/php7-fpm
【备注】/home/xiaoge/tools/ 为php源码包所在目录
/etc/init.d/php7-fpm (php7-fpm不能为php-fpm,否则会覆盖掉已安装的php-fpm服务)
(5)、 设置权限并启动php7-fpm:
chmod 755 /etc/init.d/php7-fpm
/etc/init.d/php7-fpm start
chkconfig --add php7-fpm #添加系统服务
chkconfig php7-fpm on #开机自启动
(6)、nginx 配置:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
【备注】fastcgi_pass 127.0.0.1:9001; (这里为php7的端口)
以上一个nginx可以同时运行多个php,php监听不同的端口