美文网首页
CentOs安装Nginx

CentOs安装Nginx

作者: 岸边露伴一动不动 | 来源:发表于2020-07-14 17:01 被阅读0次

    CentOs下安装Nginx有两种方法:

    一、源文件安装

    1、安装编译工具及库文件

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

    2、安装PCRE

    PCRE库支持正则表达式。如果我们在配置文件nginx.conf中使用了正则表达式,那么在编译Nginx时就必须把PCRE库编译进Nginx,因为Nginx的HTTP模块需要靠它来解析正则表达式。
    1、进入安装目录
    cd /usr/local/src/
    2、wget下载安装包
    wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
    3、解压安装包
    tar zxvf pcre-8.35.tar.gz
    4、进入解压后的目录
    cd pcre-8.35
    5、配置
    ./configure
    6、编译安装
    make && make install
    7、查看PCRE版本
    pcre-config --version

    3、安装Nginx

    1、下载安装包
    cd /usr/local/src/
    wget http://nginx.org/download/nginx-1.6.2.tar.gz
    2、解压并进入目录
    tar zxvf nginx-1.6.2.tar.gz
    cd nginx-1.6.2
    3、编译安装
    ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
    make && make install

    ./configure 命令的 --prefix 用于指定安装路径,--with用于指定依赖 --with-依赖包名称=依赖包目录
    如果不使用 --prefix 指定路径,则安装时可执行文件默认放在 /usr/local/bin ,库文件默认放在 /usr/local/lib ,配置文件默认放在 /usr/local/etc 。其它的资源文件放在 /usr/local/share

    4、配置Nginx

    1、创建Nginx运行使用的用户:
    /usr/sbin/groupadd www
    /usr/sbin/useradd -g www www
    2、配置 nginx.conf ,进入/usr/local/src/nginx-1.6.2/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/webserver/nginx/sbin/nginx -t
    如果显示如下:

    ➜  conf /usr/local/webserver/nginx/sbin/nginx -t
    nginx: the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/webserver/nginx/conf/nginx.conf test is successful
    

    则证明配置正确。

    5、启动Nginx

    /usr/local/webserver/nginx/sbin/nginx
    我在启动时报错如下:

    ➜  conf /usr/local/webserver/nginx/sbin/nginx
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] still could not bind()
    

    从配置文件中可以看到我们使用的是80端口,而这个错误是由于80端口被占用,查看占用80端口号的进程:
    netstat -ntlp|grep 80
    显示如下:

    ➜  conf netstat -ntlp|grep 80
    tcp6       0      0 :::80                   :::*                    LISTEN      7732/httpd
    

    Apache占用了80端口号,我们可以杀死占用端口号的进程:kill 7732,或者将配置文件中 listen 80 修改为别的端口号。我这边采用的第二种,修改为了 2333 端口,重新运行Nginx启动命令,无报错信息,启动成功。

    ➜  ~ /usr/local/webserver/nginx/sbin/nginx
    You have new mail.
    ➜  ~
    

    6、访问站点

    image.png

    如果是修改配置文件监听端口号,记得在网址后加端口号哦。

    至此,Nginx就安装成功啦。

    二、yum安装

    yum安装nginx就比较简单啦,执行命令yum install nginx即可
    安装成功后,可以使用systemctl命令

    • 启动nginx:systemctl start nginx
    • 加入开机启动:systemctl enable nginx
    • 查看nginx的状态:systemctl status nginx

    yum安装后,Nginx的配置文件在哪里呢,可以通过查找Nginx安装位置来查找配置文件:
    ps -ef |grep nginx
    显示如下:

    image.png
    再执行/usr/sbin/nginx -t
    image.png
    可以看到配置文件目录为/etc/nginx/nginx.conf

    相关文章

      网友评论

          本文标题:CentOs安装Nginx

          本文链接:https://www.haomeiwen.com/subject/ohhzcktx.html