Ubuntu下LNMP环境搭建
2016-07-02 本文已影响620人
王宝花
安装Nginx
如果已经安装了apache,则需要手动删除。
service apache2 stop
update-rc.d -f apache2 remove
apt-get remove apache2
然后再进行安装Nginx
apt-get install nginx
输入服务器,查看是否Ngnix安装成功。如果成功辉县Ngnix欢迎界面,否则无。
配置Nginx
- 打开配置文件
vim /etc/nginx/sites-available/default
修改简要:
在index 中加入index.php;
开启加载php文件的模块;等
具体修改如下:
[...]
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
# 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$ {
try_files $uri =404;
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;
}
}
[...]
安装mysql
apt-get install mysql-server mysql-client
在安装时,会有弹出窗需要为mysql的root用户设置密码,设置密码即可。
安装php5-fpm
apt-get install php5-fpm
配置php5-fpm
vi /etc/php5/fpm/php.ini # 打开配置文件
# 将cgi.fix_pathinfo=1修改为0
我为什么要修改这个文件?
具体修改如下:
[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]
重新加载php5-fpm
service php5-fpm reload
ngnix默认目录
cd /usr/share/nginx/html
vim phpinfo.php
写入如下代码:
<?php
phpinfo();
?>
检查phpinfo文件,并安装扩展模块
apt-cache search php5 # 搜索php5支持模块命令
安装扩展,可自行选择:
apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
APC是一个自由和开放的PHP操作码缓存器缓存和优化PHP的中间代码。这是类似于其他PHP操作码cachers,如eAccelerator、XCache。这是强烈建议有一个安装加速你的PHP页面。
apt-get install php-apc
重新加载nginx配置和php5-fpm配置
nginx -t # 测试Nginx配置文件是否正确配置,如果错误会显示错误信息;正确则显示ok
service nginx reload
service php5-fpm reload
如果php5-fpm显示进程已存在,无法重载。
则关闭php5-fpm进程,killall php5-fpm
,然后重启/etc/init.d/php5-fpm restart
在URL地址栏输入服务器地址,即可查看已支持的php扩展模块。
114.112.154.151/phpinfo.php # 我的服务器地址