CentOS 7 之源码编译安装 Nginx 1.13

▪ 环境

操作系统:CentOS 7.4(阿里云,最小化安装)

软件版本:Nginx 1.13

▪ 安装

1. 基础软件

通过 rpm -qa | grep name 的方式验证以下软件包是否已全部安装

openssl openssl-devel libxml2-devel libxslt libxslt-devel perl-devel perl-ExtUtils-Embed

如果缺少相关的软件包,可通过 yum -y install 的方式在线安装,或直接从系统安装光盘中找到并通过 rpm -ivh 的方式安装。

2. 下载 Nginx 源码包
$ cd /usr/local/src/

$ wget http://nginx.org/download/nginx-1.13.6.tar.gz
$ tar zxvf nginx-1.13.6.tar.gz
$ cd nginx-1.13.6
3. 创建用户、用户组和目录
$ groupadd nginx
$ useradd -M -s /bin/false -g nginx nginx

$ mkdir /var/log/nginx -p
$ chown nginx:nginx -R /var/log/nginx

$ mkdir /var/runtime/nginx -p
$ chown nginx:nginx -R /var/runtime/nginx
4. 下载扩展包,以便 Nginx 支持 Rewrite 和 Gzip 功能
$ cd /usr/local/src/nginx-1.13.6

$ wget http://sourceforge.net/projects/pcre/files/pcre/8.37/pcre-8.37.tar.gz
$ tar zxvf pcre-8.37.tar.gz

$ wget http://www.zlib.net/zlib-1.2.11.tar.gz
$ tar zxvf zlib-1.2.11.tar.gz
5. 安装 NGINX
$ ./configure \
  --prefix=/usr/local/nginx \
  --user=nginx --group=nginx \
  --pid-path=/var/runtime/nginx/nginx.pid \
  --lock-path=/var/runtime/nginx/nginx.lock \
  --http-log-path=/var/log/nginx/access.log \
  --error-log-path=/var/log/nginx/error.log \
  --with-debug --with-http_dav_module --with-http_flv_module --with-http_ssl_module \
  --with-http_xslt_module --with-http_realip_module --with-http_addition_module --with-http_stub_status_module \
  --with-http_sub_module --with-http_random_index_module --with-http_degradation_module --with-http_secure_link_module \
  --with-file-aio --with-mail --with-mail_ssl_module --with-http_gzip_static_module --with-http_perl_module \
  --with-pcre=pcre-8.37 --with-zlib=zlib-1.2.11 \
  --with-ld-opt="-Wl,-E" 
  --http-scgi-temp-path=/var/runtime/nginx/temp_scgi \
  --http-uwsgi-temp-path=/var/runtime/nginx/temp_uwsgi --http-proxy-temp-path=/var/runtime/nginx/temp_proxy \
  --http-fastcgi-temp-path=/var/runtime/nginx/temp_fastcgi --http-client-body-temp-path=/var/runtime/nginx/temp_client_body

$ make && make install
7. 配置开机自启动

由于 CentOS 7 使用 systemd 替换了 SysV, 所以 CentOS 6 下的 service 和 chkconfig 开机自启动将不在适用 CentOS 7。下面将介绍如何配置 Nginx 开机自启动。

新建服务文件:

$ vim /usr/lib/systemd/system/nginx.service

添加服务代码:

[Unit]
Description=nginx
After=network.target
  
[Service]
# service type
Type=forking

# main service
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit

# use private tmp
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

启用服务文件:

$ systemctl enable nginx.service
8. 相关命令
$ systemctl stop nginx.service
$ systemctl start nginx.service
$ systemctl status nginx.service

▪ 配置虚拟主机

1. 创建网站目录
$ mkdir /data/httpd/htdocs -p
$ mkdir /data/httpd/htdocs/xxxx -p
2. 创建虚拟网站配置目录
$ vim /usr/local/nginx/conf/nginx.conf

启用虚拟主机模式:

移除 server{ ... } 段的全部代码

在移除的代码处增加:

include /usr/local/nginx/conf/vhosts/*.conf;

完整的配置文件代码如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # upload limit
    client_max_body_size 100m;
    
    # virtual host
    include /usr/local/nginx/conf/vhosts/*.conf;


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}
$ mkdir /usr/local/nginx/conf/vhosts
$ cd /usr/local/nginx/conf/vhosts

增加默认 default.conf 配置,内容如下:

# 禁止未绑定的域名和IP访问
server {
    listen 80 default;
    server_name _;
    return 444;
}

增加自定义虚拟主机 xxxx.conf 配置,内容如下:

# 允许 xxxx.com 域名访问
server {
    listen       80;
    server_name  xxxx.com *.xxxx.com;

    root        /data/httpd/htdocs/xxxx;
    access_log  /var/log/nginx/access.xxxx.log;

    location / {
        index  index.html index.htm index.php;
        # include /data/httpd/htdocs/xxxx/.nginx;
    }

    # Deny Hidden File
    # Like:.nginx .htaccess .svn
    location ~ /\. {
        deny all;
    }

    # PHP Script
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值