一、安装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
写入以下代码
[root@localhost ~]# chown nginx.nginx /var/www -R //设置权限[root@localhost ~]# service nginx restart[root@localhost ~]# service php-fpm restart
最后可以看到网页成功显示,说明LNMP已经搭建好。
注意事项:可能会有 “Unable to connect”的情况,如要在客户机得到显示效果,可关闭防火墙。