LNMP(一):Ubuntu安装Nginx、MySQL、PHP环

2016-06-19  本文已影响380人  littledust

 参考Ubuntu中文wiki:http://wiki.ubuntu.org.cn/Nginx

0. 版本信息

nginx:1.4.6

php:5.5.9

MySQL:5.5

1. 安装nginx

sudo apt-get install nginx

nginx文件结构


/etc/nginx:所有配置文件,虚拟主机在/etc/nginx/sites-available下 |

/usr/sbin/nginx:程序文件路径

/var/log/nginx:日志文件

/etc/init.d/nginx:nginx启动脚本

2. 启动nginx

sudo /etc/init.d/nginx start

此时访问http://localhost/,应该能正常访问。

可能的问题


80端口被占用

Starting nginx: [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)

修改文件:/etc/nginx/sites-available/default,去掉 listen 前面的 # 号 , # 号在该文件里是注释的意思 , 并且把 listen 后面的 80 端口号改为自己的端口,访问是需要添加端口号。

403错误

可能是nginx配置文件里的网站路径不正确,修改/etc/nginx/sites-available的root网站路径

3. 配置PHP和MySQL

安装php和MySQL

sudo apt-get install php5-cli php5-cgi mysql-server php5-mysql mysql-client

安装FastCgi(Common Gateway Interface,通用网关接口)

sudo apt-get install php5-fpm

配置nginx

修改/etc/nginx/sites-available/default 修改主机名

server_name localhost;

修改首页

index index.php index.html index.htm;

去掉以下配置的注释以支持php脚本

location ~ \.php$ {

include /etc/nginx/fastcgi_params; #需放在第一行,否则会出错

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;

}

重启nginx

/etc/init.d/nginx stop

/etc/init.d/nginx start

启动fastcgi

spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi

让php-cgi开机自启动

为了让php-cgi开机自启动: Ubuntu开机之后会执行/etc/rc.local文件中的脚本 所以我们可以直接在/etc/rc.local中添加启动脚本。 spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi 添加到语句:exit 0 前面才行

创建、测试phpinfo

sudo vi /usr/share/nginx/html/info.php

测试

打开http://localhost/info.php

测试MySQL

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

<?php

header("content-type:text/html;charset=utf-8");

$conn = mysql_connect('localhost','root','chenxu0909');

if(!$conn) {

echo 'connect mysql error'.mysql_error();

die();

}

else {

echo 'connect mysql success';

}

// 创建数据库

if(mysql_query("CREATE DATABASE IF NOT EXISTS `test` CHARACTER SET utf8",$conn)) {

echo "Database created";

}

else {

echo 'Create database failed'.mysql_error();

die();

}

mysql_close($conn);

访问http://localhost/mysql.php

上一篇下一篇

猜你喜欢

热点阅读