Debian/Ubuntu搭建LNMP(Linux,nginx,MySQL,PHP)环境,搭建wordpress

本文介绍了Debian/Ubuntu搭建LNMP(Linux,nginx,MySQL,PHP)环境的基本方法,并安装wordpress,附上视频教程。

环境准备

1.有root账号的Ubuntu或者Debian环境。

2.拥有独立IP的服务器一台。

3.有一定的Linux操作知识。

安装Nginx服务器

sudo apt-get update
sudo apt-get install -y nginx 

如果没有sudo命令,请apt-get install sudo

获取自己服务器的IP:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

当安装成功后,直接在浏览器中输入服务器IP或已经解析好的域名:

http://server_domain_name_or_IP/

如果出现:

Welcome to Nginx! 

的默认欢迎页面说明Nginx服务器已经安装好了。

安装MySQL数据库

根之前的文章一样:

sudo apt-get install mysql-server

然后

sudo mysql_install_db

sudo mysql_secure_installation

记得设置mysql的root用户的管理密码。

安装PHP环境

sudo apt-get install php5-fpm php5-mysql

使用默认vim编辑器:Debian下更换默认编辑器为vim

sudo vi /etc/php5/fpm/php.ini

修改下面的内容:

cgi.fix_pathinfo=0

因为:

What we are looking for in this file is the parameter that sets cgi.fix_pathinfo. This will be commented out with a semi-colon (;) and set to “1” by default.

This is an extremely insecure setting because it tells PHP to attempt to execute the closest file it can find if a PHP file does not match exactly. This basically would allow users to craft PHP requests in a way that would allow them to execute scripts that they shouldn’t be allowed to execute.

这是一个非常不安全的设置,告诉PHP试图去执行相近的文件而不是绝对精确的文件。如果是默认设置的话,会允许用户篡改PHP的请求,并且会去执行一些可能不被允许执行的脚本。

修改完后:

sudo service php5-fpm restart

重启PHP。

配置Nginx使用PHP进程

sudo vi /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;
    }
}

上面的文件很好理解:

监听80端口 默认服务器
nginx的根目录为 /usr/share/nginx/html
index 会自动执行index.html
服务器的地址 指向了 localhost 也就是本机

配置成如下配置:

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

root /var/www;
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;
}
}

然后重启:

sudo service nginx restart

如果重启失败可以使用:

nginx -t 

来看看系统提示了什么,再来修改。

最后测试一下

cd /var
mkdir www
cd /var/www

相当于新建了一个/var/www目录

vi info.php

按i后输入:

<? phpinfo(); ?>

然后,再来访问:

http://server_domain_name_or_IP/

就可以看到php相关信息了,就是这么简单。

搭建wordpress

参考:
WordPress安装视频教程

wordpress伪静态文件.htaccess文件配置

VPS推荐与选择&putty基本使用方法

监控mysql运行状态,停止则重启

配置iptables

用SSH来管理Linux服务器,禁用口令登陆,提高Linux服务器安全

Debian7安装wordpress权限问题

MySql中添加用户,新建数据库,用户授权,删除用户,修改密码

整个视频教程