CentOS安装配置Nginx服务器

CentOS安装配置Nginx服务器

本文参考http://www.runoob.com/linux/nginx-install-setup.html编写

安装成功图示

这里写图片描述

搭建环境

  1. CentOS Linux release 7.3.1611 (Core) (64)

查看系统版本

  1. 查看版本: cat /etc/redhat-release
  2. 查看64or32:getconf LONG_BIT

安装步骤:

安装过程可能存在权限问题,可以使用root用户或命令前添加sudo

  1. 安装编译工具及库文件:

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

    如果这个命令没法运行出现

    Another app is currently holding the yum lock; waiting for it to exit… The other application is: PackageKit Memory : 141 M RSS (546 MB VSZ) Started: Mon Apr 10 17:56:46 2017 - 01:23 ago

    运行下面命令将yum暂定掉

    pkill yum
  2. 安装PCRE:

    cd /usr/local/src/
    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
    make && make install

  6. 查看pcre版本:

    pcre-config --version
  7. 下载Nginx:

    wget http://nginx.org/download/nginx-1.6.2.tar.gz
  8. 解压安装包:

    tar zxvf nginx-1.6.2.tar.gz
  9. 进入安装包目录:

    cd nginx-1.6.2
  10. 编译安装:

    ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35

    安装nginx在/usr/local/webserver/nginx 目录下,该目录可以自定义。

    make && make install

  11. 查看nginx版本:

    /usr/local/webserver/nginx/sbin/nginx -v
  12. 创建nginx用户:

    /usr/sbin/groupadd www 

    www可自定义

    /usr/sbin/useradd -g www www

    第一个www为用户组 第二个www为用户名 两个参数都可自定义

  13. 修改nginx.conf 文件自定义nginx启动参数

    cd  /usr/local/webserver/nginx/conf/
    rm nginx.conf

    删除原来的nginx.conf 配置文件

    vim 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 8888;#监听端口
        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;
      }
    
    }
  14. 检查配置文件nginx.conf 是否正确:

    cd  /usr/local/webserver/nginx/sbin/
    ./nginx -t
  15. 启动nginx:

    ./nginx 

    nginx其他命令:

    ./nginx -t #检查nginx.conf配置文件是否正确
    ./nginx -s reload #重新载入配置文件
    ./nginx -s reopen #重启nginx
    ./nginx -s stop  #停止nginx

  16. 关闭CentOS防火墙:

    service iptables stop 

    防火墙其他命令:

    servive iptables status #查看防火墙状态
    service iptables start #启动防火墙
    service iptables stop #关闭防火墙
    service iptables restart #重启防火墙

  17. 登录nginx服务器:

    在浏览器输入CentOS的IP地址+端口号,如下图所示:

    这里写图片描述

    补充其他命令

    1. lsof -i :8888 #查看端口号8888下运行的进程
    2. ps -ef | grep nginx #查看所有与nginx相关的进程
    3. kill -9 8888 #杀死pid为8888的进程
    4. pkill nginx #杀死所有与nginx相关的进程

注:

本文属于作者原创,如需转载,请注明。

内部如果引用的文字,连接,图片等资源存在侵犯原作者的情况,请联系本人,立即删除。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值