一、配置CentOS 第三方yum源(CentOS默认的标准源里没有nginx软件包)

[[email protected] ~]# yum install wget #安装下载工具wget
[[email protected] ~]#wget http://www.atomicorp.com/installers/atomic #下载atomic yum源
[[email protected] ~]#sh ./atomic #安装
[[email protected] ~]# yum check-update #更新yum软件包

二、安装开发包和库文件

[[email protected] ~]# yum -y install ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel

三、卸载已安装的apache、mysql、php

[[email protected] ~]# yum remove httpd
[[email protected] ~]# yum remove mysql
[[email protected] ~]# yum remove php

四、安装nginx

[[email protected] ~]# yum install nginx -y
[[email protected] ~]# service nginx start
[[email protected] ~]# chkconfig –levels 235 nginx on #设置2、3、5级别开机启动

五、安装mysql

[[email protected] ~]# yum install mysql mysql-server mysql-devel -y
[[email protected] ~]# service mysqld start
[[email protected] ~]# chkconfig –levels 235 mysqld on
#为root账户设置密码
[[email protected] ~]#mysql_secure_installation #初始化MySQL
Enter current password for root (enter for none): <—输入现在的root密码,因为我们还没设置,直接回车
Set root password? [Y/n] Y <—是否设定root密码,当然设置了,输入Y回车
New password: <—输入root密码,并回车,输入的过程中不会有任何显示
Re-enter new password: <—再次输入root密码,并回车,输入的过程中不会有任何显示
Remove anonymous users? [Y/n] Y <—是否删除匿名用户,删除,输入Y回车
Disallow root login remotely? [Y/n] Y <—是否删禁止root用户远程登录,当然禁止,输入Y回车
Remove test database and access to it? [Y/n] <—是否删除测试数据库test,看个人喜好
Reload privilege tables now? [Y/n] Y <—刷新权限,输入Y回车
最后出现:Thanks for using MySQL!
MySql密码设置完成,重新启动 MySQL:
[[email protected] ~]#/etc/init.d/mysqld start #启动
[[email protected] ~]# /etc/init.d/mysqld status
[[email protected] ~]# chkconfig mysqld on

六、安装PHP

[[email protected] ~]# yum -y install php 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模式
[[email protected] ~]# yum install php-tidy php-common php-devel php-fpm php-mysql -y
[[email protected] ~]# service php-fpm start
Starting php-fpm: [ OK ]
[[email protected] ~]# chkconfig –levels 235 php-fpm on

七、配置nginx支持php

[[email protected] ~]# cp /etc/nginx/nginx.conf{,.bak} #将配置文件改为备份文件
#修改nginx配置文件,添加fastcgi支持
vim /etc/nginx/nginx.conf #编辑
user nginx nginx; #修改nginx运行账号为:nginx组的nginx用户
:wq! #保存退出
[[email protected] nginx]# cp /etc/nginx/conf.d/default.conf{,.bak}
[[email protected] nginx]# vim /etc/nginx/conf.d/default.conf 
    location / {
        root /usr/share/nginx/html;
        index index.php index.html index.htm; #增加index.php
    }
      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

八、配置php

[[email protected] nginx]# cp /etc/php.ini{,.bak}
[[email protected] ~]# vim /etc/php.ini
expose_php = Off #禁止显示php版本的信息

九、重启nginx php-fpm

[[email protected] nginx]# service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
[[email protected] nginx]# service php-fpm restart
Stopping php-fpm: [ OK ]
Starting php-fpm:   

十、测试

[[email protected] ~]# cd /usr/share/nginx/html/  
[[email protected] html]# vim index.php 
<?php
     phpinfo();
?>
[[email protected] html]# chown nginx.nginx /usr/share/nginx/html/ -R #设置目录所有者
#配置防火墙,开启80端口、3306端口
[[email protected] ~]vi /etc/sysconfig/iptables
-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端口通过防火墙)
特别提示:很多网友把这两条规则添加到防火墙配置的最后一行,导致防火墙启动失败,正确的应该是添加到默认的22端口这条规则的下面
添加好之后防火墙规则如下所示:
#########################################################
# 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 -mstate –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
-A INPUT -mstate –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
######
[[email protected] ~]/etc/init.d/iptables restart #最后重启防火墙使配置生效
在客户端浏览器输入服务器IP地址,可以看到相关的配置信息!

发表评论

电子邮件地址不会被公开。 必填项已用*标注