nginx +php + redis和 mysql 集群部署_CentOS安装配置LNMP(Linux+Nginx+PHP+MySQL)和Redis

准备篇

1、配置防火墙

开启80端口、3306端口

/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT

/sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT

然后保存:

#/etc/rc.d/init.d/iptables save

再查看是否已经有了:

/etc/init.d/iptables status

表格:filter

Chain INPUT (policy ACCEPT)

num  target     prot opt source               destination

1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:3306

2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22

3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80

Chain FORWARD (policy ACCEPT)

num  target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)

num  target     prot opt source               destination

重启防火墙使配置生效

/etc/init.d/iptables restart

2、关闭SELINUX

vim /etc/selinux/config

#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled #增加 重启系统

shutdown -r now

3、安装第三方yum源

安装下载工具

yum install wget下载atomic源

wget http://www.atomicorp.com/installers/atomic安装atomic源

sh ./atomic更新yum源

yum check-update

安装篇

1、安装nginx

yum remove httpd* php* #删除系统自带的软件包

yum install nginx #安装nginx 根据提示输入y进行安装

chkconfig nginx on #设置nginx开机启动

service nginx start #启动nginx

2、安装MySQL

yum install mysql mysql-server启动MySQL

/etc/init.d/mysqld start设为开机启动

chkconfig mysqld on拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)

cp /usr/share/mysql/my-medium.cnf /etc/my.cnf为root账户设置密码

mysql_secure_installation回车,根据提示输入Y,输入2次密码,回车,根据提示一路输入Y,最后出现:Thanks for using MySQL!

MySql密码设置完成,重新启动 MySQL:

/etc/init.d/mysqld restart #重启

3、安装PHP5

yum install php php-fpm安装PHP组件,使 PHP5 支持 MySQL,选择安装包进行安装,根据提示输入Y回车

yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt设置php-fpm开机启动

chkconfig php-fpm on启动php-fpm

/etc/init.d/php-fpm start

4、安装redis

centos默认的安装源在官方centos.org上,而redis在第三方的yum源里,前面已经安装过atomic了,所以直接:

yum install redis

5、安装php-redis扩展

cd /tmp

wget https://github.com/nicolasff/phpredis/zipball/master -O php-redis.zip

unzip php-redis.zip解压后的文件夹为nicolasff-phpredis-c1f862c

/usr/bin/phpize (如果这里出现Can’t find PHP headers in /usr/include/php,The php-devel package is required...,执行yum install php-devel)

./configure

make

make install确认一下so文件已经放置到正确目录

ll /usr/lib/php/modules/redis.so

配置篇

1、配置nginx支持php

备份原有配置文件

cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak编辑

vim /etc/nginx/nginx.conf

#修改nginx运行账号为:nginx组的nginx用户

user nginx nginx;

备份原有配置文件

cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.confbak编辑

vim /etc/nginx/conf.d/default.conf

#增加index.php

index index.php index.html index.htm;

#取消FastCGI server部分location的注释,并要注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径

# 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;

}

如果需要兼容pathinfo模式,要这么改:

location ~ \.php { #去掉$

root html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_split_path_info ^(.+\.php)(.*)$;#增加这一句

fastcgi_param PATH_INFO $fastcgi_path_info;#增加这一句

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

重启nginx

service nginx restart

2、php配置

vim /etc/php.ini

;修改时区

date.timezone = PRC

;列出PHP可以禁用的函数,如果某些程序需要用到某个函数,可以删除,取消禁用

disable_functions =passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,ope

nlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdns

rr,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版本的信息

expose_php = Off

;打开magic_quotes_gpc来防止SQL注入

magic_quotes_gpc = On

;支持php短标签

short_open_tag = ON

;设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站,如果改了之后安装程序有问题(例如:织梦内容管理系统),可以注销此行,或者直接写上程序的目录

open_basedir = .:/tmp/

3、配置php-fpm

备份原有配置文件

cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.confbak编辑

vim /etc/php-fpm.d/www.conf

;修改用户为nginx

user = nginx

;修改组为nginx

group = nginx

4、配置redis

备份原有配置文件

cp /etc/redis.conf /etc/redis.confbak编辑

vim /etc/redis.conf

#是否以后台守护进程运行

daemonize yes

#如果以后台进程运行的话,就需要指定pid,你可以在此自定义redis.pid文件的位置。

pidfile /var/run/redis/redis.pid

#指定log文件

logfile /var/log/redis/redis.log运行redis:

/usr/sbin/redis-server /etc/redis.conf

5、启用php-redis扩展

vim /etc/php.ini

#加入

extension=redis.so

测试篇

编辑

cd /usr/share/nginx/html

vim index.php

phpinfo();

?>设置权限

chown nginx.nginx /usr/share/nginx/html -R重启nginx

service nginx restart重启php-fpm

service php-fpm restart在客户端浏览器输入服务器IP地址,可以看到相关的配置信息,说明lnmp配置成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值