你的位置:首页 > 软件开发 > 操作系统 > 烂泥:学习centos之快速搭建LNMP环境

烂泥:学习centos之快速搭建LNMP环境

发布时间:2015-07-14 22:00:06
本文由秀依林枫提供友情赞助,首发于烂泥行天下 以前在centos下安装软件都是喜欢源码安装,不过昨天因为一个事情需要一个centos 下的LNMP环境。反倒不会搞了,今天特意记录下,以备后续使用。 一、安装nginx 我们先来安装nginx,如下: yum -y inst ...

烂泥:学习centos之快速搭建LNMP环境

本文由秀依林枫提供友情赞助,首发于烂泥行天下

以前在centos下安装软件都是喜欢源码安装,不过昨天因为一个事情需要一个centos 下的LNMP环境。反倒不会搞了,今天特意记录下,以备后续使用。

一、安装nginx

我们先来安装nginx,如下:

yum -y install nginx

烂泥:学习centos之快速搭建LNMP环境

通过上图,我们可以看到目前的yum源中是没有nginx软件包的。我们需要安装包含nginx的yum源,如下:

rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

烂泥:学习centos之快速搭建LNMP环境

查看刚刚安装的rpm包,如下:

rpm -ql nginx-release-centos-6-0.el6.ngx.noarch

烂泥:学习centos之快速搭建LNMP环境

通过上图,我们可以很明显的看出刚刚安装的rpm包,只是新添加了一个nginx的yum源。查看该yum源,如下:

cat /etc/yum.repos.d/nginx.repo

烂泥:学习centos之快速搭建LNMP环境

现在我们再来通过yum来安装nginx,如下:

yum -y install nginx

烂泥:学习centos之快速搭建LNMP环境

通过上图,我们可以很明显的看出nginx已经安装。

现在来查看nginx安装的位置及其文件,如下:

rpm -ql nginx

烂泥:学习centos之快速搭建LNMP环境

通过上图,我们可以很明显的看出nginx默认安装到/etc/nginx目录,而nginx的默认网站安装到/usr/share/nginx/html目录下。

现在我们来启动nginx,并访问nginx。如下:

/etc/init.d/nginx start

curl http://192.168.1.124

烂泥:学习centos之快速搭建LNMP环境

烂泥:学习centos之快速搭建LNMP环境

查看nginx运行时使用的用户,如下:

ps -ef |grep nginx

烂泥:学习centos之快速搭建LNMP环境

通过上图,我们可以很明显的看出nginx是root用户启动,但是nginx运行时使用的是nginx这个用户。

二、安装php及php-fpm

安装php及php-fpm,使用如下命令:

yum -y install php php-fpm

烂泥:学习centos之快速搭建LNMP环境

查看php-fpm安装的位置及其文件,如下:

rpm -ql php-fpm

烂泥:学习centos之快速搭建LNMP环境

启动php-fpm,使用如下命令:

/etc/init.d/php-fpm start

ps -ef |grep php-fpm

netstat -tunlp |grep 1355

烂泥:学习centos之快速搭建LNMP环境

通过上图,我们可以看出php-fpm运行时使用apache这个用户,而且php-fpm监听的是本机的9000端口。

如果要修改php-fpm运行时的用户及端口的话,我们可以通过修改php-fpm配置文件/etc/php-fpm.d/www.conf,如下:

cat /etc/php-fpm.d/www.conf |grep -v '^;'|grep -v ^$

烂泥:学习centos之快速搭建LNMP环境

三、nginx与php集成

nginx与php集成是通过fastcgi来实现,而fastcgi我们一般使用的是php-fpm。在第二章,我们已经启动php-fpm,现在我们来修改nginx配置文件使其支持php。

现在我们修改nginx的默认网站配置文件default.conf,如下:

cat /etc/nginx/conf.d/default.conf

server {

listen 80;

server_name localhost;

location / {

root /usr/share/nginx/html;

index index.php index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/html;

}

location ~ \.php$ {

root /usr/share/nginx/html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

烂泥:学习centos之快速搭建LNMP环境

以上配置完毕后,我们再重启nginx和php-fpm,如下:

/etc/init.d/nginx restart

/etc/init.d/php-fpm restart

烂泥:学习centos之快速搭建LNMP环境

烂泥:学习centos之快速搭建LNMP环境

以上是修改nginx的默认网站,现在我们新加一个nginx虚拟主机,并使其支持php,如下:

vi /etc/nginx/conf.d/ilanni.conf

server {

listen 80;

server_name test.ilanni.com;

location / {

root /ilanni;

index index.php index.html index.htm;

}

location ~ \.php$ {

root /ilanni;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

烂泥:学习centos之快速搭建LNMP环境

nginx虚拟主机配置文件完毕后,现在来创建虚拟主机ilanni的index.php,如下:

mkdir /ilanni

vi /ilanni/index.php

<?php phpinfo(); ?>

烂泥:学习centos之快速搭建LNMP环境

修改ilanni目录的所属用户,如下:

chown nginx:nginx -R /ilanni/

ll /ilanni/

烂泥:学习centos之快速搭建LNMP环境

如上图所示,我们现在把ilanni这个目录修改为nginx运行时使用的用户nginx。

现在我们再来修改php-fpm运行时的用户,如下:

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

烂泥:学习centos之快速搭建LNMP环境

我们来重启nginx与php-fpm,如下:

/etc/init.d/nginx restart

/etc/init.d/php-fpm restart

烂泥:学习centos之快速搭建LNMP环境

烂泥:学习centos之快速搭建LNMP环境

通过上图,我们可以看到nginx的虚拟主机已经可以正确解析php。

四、安装mysql

安装MySQL,使用如下命令:

yum -y install mysql mysql-server php-mysql

烂泥:学习centos之快速搭建LNMP环境

安装完毕后,启动mysql。如下:

/etc/init.d/mysqld start

烂泥:学习centos之快速搭建LNMP环境

现在我们在nginx默认网站创建一个mysql.php文件,用来连接mysql数据库。mysql.php内容如下:

cat /usr/share/nginx/html/mysql.php

<?php

$host='localhost';

$root='root';

$pwd='';

$con= mysql_connect($host,$root,$pwd);

if ( $con == false ) {

echo "connect false";

} else{

echo "connect true";

}

?>

烂泥:学习centos之快速搭建LNMP环境

重启nginx、php-fpm、mysql,如下:

/etc/init.d/nginx restart

/etc/init.d/php-fpm restart

/etc/init.d/mysqld restart

烂泥:学习centos之快速搭建LNMP环境

现在通过访问该mysql.php文件,如下:

http://192.168.1.124/mysql.php

烂泥:学习centos之快速搭建LNMP环境

通过上图,我们可以很明显的看出php已经解析mysql.php文件,并且也已经成功的连接mysql数据库。


 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:烂泥:学习centos之快速搭建LNMP环境

关键词:Centos

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。