ngnix是一款基于BSD License的开源的,免费的,高性能的,高并发的,http服务器,反代理服务器,IMAP/POP3服务器。在优化配置的情况下,其性能远远由于apache http服务器。
本篇介绍如何在ubuntu下面安装和架设nginx服务器,分下面3个阶段进行,
安装
配置
php测试
1. 安装
默认,nginx是不在ubutnu的仓库中的,因此如果你想直接使用 apt-get install nginx,你会被告知找不到nginx的,因此你先需要将对应的仓库添加到源中。
a. 添加源
将下面的内容加到/etc/apt/sources.list的文件的末尾,记住需要超级管理员权限
deb http://nginx.org/packages/ubuntu/ codename nginx deb-src http://nginx.org/packages/ubuntu/ codename nginx
b. 更性仓库
然后在运行
sudo apt-get update
c. 安装
接下来就可以安装了
sudo apt-get install nginx
还有另外一种安装方式就是源码安装。下载nginx的源码,然后编译安装。
./configure make sudo make install
d. 检测安装
使用
whereis nginx
如果你看到
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx /usr/share/man/man1/nginx.1.gz
说明安装没有问题
e. 运行
nginx安装为系统的服务,因此可以使用service命令来,查看它的状态
service nginx status
运行可以使用
sudo service nginx start
一定需要超级管理员的权限运行,否则会遇到权限错误,
Starting nginx: nginx: [alert] could not open error log file: open() “/var/log/nginx/error.log” failed (13: Permission denied)
2013/07/14 11:08:55 [warn] 3773#0: the “user” directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2013/07/14 11:08:55 [emerg] 3773#0: open() “/var/run/nginx.pid” failed (13: Permission denied)
nginx: configuration file /etc/nginx/nginx.conf test failed
f. 网页测试
使用 http://localhost,如果成功你可以看到
Welcome to nginx!
2. 配置
nginx的配置文件的位置
/etc/nginx/nginx.conf
在这个配置文件中,我们主要关注:
这是默认的config的内容
user www-data;
2 worker_processes 4;
3 pid /var/run/nginx.pid;
4
5 events {
6 worker_connections 768;
7 # multi_accept on;
8 }
9
10 http {
11
12 ##
13 # Basic Settings
14 ##
15
16 sendfile on;
17 tcp_nopush on;
18 tcp_nodelay on;
19 keepalive_timeout 65;
20 types_hash_max_size 2048;
21 # server_tokens off;
22
23 # server_names_hash_bucket_size 64;
24 # server_name_in_redirect off;
25
26 include /etc/nginx/mime.types;
27 default_type application/octet-stream;
28
29 ##
30 # Logging Settings
31 ##
32
33 access_log /var/log/nginx/access.log;
34 error_log /var/log/nginx/error.log;
………………….
68 # Virtual Host Configs
69 ##
70
71 include /etc/nginx/conf.d/*.conf;
72 include /etc/nginx/sites-enabled/*;
…………………
有一部分作为web服务器并关心的配置就用省略号代替了。
比如支持的mime的类型
26 include /etc/nginx/mime.types;
log文件的配置
33 access_log /var/log/nginx/access.log;
34 error_log /var/log/nginx/error.log;
最重要的可能就是,虚拟机的配置,也就是apache中virtual host的配置
68 # Virtual Host Configs
69 ##
70
71 include /etc/nginx/conf.d/*.conf;
72 include /etc/nginx/sites-enabled/*;
72行中指定了配置虚拟机的位置,我们看看这下面都有什么,默认只有一个default,这种管理方式和apache非常相似,通过sites-avaiable, sites-enabled来管理站点,第一个文件夹中放置的是可用的站点,而第二个中则是激活的站点。服务器真正加在的是sites-enabled的站点,因此需要让想被加在的站点到sites-enabled的文件夹中,做到这点,使用一个apache管理的站点极其相似的方法,apache中a2ensite命令,nginx中nginx-ensite,其实这些都是脚本,实际做的事情就是在sites-enabled创建在sites-avaiable中的符号链接,当然这个脚本里头的东西远比下面这条命令要多,有兴趣可以自己打开脚本查看,
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
从下面的网址下载nginx-ensite
https://github.com/perusio/nginx_ensite
或者直接下载我缩使用的这个版本:
https://github.com/perusio/nginx_ensite/archive/master.zip
下载之后拷贝到/usr/sbin
sudo cp nginx_ensite /usr/sbin
sudo cp ./man/
nginx_ensite.8 /usr/sbin/
接下来我们创建一个虚拟机,然后使用这个工具来激活我们虚拟机,然后测试它。首先在sites_available下面创建一个test这个空的文件,里面输入如下内容:
server {
listen 8088; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/test;
index 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 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;
}
}
上面就是让nginx监听8088端口,虚拟机的根目录是 /usr/share/nginx/test,默认的文件类型是index.html和index.htm
在/usr/share/nginx/下面创建一个test的文件夹,进入这个文件夹,创建一个index.html,里面随便输入一些html的语句,比如输出 hello world就可以了。
然后激活站点
sudo nginx_ensite test
此时去sites-enabled目录,就会发现多了一个test,这个就是运行上面的命令后创建的,接下来重启nginx,让其加在我们的站点,
sudo service nginx restart
在浏览器中使用下面的url访问的刚才创建的hello world的页面,看你是不是可以页面输出hello world
http://localhost:8088
是不是好简单。
3. php测试
上面我们测试只是静态html,接下来我们使用php来创建一个简单的站点。在我们使用php之前,还有安装配置php,所以先来安装和配置php。nginx所用的php-fpm, fpm是fast-cgi process manager的缩写,使用下面的命令安装
sudo apt-get install php5-cli php5-common php5-suhosin
sudo apt-get install php5-fpm php5-cgi
安装完成后,使用ps -ef查看进程中是否有php-fpm这个进程在运行,如果是,表明我们安装成功了。接下来,就是修改我们刚才创建那个virtual host了,将其修改为如下的内容:
server {
listen 8088; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/test;
index index.php;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.php;
# 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;
}
## Parse all .php file in the /var/www directory
location ~ .php$ {
# for security reasons the next line is highly encouraged
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+.php)(.*)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
include fastcgi_params;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
}
重新启动nginx,让其重新加在我们的站点,然后去站点的根目录,在我们的test中,它就是/usr/share/nginx/test,创建一个index.php,内容如下:
<?php
phpinfo();
?>
使用如下的url看是否能看到php的配置信息,
http://localhost:8088
至此,我们的nginx的安装,配置,php测试全部完成。
配置参考文档:
http://wiki.nginx.org/Configuration
版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.
Very well done & written my friend.
I’ve only just begun writing recently annd realised that many
writers simply rework old ideas but add very little of worth.
It’s fantastic to read an educational write-up of
some real value to me.
It is on my ist of details I need to replicate as a new blogger.
Audience engagement and ckntent quality are king.
Some wonderful thoughts; youu have unquestionably made it onn my list of writers to follow!
Carry on the excellpent work!
Well done,
Mickey