Ubuntu 14.04 LNMP 环境搭建

  • 准备:更新apt安装源
sudo apt-get update

1、安装Nginx

sudo apt-get install nginx

安装成功测试:

curl localhost

看到Nginx的欢迎界面就是成功!

2、安装MySQL

sudo apt-get install mysql-server

安装过程中会要求输入root用户的密码,

然后:

// 让MySQL创建数据存储目录
sudo mysql_install_db

// 修改一些不安全的数据库默认配置,根据情况选择就好
sudo mysql_secure_installation

3、安装PHP

// php-mysql是PHP用来连接数据库的
sudo apt-get install php-fpm php-mysql

修改 php-fpm 配置:

sudo vim /etc/php5/fpm/php.ini
// 将
cgi.fix_pathinfo=1
// 修改为
cgi.fix_pathinfo=0
// 否则PHP会在找不到请求文件给的情况下找最近的文件执行,这太不安全

重启PHP 进程

sudo service php5-fpm restart

4、配置Nginx使其使用PHP进程处理对PHP类型文件的请求

sudo vim /etc/nginx/sites-available/default

默认:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
    }
}

重新配置为:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name server_domain_name_or_IP;

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ .php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

重启Nginx(一定要严格遵守格式):

sudo service nginx restart

5、测试:

Nginx访问目录创建info.php:

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

保存一下内容:

<?php
// 这里的用户名密码都是自己设定的
$con = mysql_connect('username', 'password', 'localhost');
if ($con) {
  echo "数据库连接成功!";
} else {
  echo "数据库连接失败!";
}
echo "PHP信息:";
phpinfo();
?>

用浏览器访问即可看到页面:

http://server_domain_name_or_IP/info.php

看完就删掉,被其他人看到会暴露服务器信息:

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

原文地址

原文地址:https://www.cnblogs.com/kiscall/p/5561428.html