欢迎光临
我们一直在努力

centos7通过编译源码的方式安装nginx

环境:

  • 系统:digitalocean CentOS 7.4 x64
  • nginx: nginx-1.14.2.tar.gz

为了使Nginx能够启用http/2,需要通过源码的方式安装nginx,并指定--with-openssl的版本在1.0.2版本以上,具体安装步骤如下:

1.安装可选依赖

sudo yum -y install  gcc gcc-c++ ncurses-devel perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel

2.下载nginx

$ cd /usr/local/src/
$ sudo wget http://nginx.org/download/nginx-1.14.2.tar.gz && sudo tar zxvf nginx-1.14.2.tar.gz

3.源码安装必备依赖

nginx 需要依赖3个库,分别是:PCRE, zlib and OpenSSL,接下来我们从源码进行安装:

$ cd /usr/local/src/

# PCRE version 8.40
$ sudo wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz && sudo tar xzvf pcre-8.40.tar.gz

# zlib version 1.2.11
$ sudo wget https://www.zlib.net/zlib-1.2.11.tar.gz && sudo tar xzvf zlib-1.2.11.tar.gz

# OpenSSL version openssl-1.1.0h
$ sudo wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz && sudo tar xzvf openssl-1.1.0h.tar.gz

4.删除不需要的源码包

$ sudo rm -rf *.tar.gz

5.编译

cd /usr/local/src/nginx-1.14.2

./configure --prefix=/etc/nginx \
            --sbin-path=/usr/sbin/nginx \
            --modules-path=/usr/lib64/nginx/modules \
            --conf-path=/etc/nginx/nginx.conf \
            --error-log-path=/var/log/nginx/error.log \
            --pid-path=/var/run/nginx.pid \
            --lock-path=/var/run/nginx.lock \
            --user=nginx \
            --group=nginx \
            --build=CentOS \
            --builddir=nginx-1.14.2 \
            --with-select_module \
            --with-poll_module \
            --with-threads \
            --with-file-aio \
            --with-http_ssl_module \
            --with-http_v2_module \
            --with-http_realip_module \
            --with-http_addition_module \
            --with-http_xslt_module=dynamic \
            --with-http_image_filter_module=dynamic \
            --with-http_geoip_module=dynamic \
            --with-http_sub_module \
            --with-http_dav_module \
            --with-http_flv_module \
            --with-http_mp4_module \
            --with-http_gunzip_module \
            --with-http_gzip_static_module \
            --with-http_auth_request_module \
            --with-http_random_index_module \
            --with-http_secure_link_module \
            --with-http_degradation_module \
            --with-http_slice_module \
            --with-http_stub_status_module \
            --http-log-path=/var/log/nginx/access.log \
            --http-client-body-temp-path=/var/cache/nginx/client_temp \
            --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
            --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
            --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
            --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
            --with-mail=dynamic \
            --with-mail_ssl_module \
            --with-stream=dynamic \
            --with-stream_ssl_module \
            --with-stream_realip_module \
            --with-stream_geoip_module=dynamic \
            --with-stream_ssl_preread_module \
            --with-compat \
            --with-pcre=../pcre-8.40 \
            --with-pcre-jit \
            --with-zlib=../zlib-1.2.11 \
            --with-openssl=../openssl-1.1.0h \
            --with-openssl-opt=no-nextprotoneg \
            --with-debug

6.安装

cd /usr/local/src/nginx-1.14.2

make 

sudo make install

7.创建软链

sudo ln -s /usr/lib64/nginx/modules /etc/nginx/modules

这样就可以在nginx的配置文件中像这样加载动态模块:load_module modules/ngx_foo_module.so;

8.查看编译信息

sudo nginx -V

9.修改/etc/nginx/nginx.conf

去掉 user nobody;一行的注释。

10.检查配置是否存在潜在错误

## 执行检查可能会报错
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

## 如果报上面的错误,则执行下面的代码
sudo mkdir -p /var/cache/nginx && sudo nginx -t

11.创建nginx系统服务单元

编辑文件:

sudo vi /usr/lib/systemd/system/nginx.service

将下面的内容复制进去:

[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

12.启动nginx并设置开机启动

sudo systemctl start nginx.service && sudo systemctl enable nginx.service

13.检查系统重启是否会自动重启

sudo systemctl is-enabled nginx.service

14.相关错误及解决办法

1.getpwnam("nginx") failed

解决办法:编辑/etc/nginx/nginx.conf,将user nobody的注释去掉既可,参考:http://www.iitshare.com/install-nginx-nginx-emerg-getpwnam-www-failed-error.html。

2.执行sudo nginx -t报错nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)

解决办法:sudo mkdir -p /var/cache/nginx && sudo nginx -t

参考文章:

  1. How to Compile Nginx From Source on CentOS 7
赞(0)
未经允许不得转载:Ddmit » centos7通过编译源码的方式安装nginx

评论 5

  1. #1

    Job for nginx.service failed because the control process exited with error code. See “systemctl status nginx.service” and “journalctl -xe” for details.
    博主能麻烦帮忙看看吗? 按照步骤操作,最后设置开机启动的时候报错,nginx.service也是直接复制进去的

    螺旋升天6年前 (2019-07-08)回复
    • 运行systemctl status nginx.service命令可以看下有什么错

      michael6年前 (2019-07-08)回复
  2. #2

    ● nginx.service – nginx – high performance web server
    Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
    Active: failed (Result: exit-code) since Mon 2019-07-08 12:29:58 CST; 1 day 3h ago
    Docs: https://nginx.org/en/docs/
    Process: 2529 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)

    Jul 08 12:29:58 izj6chhbyob5va2jsq3k2qz systemd[1]: Starting nginx – high performance web server…
    Jul 08 12:29:58 izj6chhbyob5va2jsq3k2qz nginx[2529]: nginx: the configuration file /etc/nginx/nginx.c… ok
    Jul 08 12:29:58 izj6chhbyob5va2jsq3k2qz nginx[2529]: nginx: [emerg] mkdir() “/var/cache/nginx/client_…ry)
    Jul 08 12:29:58 izj6chhbyob5va2jsq3k2qz nginx[2529]: nginx: configuration file /etc/nginx/nginx.conf …led
    Jul 08 12:29:58 izj6chhbyob5va2jsq3k2qz systemd[1]: nginx.service: control process exited, code=exite…s=1
    Jul 08 12:29:58 izj6chhbyob5va2jsq3k2qz systemd[1]: Failed to start nginx – high performance web server.
    Jul 08 12:29:58 izj6chhbyob5va2jsq3k2qz systemd[1]: Unit nginx.service entered failed state.
    Jul 08 12:29:58 izj6chhbyob5va2jsq3k2qz systemd[1]: nginx.service failed.
    Hint: Some lines were ellipsized, use -l to show in full.

    螺旋升天6年前 (2019-07-09)回复
    • 14.相关错误及解决办法 第二个就是解决办法

      michael6年前 (2019-07-10)回复

登录

找回密码

注册