在CentOS 6.5 环境下利用yum搭建LNMP环境

本文详细介绍如何在CentOS 6.5上安装配置LNMP(Linux+Nginx+MySQL+PHP)环境,包括系统安装、防火墙及SELINUX配置、安装Nginx、MySQL和PHP及其相关组件,并对Nginx和PHP进行必要的配置。

一、安装CentOS 6.5 ,采用最小安装min无桌面模式

二、开启网卡,确保联网可执行yum命令

[root@localhost ~]# vi /etc/sysconfig/network-script/ifcfg-eth0

       将onboot=no改成yes

三、配置防火墙,开启80端口和3306端口

[root@localhost ~]# vi /etc/sysconfig/iptables

            -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

[root@localhost ~]# /etc/init.d/iptables restart        //重启防火墙使配置生效

四、关闭SELINUX

[root@localhost ~]# vi /etc/sysconfig/iptables

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

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

五、安装Nginx

[root@localhost ~]# yum -y install nginx        //安装nginx软件
[root@localhost ~]# service nginx start          //启动
[root@localhost ~]# chkconfig nginx on        // 设置开机启动
[root@localhost ~]# /etc/init.d/nginx restart         //重启nginx服务

六、安装MySQL

[root@localhost ~]# yum install mysql mysql-server -y        //安装mysql
[root@localhost ~]# /etc/init.d/mysqld start                        // 启动mysql
[root@localhost ~]# chkconfig mysqld on                            //设置开机启动
[root@localhost ~]# cp /usr/share/mysql/my-medium.cnf  /etc/my.cnf     //拷贝文件覆盖原有的
[root@localhost ~]# reboot            //重启系统
[root@localhost ~]# mysql_secure_installation        //为root设置密码以及其他安全设置
[root@localhost ~]# service mysqld restart            //重启mysql

七、安装PHP

[root@localhost ~]# yum install php -y
[root@localhost ~]# yum -y install php-mysql phpgd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel php-fpm
[root@localhost ~]# /etc/init.d/mysqld restart        //重启mysql服务
[root@localhost ~]# /etc/init.d/nginx restart          // 重启nginx服务
[root@localhost ~]# /etc/rc.d/init.d/php-fpm start     //启动php-fpm服务
[root@localhost ~]# chkconfig php-fpm on        //设置开机启动

八、配置Nginx支持PHP  

[root@localhost ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak        //备份
[root@localhost ~]# vi /etc/nginx/nginx.conf                //修改用户名和组
     user     nginx   nginx;   #修改 nginx 运行账号为:nginx 组的 nginx 用 户!
[root@localhost ~]# cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak//备份
[root@localhost ~]# rm /etc/nginx/conf.d/default.conf      //删除default.conf
[root@localhost ~]# vi /etc/nginx/conf.d/default.conf        //创建并将下列内容写入default.conf
server{
    listen      80;
    server_name _;
    index index.php index.html index.htm;
    root  /var/www;

    location ~ .*\.(php|php5)?$
    {
            #fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
}

注释:/var/www 为web根目录, location / ... 为url的rewrite,隐藏 index.php

      九、配置PHP

[root@localhost ~]# vim /etc/php.ini

        date.timezone = PRC
        expose_php = Off
        #;open_basedir = .:/tmp/        //注释掉这行

      十、配置php-fpm

[root@localhost ~]# vim /etc/php-fpm.d/www.conf        //编辑

        user = nginx           //编辑用户为nginx
        group = nginx        //修改组为nginx

[root@localhost ~]# /etc/init.d/mysqld restart     //重启mysql
[root@localhost ~]# /etc/init.d/nginx restart        //启动nginx
[root@localhost ~]# /etc/rc.d/init.d/php-fpm restart        //重启Php-fpm

十一、测试

[root@localhost ~]# cd /var/www
[root@localhost ~]# vi index.php

写入以下代码

<?php
phpinfo();
?>
[root@localhost ~]# chown nginx.nginx /var/www -R        //设置权限
[root@localhost ~]# service nginx restart
[root@localhost ~]# service php-fpm restart

最后可以看到网页成功显示,说明LNMP已经搭建好。

注意事项:可能会有 “Unable to connect”的情况,如要在客户机得到显示效果,可关闭防火墙。

转载于:https://my.oschina.net/drathin/blog/877991

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值