日志

Ubuntu14.04 搭建 LNMP 环境

  • 环境

系统:Ubuntu 14.04.1 LTS

下面所有的安装都通过apt-get的方式,为了防止安装过程报错建议先执行一次更新

sudo apt-get update
  • MySQL安装

sudo apt-get install mysql-server mysql-client

安装期间会出现类似如下的Y/N选择,直接Y 回车即可(下同)

root@li568-33:/# sudo apt-get install mysql-server mysql-client
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libaio1 libdbd-mysql-perl libdbi-perl libmysqlclient18 libterm-readkey-perl
mysql-client-5.5 mysql-client-core-5.5 mysql-common mysql-server-5.5
mysql-server-core-5.5
Suggested packages:
libclone-perl libmldbm-perl libnet-daemon-perl libplrpc-perl
libsql-statement-perl tinyca mailx
The following NEW packages will be installed:
libaio1 libdbd-mysql-perl libdbi-perl libmysqlclient18 libterm-readkey-perl
mysql-client mysql-client-5.5 mysql-client-core-5.5 mysql-common
mysql-server mysql-server-5.5 mysql-server-core-5.5
0 upgraded, 12 newly installed, 0 to remove and 123 not upgraded.
Need to get 0 B/9,260 kB of archives.
After this operation, 96.5 MB of additional disk space will be used.
Do you want to continue? [Y/n]y

中途会出现新的界面设置MySQL的root用户密码

首先设置root密码:yourpassword
然后重复输入root密码:yourpassword

等安装结束后输入以下命令查看是否成功

mysql --version

正常会显示类似以下的版本信息,则安装完成

mysql  Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (x86_64) using readline 6.3

mysql的配置文件在:/etc/mysql/my.cnf
例如:若要允许远程访问请注释my.cnf 的 bind_address 127.0.0.1

  • Nginx安装

执行以下命令安装

sudo apt-get install nginx

安装非常简单,安装结束后可以通过以下命令查看nginx的版本信息

nginx -v
nginx version: nginx/1.4.6 (Ubuntu)

配置目录在 /etc/nginx/

要测试是否安装正常可以访问你的远程主机ip ,若在本地部署可直接访问 localhost Snip20150523_12

显示 Nginx的欢迎页则表示已经安装好了

  • PHP-FPM安装

Nginx需要通过FastCGI运行PHP,PHP-FPM是一个PHP的FastCGI进程管理器,相比FastCGI静态的唤起CGI,PHP-FPM能根据访问的压力动态的唤起CGI进程和销毁以达到动态的调整CGI数量,这样可以有效的使用内存.另外它还有其他的优点:

  1. FPM可以平滑的重载php配置(新的worker用新的配置,已经存在的worker采用原配置处理,通过这种机制来平滑过度)
  2. 由于FPM是使用Unix-Socket来和服务器通讯,所以不用再配置cgi端口;(nginx 和 php-fpm 通信使用unix socket还是TCP)
  3. FPM有更好的状态输出和slowlog日志,502的时候能给出更多的错误细节.

安装PHP-FPM

sudo apt-get install php5-fpm

Nginx默认是不会设置PATH_INFO环境变量的的值,需要php.ini使用cgi.fix_pathinfo=1的配置来完成路径信息的获取,但同时会带来安全隐患,所以建议把php.ini的cgi.fix_pathinfo=0设置为0

vim /etc/php5/fpm/php.ini

这样php就获取不到PATH_INFO信息,我们可以通过rewrite方式代替php中的PATH_INFO,例如:THINKPHP框架的phpinfo解决方案(URL_MODE = 2)

location / {
    if (!-e $request_filename){
        rewrite ^/(.*)$ /index.php?s=/$1 last;
    }
}

也可以在nginx配置文件使用fastcgi_split_path_info指令来设置PATH_INFO值

location ~ ^.+.php {
    (...)
    fastcgi_split_path_info ^((?U).+.php)(/?.+)$;
    fastcgi_param SCRIPT_FILENAME /path/to/php$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    (...)
}

FPM和PHP的配置目录位于 /etc/php5/fpm/

启动 phpfpm

php5-fpm -c /etc/php5/fpm/php.ini -y /etc/php5/fpm/php-fpm.conf

关闭 phpfpm

service php5-fpm stop

如果返回 unknown instance 请使用下面的命令终止

sudo pkill php5-fpm

另外,以后安装完PHP扩展后需要重启FPM

service php5-fpm restart

  • 配置Nginx运行PHP

创建根目录

sudo mkdir /var/www/

创建一个local的文件目录

sudo mkdir /var/www/local

在local下新建一个index.php的文件作为访问页

sudo vim /var/www/local/index.php

php代码如下

<?php
    phpinfo();
?>

修改相关的用户权限

sudo chown -R www-data:www-data /var/www/
sudo chmod -R 775 /var/www/

创建localhost的Nginx配置,Nginx安装完毕后在sites-avaiable目录下会有一份默认default的配置作为参考,我们将基于default修改一份访问localhost下的index.php的专属配置

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

local配置如下(主要设置了root路径以及开启了fastcgi)

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

<pre><code>root /var/www/local;
index index.php index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost;

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
}

# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
#   proxy_pass http://127.0.0.1:8080;    
#}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
#   root /usr/share/nginx/html;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    # With php5-cgi alone:
    # fastcgi_pass 127.0.0.1:9000;
    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#   deny all;
#}
</code></pre>

}

移除默认的default软链接(防止和local配置中的server_name冲突)

sudo rm /etc/nginx/sites-enabled/default

在sites-enabled创建local的软链接,这样才能使刚才的配置生效

sudo ln -s /etc/nginx/sites-available/local /etc/nginx/sites-enabled/local

访问你的localhost或你的远程主机IP,可以看到phpinfo的页面则大功告成
Snip20150523_13

转载请注明出处:

© http://hejunhao.me