1、配置防火墙,开启80端口、3306端口
vi /etc/sysconfig/iptables
//在22端口的下一行添加
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT //(允许80端口通过防火墙)
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT //(允许3306端口通过防火墙)
//保存退出,重启防火墙使配置生效
/etc/init.d/iptables restart
注:笔者在阿里云ecs上操作时,主机没有安装iptables
yum -y install iptables
service iptables start
//此时提示启动失败,没有配置文件
vi /etc/sysconfig/iptables
//写入以下内容
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
//保存退出,service iptables start 此时启动成功
2、关闭SELINUX(笔者没有操作此项,感觉不用管)
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
shutdown -r now #重启系统
3、配置第三方yum源(CentOS默认的标准源里没有nginx软件包)
yum install wget #安装下载工具wget
wget http://www.atomicorp.com/installers/atomic #下载atomic yum源
sh ./atomic #安装
yum check-update #更新yum软件包
4、安装nginx
yum -y install nginx #安装nginx,根据提示,输入Y安装即可成功安装
service nginx start #启动
chkconfig nginx on #设为开机启动
/etc/init.d/nginx restart #重启
rm -rf /usr/share/nginx/html/* #删除ngin默认测试页
注:笔者在阿里云ecs上操作时,安装完后提示启动失败
centos6.5环境
修改nginx配置文件后,重启报错:
nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
解决办法:
vi /etc/nginx/conf.d/default.conf
将
listen 80 default_server;
listen [::]:80 default_server;
改为:
listen 80;
#listen [::]:80 default_server;
重新启动nginx即可,网上说的那种kill掉nginx进程和ipv6 on的方法不适用
5、安装MySQL
yum -y install mysql mysql-server mysql-devel
service mysqld start
chkconfig --levels 235 mysqld on
//登陆MySQL删除空用户,修改root密码
mysql>select user,host,password from mysql.user;
mysql>drop user ''@localhost;
mysql>update mysql.user set password = PASSWORD('*********') where user='root';
mysql>flush privileges;
6、安装PHP
//安装依赖库
yum -y install php lighttpd-fastcgi php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-mssql php-snmp php-soap
//安装php和所需组件使PHP支持MySQL、FastCGI模式
yum -y install php-tidy php-common php-devel php-fpm php-mysql
service php-fpm start
chkconfig --levels 235 php-fpm on
7、配置nginx支持php
//将配置文件改为备份文件
mv /etc/nginx/nginx.conf /etc/nginx/nginx.confbak
//由于原配置文件要自己去写因此可以使用默认的配置文件作为配置文件
cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf
//修改nginx配置文件,添加fastcgi支持
vi /etc/nginx/nginx.conf
//加入index.php
index index.php index.html index.htm;
//将以下代码注释去掉,并修改成nginx默认路径
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
8、配置php
//编辑文件php.ini
[root@CentOS ~]# vi /etc/php.ini
//在阿里云ECS主机上操作时发现session不可用,如下处理
//根据php-fpm的session配置信息(tail /etc/php-fpm.d/www.conf)
//修改php.ini中的session目录如下
session.save_path = "/var/lib/php/session"
//并且session目录的所有者要设为php-fpm的process user,笔者这里是apache
ps -ef | grep php-fpm
chown root:apache /var/lib/php/session
//在文件末尾添加
cgi.fix_pathinfo = 1
9、重启nginx php-fpm
service nginx restart
service php-fpm restart
10、测试验证
//如nginx自定义项目目录,要给读和执行权限
vi /usr/share/nginx/html/info.php
//保存退出
11:测试nginx是否解析php
本地浏览器输入:192.168.1.105/info.php
显示phpinfo界面 环境搭建成功