ubunut 安装nginx基本目录结构介绍和配置使用方法

来源:互联网 发布:证券软件免费下载 编辑:程序博客网 时间:2024/04/28 12:13

安装,注意是nginx不是ngnix。否则会安装失败。

[plain] view plaincopy
  1. root@coder-671T-M:/soft_my# apt-get install nginx  
  2. 正在读取软件包列表... 完成  
  3. 正在分析软件包的依赖关系树         
  4. 正在读取状态信息... 完成         
  5. 将会安装下列额外的软件包:  
  6.   nginx-common nginx-full  
  7. 下列【新】软件包将被安装:  
  8.   nginx nginx-common nginx-full  
  9. 升级了 0 个软件包,新安装了 3 个软件包,要卸载 0 个软件包,有 184 个软件包未被升级。  
  10. 需要下载 379 kB 的软件包。  
  11. 解压缩后会消耗掉 1,180 kB 的额外空间。  
  12. 您希望继续执行吗?[Y/n]Y  
  13. 获取:1 http://cn.archive.ubuntu.com/ubuntu/ oneiric/universe nginx-common all 1.0.5-1 [14.6 kB]  
  14. 获取:2 http://cn.archive.ubuntu.com/ubuntu/ oneiric/universe nginx-full i386 1.0.5-1 [359 kB]  
  15. 获取:3 http://cn.archive.ubuntu.com/ubuntu/ oneiric/universe nginx all 1.0.5-1 [5,616 B]  
  16. 下载 379 kB,耗时 6秒 (61.9 kB/s)                                                
  17. 选中了曾被取消选择的软件包 nginx-common。  
  18. (正在读取数据库 ... 系统当前共安装有 244027 个文件和目录。)  
  19. 正在解压缩 nginx-common (从 .../nginx-common_1.0.5-1_all.deb) ...  
  20. 选中了曾被取消选择的软件包 nginx-full。  
  21. 正在解压缩 nginx-full (从 .../nginx-full_1.0.5-1_i386.deb) ...  
  22. 选中了曾被取消选择的软件包 nginx。  
  23. 正在解压缩 nginx (从 .../archives/nginx_1.0.5-1_all.deb) ...  
  24. 正在处理用于 ufw 的触发器...  
  25. 正在处理用于 ureadahead 的触发器...  
  26. ureadahead will be reprofiled on next reboot  
  27. 正在处理用于 man-db 的触发器...  
  28. 正在设置 nginx-common (1.0.5-1) ...  
  29. 正在设置 nginx-full (1.0.5-1) ...  
  30. 正在设置 nginx (1.0.5-1) ...  

nginx的目录存放位置:

配置文件目录 /etc/nginx

网站资源文件存放位置/usr/share/nginx

[plain] view plaincopy
  1. root@coder-671T-M:~# cd /etc/nginx/  
  2. root@coder-671T-M:/etc/nginx# ll  
  3. 总用量 64  
  4. drwxr-xr-x   5 root root  4096 2011-11-29 11:41 ./  
  5. drwxr-xr-x 172 root root 12288 2011-11-29 11:41 ../  
  6. drwxr-xr-x   2 root root  4096 2011-07-27 23:37 conf.d/  
  7. -rw-r--r--   1 root root   867 2011-03-23 12:20 fastcgi_params  
  8. -rw-r--r--   1 root root  2258 2010-12-02 14:19 koi-utf  
  9. -rw-r--r--   1 root root  1805 2010-12-02 14:19 koi-win  
  10. -rw-r--r--   1 root root  2059 2011-02-22 15:49 mime.types  
  11. -rw-r--r--   1 root root  1340 2010-12-02 14:19 nginx.conf  
  12. -rw-r--r--   1 root root   131 2011-05-02 13:25 proxy_params  
  13. -rw-r--r--   1 root root   465 2010-12-02 14:19 scgi_params  
  14. drwxr-xr-x   2 root root  4096 2011-11-29 11:41 sites-available/  
  15. drwxr-xr-x   2 root root  4096 2011-11-29 11:41 sites-enabled/  
  16. -rw-r--r--   1 root root   497 2010-12-02 14:19 uwsgi_params  
  17. -rw-r--r--   1 root root  3071 2010-12-02 14:19 win-utf  
  18. root@coder-671T-M:/etc/nginx# tree  
  19. .  
  20. ├── conf.d  
  21. ├── fastcgi_params  
  22. ├── koi-utf  
  23. ├── koi-win  
  24. ├── mime.types  
  25. ├── nginx.conf  
  26. ├── proxy_params  
  27. ├── scgi_params  
  28. ├── sites-available  
  29. │   └── default  
  30. ├── sites-enabled  
  31. │   └── default -> /etc/nginx/sites-available/default  
  32. ├── uwsgi_params  
  33. └── win-utf  
  34.   
  35. 3 directories, 11 files  


[plain] view plaincopy
  1. root@coder-671T-M:/usr/share/nginx# tree  
  2. .  
  3. └── www  
  4.     ├── 50x.html  
  5.     └── index.html  
  6.   
  7. 1 directory, 2 files  
  8. root@coder-671T-M:/usr/share/nginx#   

nginx修改端口

默认的是80,修改成7888

ubuntu的配置文件分成三部分:

/etc/nginx根目录存放公共的配置

在sites_available和sites_enabled配置站点相关的信息例如域名端口等等。所以修改默认的sites_available/default文件如下

[plain] view plaincopy
  1. # You may add here your  
  2. # server {  
  3. #   ...  
  4. # }  
  5. # statements for each of your virtual hosts to this file  
  6.   
  7. ##  
  8. # You should look at the following URL's in order to grasp a solid understanding  
  9. # of Nginx configuration files in order to fully unleash the power of Nginx.  
  10. # http://wiki.nginx.org/Pitfalls  
  11. # http://wiki.nginx.org/QuickStart  
  12. # http://wiki.nginx.org/Configuration  
  13. #  
  14. # Generally, you will want to move this file somewhere, and start with a clean  
  15. # file but keep this around for reference. Or just disable in sites-enabled.  
  16. #  
  17. # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.  
  18. ##  
  19.   
  20. server {  
  21.     <strong>listen   7888; </strong>## listen for ipv4; this line is default and implied  
  22.     #listen   [::]:80 default ipv6only=on; ## listen for ipv6  
  23.   
  24.     root /usr/share/nginx/www;  
  25.     index index.html index.htm;  
  26.   
  27.     # Make site accessible from http://localhost/  
  28.     server_name localhost;  
  29.   
  30.     location / {  
  31.         # First attempt to serve request as file, then  
  32.         # as directory, then fall back to index.html  
  33.         try_files $uri $uri/ /index.html;  
  34.     }  
  35.   
  36.     location /doc {  
  37.         root /usr/share;  
  38.         autoindex on;  
  39.         allow 127.0.0.1;  
  40.         deny all;  
  41.     }  
  42.   
  43.     location /images {  
  44.         root /usr/share;  
  45.         autoindex off;  
  46.     }  
  47.   
  48.     #error_page 404 /404.html;  
  49.   
  50.     # redirect server error pages to the static page /50x.html  
  51.     #  
  52.     #error_page 500 502 503 504 /50x.html;  
  53.     #location = /50x.html {  
  54.     #   root /usr/share/nginx/www;  
  55.     #}  
  56.   
  57.     # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  58.     #  
  59.     #location ~ \.php$ {  
  60.     #   proxy_pass http://127.0.0.1;  
  61.     #}  
  62.   
  63.     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  64.     #  
  65.     #location ~ \.php$ {  
  66.     #   fastcgi_pass 127.0.0.1:9000;  
  67.     #   fastcgi_index index.php;  
  68.     #   include fastcgi_params;  
  69.     #}  
  70.   
  71.     # deny access to .htaccess files, if Apache's document root  
  72.     # concurs with nginx's one  
  73.     #  
  74.     #location ~ /\.ht {  
  75.     #   deny all;  
  76.     #}  
  77. }  
  78.   
  79.   
  80. # another virtual host using mix of IP-, name-, and port-based configuration  
  81. #  
  82. #server {  
  83. #   listen 8000;  
  84. #   listen somename:8080;  
  85. #   server_name somename alias another.alias;  
  86. #   root html;  
  87. #   index index.html index.htm;  
  88. #  
  89. #   location / {  
  90. #       try_files $uri $uri/ /index.html;  
  91. #   }  
  92. #}  
  93.   
  94.   
  95. # HTTPS server  
  96. #  
  97. #server {  
  98. #   listen 443;  
  99. #   server_name localhost;  
  100. #  
  101. #   root html;  
  102. #   index index.html index.htm;  
  103. #  
  104. #   ssl on;  
  105. #   ssl_certificate cert.pem;  
  106. #   ssl_certificate_key cert.key;  
  107. #  
  108. #   ssl_session_timeout 5m;  
  109. #  
  110. #   ssl_protocols SSLv3 TLSv1;  
  111. #   ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;  
  112. #   ssl_prefer_server_ciphers on;  
  113. #  
  114. #   location / {  
  115. #       try_files $uri $uri/ /index.html;  
  116. #   }  
  117. #}  

这样默认的端口就修改成7888了


nginx的服务器启动,重启,停止

[php] view plaincopy
  1. root@coder-671T-M:/etc/nginx/sites-available# /etc/init.d/nginx start  
  2. Starting nginx: nginx.  
  3. root@coder-671T-M:/etc/nginx/sites-available# /etc/init.d/nginx start  
  4. Starting nginx: nginx.  
  5. root@coder-671T-M:/etc/nginx/sites-available# /etc/init.d/nginx stop  
  6. Stopping nginx: nginx.  
  7. root@coder-671T-M:/etc/nginx/sites-available# /etc/init.d/nginx restart  
  8. Restarting nginx: nginx.  

查看端口情况

[php] view plaincopy
  1. root@coder-671T-M:/etc/nginx/sites-available# lsof -i:7888  
  2. COMMAND  PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME  
  3. nginx   6029     root    6u  IPv4  89046      0t0  TCP *:7888 (LISTEN)  
  4. nginx   6030 www-data    6u  IPv4  89046      0t0  TCP *:7888 (LISTEN)  
  5. nginx   6031 www-data    6u  IPv4  89046      0t0  TCP *:7888 (LISTEN)  
  6. nginx   6032 www-data    6u  IPv4  89046      0t0  TCP *:7888 (LISTEN)  
  7. nginx   6033 www-data    6u  IPv4  89046      0t0  TCP *:7888 (LISTEN)  
  8. root@coder-671T-M:/etc/nginx/sites-available# netstat -anpl | grep 7888  
  9. tcp        0      0 0.0.0.0:7888            0.0.0.0:*               LISTEN      6029/nginx        
  10. root@coder-671T-M:/etc/nginx/sites-available#   
0 0