当前位置:Gxlcms > PHP教程 > Linux下安装配置nginx详解

Linux下安装配置nginx详解

时间:2021-07-01 10:21:17 帮助过:26人阅读

一、Linux下安装配置nginx

第一次安装nginx,中间出现的问题一步步解决。

用到的工具secureCRT,连接并登录服务器。

1.1 rz命令,会弹出会话框,选择要上传的nginx压缩包。

  1. #rz

1.2 解压

  1. [root@vw010001135067 ~]# cd /usr/local/
  2. [root@vw010001135067 local]# tar -zvxf nginx-1.10.2.tar.gz

1.3 进入nginx文件夹,执行./configure命令

  1. [root@vw010001135067 local]# cd nginx-1.10.2
  2. [root@vw010001135067 nginx-1.10.2]# ./configure

报错如下:

  1. checking for OS
  2. + Linux 2.6.32-431.el6.x86_64 x86_64
  3. checking for C compiler ... not found
  4. ./configure: error: C compiler cc is not found

出现这个错误。那么就是gcc 包没有安装。

1.3.1 安装gcc

查看gcc

  1. [root@vw010001135067 nginx-1.10.2]# whereis gcc
  2. gcc:

安装gcc

  1. [root@vw010001135067 nginx-1.10.2]# yum -y install gcc

安装成功后再次查看

  1. [root@vw010001135067 nginx-1.10.2]# whereis gcc
  2. gcc: /usr/bin/gcc /usr/lib/gcc /usr/libexec/gcc /usr/share/man/man1/gcc.1.gz

gcc安装好了。

1.3.2 继续执行./configure

  1. [root@vw010001135067 nginx-1.10.2]# ./configure
  2. checking for OS
  3. + Linux 2.6.32-431.el6.x86_64 x86_64
  4. checking for C compiler ... found
  5. ......
  6. checking for PCRE library ... not found
  7. checking for PCRE library in /usr/local/ ... not found
  8. checking for PCRE library in /usr/include/pcre/ ... not found
  9. checking for PCRE library in /usr/pkg/ ... not found
  10. checking for PCRE library in /opt/local/ ... not found
  11. ./configure: error: the HTTP rewrite module requires the PCRE library.
  12. You can either disable the module by using --without-http_rewrite_module
  13. option, or install the PCRE library into the system, or build the PCRE library
  14. statically from the source with nginx by using --with-pcre=<path> option.

出现如上错误。安装pcre-devel

  1. [root@vw010001135067 nginx-1.10.2]# yum install pcre-devel

1.3.3 再次执行./configure

  1. error: the HTTP gzip module requires the zlib library.
  2. You can either disable the module by using --without-http_gzip_module
  3. option, or install the zlib library into the system, or build the zlib library
  4. statically from the source with nginx by using --with-zlib=<path> option.

如果有这个错误 那么执行

  1. yum install zlib-devel

1.3.4 执行./configure后没有报错

  1. [root@vw010001135067 nginx-1.10.2]# ./configure
  2. checking for OS
  3. + Linux 2.6.32-431.el6.x86_64 x86_64
  4. checking for C compiler ... found
  5. + using GNU C compiler
  6. + gcc version: 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
  7. .......
  8. Configuration summary
  9. + using system PCRE library
  10. + OpenSSL library is not used
  11. + md5: using system crypto library
  12. + sha1: using system crypto library
  13. + using system zlib library
  14. nginx path prefix: "/usr/local/nginx"
  15. nginx binary file: "/usr/local/nginx/sbin/nginx"
  16. nginx modules path: "/usr/local/nginx/modules"
  17. nginx configuration prefix: "/usr/local/nginx/conf"
  18. nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  19. nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  20. nginx error log file: "/usr/local/nginx/logs/error.log"
  21. nginx http access log file: "/usr/local/nginx/logs/access.log"
  22. nginx http client request body temporary files: "client_body_temp"
  23. nginx http proxy temporary files: "proxy_temp"
  24. nginx http fastcgi temporary files: "fastcgi_temp"
  25. nginx http uwsgi temporary files: "uwsgi_temp"
  26. nginx http scgi temporary files: "scgi_temp"

1.4 如果你想使用openssl 功能,sha1 功能。 那么安装openssl ,sha1 吧

  1. [root@vw010001135067 nginx-1.10.2]# yum install openssl openssl-devel
  2. [root@vw010001135067 nginx-1.10.2]# install perl-Digest-SHA1.x86_64

1.4.1 开启ssl 模块 执行./configure –with-http_ssl_module

  1. [root@vw010001135067 nginx-1.10.2]# ./configure --with-http_ssl_module

1.4.2 启用“server+status”页,执行./configure –with-http_stub_status_module

  1. [root@vw010001135067 nginx-1.10.2]# ./configure --with-http_stub_status_module

上面两个命令同时启动可以

  1. [root@vw010001135067 nginx-1.10.2]# ./configure --with-http_stub_status_module --with-http_ssl_module

1.5 上面configure就通过了

执行make 命令,执行make install 命令

  1. [root@vw010001135067 nginx-1.10.2]# make
  2. [root@vw010001135067 nginx-1.10.2]# make install

至此,nginx 执行成功了

1.6 配置环境变量

在/etc/profile 中加入配置

打开配置文件

  1. [root@vw010001135067 nginx-1.10.2]# vi /etc/profile

在配置文件中加入

  1. #nginx configure
  2. export NGINX_HOME=/usr/local/nginx-1.10.2
  3. export PATH=$PATH:$NGINX_HOME/sbin

我开始像上面填写,结果nginx -v的时候查找不到。注意到上面我的nginx_home配置的地址不对。先找到nginx的安装地址

  1. [root@vw010001135067 nginx-1.10.2]# whereis nginx
  2. nginx: /usr/local/nginx

还真是地址写错了,把上面的改成

  1. #nginx configure
  2. export NGINX_HOME=/usr/local/nginx
  3. export PATH=$PATH:$NGINX_HOME/sbin

编译完保存退出并执行

  1. [root@vw010001135067 nginx-1.10.2]# source /etc/profile


使配置生效。

1.7 查看nginx版本

  1. [root@vw010001135067 nginx]# nginx -v
  2. nginx version: nginx/1.10.2

整个过程成功了!

二、修改nginx.conf

2.1 启动nginx

我的nginx服务在http://10.1.135.67/,配置成功后,现在启动nginx

  1. [root@vw010001135067 nginx]# cd /usr/local/nginx
  2. [root@vw010001135067 nginx]# nginx -c conf/nginx.conf

启动成功,在浏览器打开http://10.1.135.67/,默认端口号80.

Linux下安装配置nginx详解

如上图,nginx已经正常工作了。

2.2 配置tomcat服务

现在我的tomcat服务在10.1.29.15,需要通过nginx转发。那么打开nginx.conf,修改配置文件。如下,添加:

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;#最大连接数,默认为512
  9. accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
  10. multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
  11. #use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
  12. }
  13. http {
  14. #文件扩展名与文件类型映射表
  15. include mime.types;
  16. #默认文件类型,默认为text/plain
  17. default_type application/octet-stream;
  18. #自定义格式
  19. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  20. '$status $body_bytes_sent "$http_referer" '
  21. '"$http_user_agent" "$http_x_forwarded_for"';
  22. #combined为日志格式的默认值
  23. access_log logs/access.log main;
  24. #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块
  25. sendfile on;
  26. sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
  27. #tcp_nopush on;
  28. #连接超时时间,默认为75s,可以在http,server,location块。
  29. keepalive_timeout 65;
  30. #gzip on;
  31. upstream upload {
  32. server 10.1.29.15:8080;
  33. }
  34. error_page 404 https://www.baidu.com; #错误页
  35. server {
  36. keepalive_requests 120; #单连接请求上限次数。
  37. listen 80; #监听端口
  38. server_name localhost; #监听地址
  39. #charset koi8-r;
  40. #access_log logs/host.access.log main;
  41. location ~ ^.*?/upload/[^/]*?$ {
  42. proxy_connect_timeout 15;
  43. proxy_send_timeout 15;
  44. proxy_read_timeout 15;
  45. proxy_set_header Host $host;
  46. proxy_set_header X-Real-IP $remote_addr;
  47. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  48. proxy_set_header Connection "";
  49. proxy_pass http://upload; #请求转向upload 定义的服务器列表
  50. client_max_body_size 1024m;
  51. }
  52. }
  53. }

配置好后,保存配置文件,并且重启nginx

  1. [root@vw010001135067 nginx]# nginx -s reload

在浏览器调用upload项目是否成功

Linux下安装配置nginx详解

如图能正确访问项目,配置成功!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHP中文网。

更多Linux下安装配置nginx详解相关文章请关注PHP中文网!

人气教程排行