centos安装nginx教程

    技术2024-01-01  101

    http://www.hebinghua.com/linux/28.html

    安装nginx配到的坑比事情。

     

    https://www.runoob.com/linux/nginx-install-setup.html

    安装nginx非常详细的教程

     

    -----------------------------------------------------------------------------------------------------------------

    正文

     

    1 安装编译工具库文件

    yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

    这一步没有要求路径

    2 安装PCRE 

    yum -y install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel

    可以一起yum install 也可以去下载好源码包 然后安装即可

    wget http://nginx.org/download/nginx-1.6.2.tar.gz

    然后解压  ./configure   make && make install

    安装好了之后我们执行这个测试一下 pcre-config --version是否安装好了。

    3 安装nginx

    2.1 官方网站下载Nginx, 默认安装路径:/usr/local/nginx

    进入 cd /usr/local/nginx

    下载命令:

    wget http://nginx.org/download/nginx-1.12.2.tar.gz

    2.2解压

    tar zxvf nginx-1.12.2.tar.gz

    进入nginx源文件目录

    cd nginx-1.12.2

    2.3编译

    默认https没有打开,需要添加 --with-http_ssl_module

    执行命令./configure --with-http_ssl_module

    2.4 安装Nginx 执行make && make install

    2.5 启动Nginx

    /usr/local/nginx/sbin/nginx

    浏览器输入http://服务器ip:80

     总结:这个地方没那么顺利的,会遇见坑逼,详细的后面看 坑逼打全集。

     

    4 nginx配置

    创建 Nginx 运行使用的用户 www:

    # /usr/sbin/groupadd www  # /usr/sbin/useradd -g www www

    配置nginx.conf ,将/usr/local/nginx/conf/nginx.conf替换为以下内容

    [root@bogon conf]#  cat /usr/local/nginx/conf/nginx.conf user www www; worker_processes 2; #设置值和CPU核心数一致 error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别 pid /usr/local/webserver/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 65535; events {   use epoll;   worker_connections 65535; } http {   include mime.types;   default_type application/octet-stream;   log_format main  '$remote_addr - $remote_user [$time_local] "$request" '                '$status $body_bytes_sent "$http_referer" '                '"$http_user_agent" $http_x_forwarded_for';    #charset gb2312;         server_names_hash_bucket_size 128;   client_header_buffer_size 32k;   large_client_header_buffers 4 32k;   client_max_body_size 8m;         sendfile on;   tcp_nopush on;   keepalive_timeout 60;   tcp_nodelay on;   fastcgi_connect_timeout 300;   fastcgi_send_timeout 300;   fastcgi_read_timeout 300;   fastcgi_buffer_size 64k;   fastcgi_buffers 4 64k;   fastcgi_busy_buffers_size 128k;   fastcgi_temp_file_write_size 128k;   gzip on;    gzip_min_length 1k;   gzip_buffers 4 16k;   gzip_http_version 1.0;   gzip_comp_level 2;   gzip_types text/plain application/x-javascript text/css application/xml;   gzip_vary on;     #limit_zone crawler $binary_remote_addr 10m;  #下面是server虚拟主机的配置  server   {     listen 80;#监听端口     server_name localhost;#域名     index index.html index.htm index.php;     root /usr/local/webserver/nginx/html;#站点目录       location ~ .*\.(php|php5)?$     {       #fastcgi_pass unix:/tmp/php-cgi.sock;       fastcgi_pass 127.0.0.1:9000;       fastcgi_index index.php;       include fastcgi.conf;     }     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$     {       expires 30d;   # access_log off;     }     location ~ .*\.(js|css)?$     {       expires 15d;    # access_log off;     }     access_log off;   } }

    检查配置文件nginx.conf的正确性命令:

    /usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload # 重新载入配置文件 /usr/local/nginx/sbin/nginx -s reopen # 重启 Nginx /usr/local/n-inx/sbin/nginx -s stop # 停止 Nginx

     

    坑来了:

    扩展模块,需要编译安装的nginx,版本自己选择(我的是1.14.0)

        1.获取安装包

        wget http://nginx.org/download/nginx-1.14.1.tar.gz     wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master

        2.安装扩展依赖

        yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

        

    3.给nginx打个补丁(加入nginx+nginx_upstream_check_module)

            1>.进入nginx-1.14目录(源码目录)

        patch -p1 < /usr/download/nginx_upstream_check_module-master/check_1.14.0+.patch

        ps:我的两个包下载在/usr/download/,这个自定义 ;master加压后的文件为nginx_upstream_check_module-master 里面包含了多个补丁版本,根据自己的nginx版本选择

        2>.新建一个nginx的安装目录,我的在/usr/local/nginx-1.14.1,然后还在nginx的源码目录中执行

        ./configure --prefix=/usr/local/nginx-1.14.1 --add-module=/usr/download/nginx_upstream_check_module-master/

          正常现象:

        Configuration summary       + using system PCRE library       + OpenSSL library is not used       + using system zlib library            nginx path prefix: "/usr/local/nginx-1.14.1"       nginx binary file: "/usr/local/nginx-1.14.1/sbin/nginx"       nginx modules path: "/usr/local/nginx-1.14.1/modules"       nginx configuration prefix: "/usr/local/nginx-1.14.1/conf"       nginx configuration file: "/usr/local/nginx-1.14.1/conf/nginx.conf"       nginx pid file: "/usr/local/nginx-1.14.1/logs/nginx.pid"       nginx error log file: "/usr/local/nginx-1.14.1/logs/error.log"       nginx http access log file: "/usr/local/nginx-1.14.1/logs/access.log"       nginx http client request body temporary files: "client_body_temp"       nginx http proxy temporary files: "proxy_temp"       nginx http fastcgi temporary files: "fastcgi_temp"       nginx http uwsgi temporary files: "uwsgi_temp"       nginx http scgi temporary files: "scgi_temp"

       

        大概率报错:

        [root@localhost nginx-1.14.1]# ./configure --prefix=/usr/local/nginx-1.14.1 --add-module=/usr/download/nginx_upstream_check_module-master/     checking for OS      + Linux 4.18.0-80.el8.x86_64 x86_64     checking for C compiler ... not found          ./configure: error: C compiler cc is not found

       解决办法:

        yum -y install gcc gcc-c++ autoconf automake make

     

        3>.编译 make

        make

        大概率出现的报错:

        make[1]: 进入目录“/usr/download/nginx-1.14.1”     cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I /usr/download/nginx_upstream_check_module-master/ -I objs \      -o objs/src/os/unix/ngx_user.o \      src/os/unix/ngx_user.c     src/os/unix/ngx_user.c: 在函数‘ngx_libc_crypt’中:     src/os/unix/ngx_user.c:26:7: 错误:‘struct crypt_data’没有名为‘current_salt’的成员          cd.current_salt[0] = ~salt[0];            ^     make[1]: *** [objs/Makefile:788:objs/src/os/unix/ngx_user.o] 错误 1

        解决办法: 打开 src/os/unix/ngx_user.c  

        #ifdef __GLIBC__         /* work around the glibc bug */         cd.current_salt[0] = ~salt[0];     ##把这一行注释掉     #endif

        4>.编译安装

        make install

        大概率报错:

            igned int (*)(struct <匿名> *)’} to ‘void (*)(ngx_http_script_engine_t *)’ {或称 ‘void (*)(struct <匿名> *)’} [-Werror=cast-function-type]          code->code = (ngx_http_script_code_pt)                       ^     src/http/ngx_http_script.c: 在函数‘ngx_http_script_add_full_name_code’中:     src/http/ngx_http_script.c:1296:18: 错误:cast between incompatible function types from ‘size_t (*)(ngx_http_script_engine_t *)’ {或称 ‘long unsigned int (*)(struct <匿名> *)’} to ‘void (*)(ngx_http_script_engine_t *)’ {或称 ‘void (*)(struct <匿名> *)’} [-Werror=cast-function-type]          code->code = (ngx_http_script_code_pt) ngx_http_script_full_name_len_code;                       ^     cc1:所有的警告都被当作是错误     make[1]: *** [objs/Makefile:893:objs/src/http/ngx_http_script.o] 错误 1     make[1]: 离开目录“/usr/download/nginx-1.14.1”     make: *** [Makefile:8:build] 错误 2

        解决办法:打开  vim objs/Makefile  把 -Werrori删掉   (-Werror,它要求GCC将所有的警告当成错误进行处理)

        5.>启动nginx

           在刚刚自定义的nginx安装目录里有生成的文件,其中,启动文件在sbin里

            直接启动:./sbin/nginx -s reload

            有可能出现报错:

            root@localhost sbin]# ./nginx -s reload         nginx: [error] open() "/usr/local/nginx-1.14.1/logs/nginx.pid" failed (2: No such file or directory)

            解决办法:

            [root@localhost sbin]# /usr/local/nginx-1.14.1/sbin/nginx -c /usr/local/nginx-1.14.1/conf/nginx.conf         [root@localhost sbin]# cd ../logs/         [root@localhost logs]# ll         总用量 8         -rw-r--r-- 1 root root   0 1月   7 15:11 access.log         -rw-r--r-- 1 root root 593 1月   7 15:30 error.log         -rw-r--r-- 1 root root   6 1月   7 15:33 nginx.pid

            然后就可以正常启动了

        4.nginx配置

            打开nginx.conf配置文件添加  include /etc/vhost/*.conf  (个人习惯,引入一个配置文件)

     

            然后根据在《nginx均衡负载》一文中跟均衡负载搭配使用。

     

            

        5.检测可视化

            在nginx的配置里加上一段

        location /nastatus {         check_status;         access_log off;     }

        ps:/nastatus 自定义一个路径

          然后打开web访问这个地址 比如 http://192.168.5.165/nastatus

    Processed: 0.014, SQL: 9