Ubuntu下安装Naginx, PHP5(及PHP-FPM),MySQL
环境:ubuntu 12.0.4 LTS
nginx(发音"engine x”)是一个自由,开放源码,高性能的HTTP server。Nginx以稳定性,丰富的功能集,简单的配置,和低资源消耗而出名。本文将向你展示怎么在ubuntu 12.0.4 LTS 上安装Nginx,php5(及php-fpm),mysql。
一:安装前做个简单的说明
我使用的域名为example.com,ip地址是218.198.177.252。你可以视具体情况更改这些设置。在下文中我将使用root权限安装所需软件,所以请先切换到root用户:sudo su
二:安装MySQL
apt-get install mysql-server mysql-client
安装过程会提示你为MySQL root 用户提供一个密码—-这个密码对 root@localhost可用,同时对root@example.com也可用,因此你需要手动为MySQL root用户指定一个密码:
New password for the MySQL “root” user: <-- yourrootsqlpassword
Repeat password for the MySQL “root” user: <– yourrootsqlpassword
三:安装Nginx
apt-get install nginx
1,启动nginx
/etc/init.d/nginx start
2,打开浏览器输入http://127.0.0.1,如果看到Welcome to nginx!,则说明安装成功,ubuntu 12.0.4 LTS上nginx默认的网站根目录在 /usr/share/nginx/www。
四:安装PHP5
PHP5可以在nginx上通过PHP-FPM(PHP—FPM(FastCGI Process Manager) 是一个可选的 FastCGI,添加了一些了一些很有用的特性,特别是对于繁忙的站点)工作。
说明:Nginx不支持对外部程序的直接调用或解析,所有的外部程序(包括PHP)必须通过FastCGI接口调用。
apt-get install php5-fpm
PHP-FPM是一个守护进程(init脚本文件在/etc/init.d/php5-fpm),它运行了一个FastCGI server,端口是 9000。
五:配置 nginx,以下是我本机的配置文件。
1,nginx的配置文件在/etc/nginx/nginx.conf, vim /etc/nginx/nginx.conf 如下:
user www-data; //指定Nginx Worker 进程运行用户及用户组
<!-- lang: php -->
worker_processes 4; / /指定Nginx开启的进程数,每个Nginx进程平均耗费10M-20M内存。
<!-- lang: php -->
pid /var/run/nginx.pid; //用来指定进程id的存储文件的位置
<!-- lang: php -->
<!-- lang: php -->
events { //用来指定Nginx的工作模式,及连接上限数
<!-- lang: php -->
use epoll;
<!-- lang: php -->
worker_connections 768;
<!-- lang: php -->
# multi_accept on;
<!-- lang: php -->
}
<!-- lang: php -->
<!-- lang: php -->
http {
<!-- lang: php -->
<!-- lang: php -->
##
<!-- lang: php -->
# Basic Settings //基本的设置
<!-- lang: php -->
##
<!-- lang: php -->
<!-- lang: php -->
sendfile on;
<!-- lang: php -->
tcp_nopush on;
<!-- lang: php -->
tcp_nodelay on;
<!-- lang: php -->
keepalive_timeout 65;
<!-- lang: php -->
types_hash_max_size 2048;
<!-- lang: php -->
# server_tokens off;
<!-- lang: php -->
<!-- lang: php -->
# server_names_hash_bucket_size 64;
<!-- lang: php -->
# server_name_in_redirect off;
<!-- lang: php -->
<!-- lang: php -->
include /etc/nginx/mime.types;
<!-- lang: php -->
default_type application/octet-stream;
<!-- lang: php -->
<!-- lang: php -->
##
<!-- lang: php -->
# Logging Settings //指定日志的存放路径
<!-- lang: php -->
##
<!-- lang: php -->
<!-- lang: php -->
access_log /var/log/nginx/access.log;
<!-- lang: php -->
error_log /var/log/nginx/error.log;
<!-- lang: php -->
<!-- lang: php -->
##
<!-- lang: php -->
# Gzip Settings //开启Gzip 压缩
<!-- lang: php -->
##
<!-- lang: php -->
<!-- lang: php -->
gzip on;
<!-- lang: php -->
gzip_disable "msie6";
<!-- lang: php -->
<!-- lang: php -->
gzip_vary on;
<!-- lang: php -->
gzip_proxied any;
<!-- lang: php -->
gzip_comp_level 6;
<!-- lang: php -->
gzip_buffers 16 8k;
<!-- lang: php -->
gzip_http_version 1.1;
<!-- lang: php -->
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
<!-- lang: php -->
<!-- lang: php -->
##
<!-- lang: php -->
# nginx-naxsi config
<!-- lang: php -->
##
<!-- lang: php -->
# Uncomment it if you installed nginx-naxsi
<!-- lang: php -->
##
<!-- lang: php -->
<!-- lang: php -->
#include /etc/nginx/naxsi_core.rules;
<!-- lang: php -->
<!-- lang: php -->
##
<!-- lang: php -->
# nginx-passenger config
<!-- lang: php -->
##
<!-- lang: php -->
# Uncomment it if you installed nginx-passenger
<!-- lang: php -->
##
<!-- lang: php -->
<!-- lang: php -->
#passenger_root /usr;
<!-- lang: php -->
#passenger_ruby /usr/bin/ruby;
<!-- lang: php -->
<!-- lang: php -->
##
<!-- lang: php -->
# Virtual Host Configs //虚拟主机的配置
<!-- lang: php -->
##
<!-- lang: php -->
<!-- lang: php -->
include /etc/nginx/conf.d/*.conf;
<!-- lang: php -->
include /etc/nginx/sites-enabled/*;
<!-- lang: php -->
<!-- lang: php -->
<!-- lang: php -->
}
<!-- lang: php -->
#mail {
<!-- lang: php -->
# # See sample authentication script at:
<!-- lang: php -->
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
<!-- lang: php -->
#
<!-- lang: php -->
# # auth_http localhost/auth.php;
<!-- lang: php -->
# # pop3_capabilities "TOP" "USER";
<!-- lang: php -->
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
<!-- lang: php -->
#
<!-- lang: php -->
# server {
<!-- lang: php -->
# listen localhost:110;
<!-- lang: php -->
# protocol pop3;
<!-- lang: php -->
# proxy on;
<!-- lang: php -->
# }
<!-- lang: php -->
#
<!-- lang: php -->
# server {
<!-- lang: php -->
# listen localhost:143;
<!-- lang: php -->
# protocol imap;
<!-- lang: php -->
# proxy on;
<!-- lang: php -->
# }
<!-- lang: php -->
#}
<!-- lang: php -->
2,虚拟主机被定义在server{}中,默认文件在/etc/nginx/sites-available/default,vim /etc/nginx/sites-available/default。
server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/www;
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 index.html
try_files $uri $uri/ /index.html;
}
location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}
location /images {
root /usr/share;
autoindex off;
}
#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/www;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
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;
}
}
3,保存文件,使配置生效 /etc/init.d/nginx reload
4,在Nginx的默认网站根目录创建一个PHP的测试文件 vim /usr/share/nginx/www/info.php
phpinfo();
?>
5,打开浏览器输入http://127.0.0.1/info.php
你可以看见PHP5已经通过FPM/FastCGI工作了,具体可看Server API那行。向下滚动可以看见所有的模块在PHP5中都是可用的,MySQL还没有被列出来,意味着MySQL还没支持PHP5。
六:让MySQL支持PHP5
1,让MySQL支持PHP5,我们可以安装php5-mysql包。其余的包,我们可以按需安装所需要的包,用apt-cache search php5列出PHP的包,看下那个是你所需要的。
2,选择一些你所需要的包,象这样安装:
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
3,重启PHP-FPM
/etc/init.d/php5-fpm restart
4,打开浏览器,输入http://127.0.0.1/info.php,看下你安装的包是不是已经被支持了。
七:配置PHP-FPM
vim /etc/php5/fpm/php-fpm.conf 或在 vim /etc/php5/fpm/conf.d/下做更详细的配置,不懂真人的默认就行了 ,也不优化了。
八:在/etc/nginx/sites-available/default中新增一个虚拟主机。
我的配置文件:
server {
listen 80 ; ## listen for ipv4; this line is default and implied
listen [::]:80 default ipv6only=on; ## listen for ipv6
root /web/example;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name 218.198.177.252 example.com ; //这个和apache一样的啦,写域名就行了
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
# Only for nginx-naxsi : process denied requests
#location /RequestDenied {
# For example, return an error code
#return 418;
#}
#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 /web/example;
}
location ~ .php$ { //nginx处理静态的页面,动态的转给FastCGI处理
# 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;
}
}
看下效果了,如过你的不成功,自己检查下….
参考资料:
1,http://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-on-ubuntu-11.10
2,http://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-on-ubuntu-11.10-p2
3,张宴的《实战Nginx:取代Apache的高性能Web服务器》
Ubuntu下安装Naginx, PHP5(及PHP-FPM),MySQL的更多相关文章
- Ubuntu下安装Nginx,PHP5(及PHP-FPM),MySQL
.简介: Tomcat在高并发环境下处理动态请求时性能很低,而在处理静态页面更加脆弱.虽然Tomcat的最新版本支持epoll,但是通过Nginx来处理静态页面要比通过Tomcat处理在性能方面好很多 ...
- Ubuntu下安装Apache2, php5 mysql
不错的博文:http://blog.csdn.net/guaikai/article/details/6905781 1:首先安装apache:打开终端(ctrl+Alt+t), 输入命令:sudo ...
- zhuan:ubuntu下安装Apache2+php+Mysql
from: http://www.cnblogs.com/lynch_world/archive/2012/01/06/2314717.html ubuntu下安装Apache+PHP+Mysql 转 ...
- ubuntu下安装pdo扩展
ubuntu下安装好LAMP后默认情况没有安装mysql_pdo扩展,以下是安装 步聚,在终端输入以下命令 1.pecl search pdo 2.sudo pecl install pdo 当出现E ...
- ubuntu下安装pdo和pdo_mysql扩展
ubuntu下安装好LAMP后默认情况没有安装mysql_pdo扩展,以下是安装步聚 1 安装pdo sudo pecl install pdo 出现以下错误是说明pdo已经加入了php的默认安装,不 ...
- 在Ubuntu下安装ovs-dpdk
在Ubuntu下安装ovs-dpdk 参考资料:https://software.intel.com/zh-cn/articles/using-open-vswitch-with-dpdk-on-ub ...
- Ubuntu 下安装QT
Ubuntu 下安装QT 本文使用的环境 QT Library: qt-everywhere-opensource-src-4.7.4.tar.gz QT Creator: qt-creator-li ...
- Ubuntu下安装JDK以及相关配置
1.查看系统位数,输入以下命令即可 getconf LONG_BIT 2.下载对应的JDK文件,我这里下载的是jdk-8u60-linux-64.tar.gz 3.创建目录作为JDK的安装目录,这里选 ...
- Ubuntu下安装mod_python报错(GIT错误)
Ubuntu下安装mod_python3.4.1版本报出如下错误: writing byte-compilation script '/tmp/tmpE91VXZ.py' /usr/bin/pytho ...
随机推荐
- [工具]Serv-U配置教程
引言 最近项目中需要用到ftp服务器,将文件放在ftp服务器上,就在网上查了一下,学习了一下Serv-u的配置及使用,这里也将如何配置的做一记录. Serv-u简介 Serv-U 是目前众多的FTP ...
- Centos6.5 python升级成2.7版本出现的一些问题解决方法
由于功能及程序依赖,需要将Centos上的python从2.6升级成2.7,把碰到的一些问题记录如下: 安装好2.7后将原来的/usr/bin/python改成/usr/bin/python26,并将 ...
- 完美且精准的 IE10- 版本检测。
通过动态的写入条件注释的方式,来判断当前的ie浏览器的版本,优点是,对比userAgent字符匹配,要精准很多,应该说是100%正确,缺点就是要通过JS写入条件注释,拖慢页面的加载速度. 如果对于只判 ...
- 当 IDENTITY_INSERT 设置为 OFF 时,不能向表 '#TT' 中的标识列插入显式值。 sql server 临时表
当 IDENTITY_INSERT 设置为 OFF 时,不能向表 '#TT' 中的标识列插入显式值.我是在SqlServer写存储过程中遇到的这个错误,当时就心想:临时表怎么会有主键呢,我也没有设置主 ...
- ASP.NET MVC程序播放H.264视频
在这篇之前,Insus.NET不管是在ASP.NET还是ASP.NET MVC实现很多视频播放,你可以参考这篇链接:http://www.cnblogs.com/insus/category/4650 ...
- C#编程总结(二)多线程基础
C#编程总结(二)多线程基础 无论您是为具有单个处理器的计算机还是为具有多个处理器的计算机进行开发,您都希望应用程序为用户提供最好的响应性能,即使应用程序当前正在完成其他工作.要使应用程序能够快速响应 ...
- 电脑Windows使用中遇到的那些问题
Win64位注册表导入方法 64位Windows操作系统注册表不同于32位Windows操作系统,Win64 位版本分为 32 位注册表项和 64 位注册表项.默认 64 位版本注册表编辑器 (Reg ...
- iOS高效开发之Xcode应用插件
前言:本文非原创 文章摘自 www.cocoachina.com/industry/20130918/7022.html 古人云“工欲善其事必先利其器”,打造一个强大的开发环境,是立即提升自身战 ...
- hangfire+bootstrap ace 模板实现后台任务管理平台
前言 前端时间刚开始接触Hangfire就翻译了一篇官方的教程[翻译+山寨]Hangfire Highlighter Tutorial,后来在工作中需要实现一个异步和定时执行的任务管理平台,就结合bo ...
- C# 站点IP访问频率限制 针对单个站点
0x00 前言 写网站的时候,或多或少会遇到,登录,注册等操作,有时候,为了防止别人批量进行操作,不得不做出一些限制IP的操作(当前也可以用于限制某个账号的密码校验等). 这样的简单限制,我们又不想对 ...