В этом гайде мы установим Nginx со всеми доступными модулями Nginx из открытого исходного кода.
Nginx может использоваться как HTTP/HTTPS-сервер, обратный прокси-сервер, почтовый прокси-сервер, балансировщик нагрузки или сервер кэширования.
Начальные шаги
Обновите систему.
# dnf update
# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
Сборка NGINX из исходного кода
Установите «Development Tools» .
# dnf groupinstall 'Development Tools'
Установите Extra Packages for Enterprise Linux (EPEL).
# dnf install epel-release
Загрузите последнюю версию исходного кода NGINX и распакуйте его.
# wget https://nginx.org/download/nginx-1.17.8.tar.gz && tar zxvf nginx-1.17.8.tar.gz
Загрузите обязательный исходный код Nginx-зависимостей и распакуйте их.
# PCRE version 8.44
# wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz && tar zxvf pcre-8.44.tar.gz
# zlib version 1.2.11
# wget https://www.zlib.net/zlib-1.2.11.tar.gz && tar xzvf zlib-1.2.11.tar.gz
# OpenSSL version 1.1.1d
# wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz && tar zxvf openssl-1.1.1d.tar.gz
Установите опциональные зависимости Nginx .
# dnf install perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel
Перейдите в директорию с исходным кодом Nginx.
# cd nginx-1.17.8/
Сконфигурируйте, скомпилируйте и установите Nginx.
./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.17.8 \
--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 \
--with-http_perl_module=dynamic \
--with-perl_modules_path=/usr/lib64/perl5 \
--with-perl=/usr/bin/perl \
--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.44 \
--with-pcre-jit \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.1d \
--with-openssl-opt=no-nextprotoneg \
--with-debug
# make
# make install
Создайте симлинк /usr/lib64/nginx/modules на директорию /etc/nginx/modules, чтобы вы могли загружать динамические модули в кофигурацию Nginx, например, load_module modules/ngx_foo_module.so;
# ln -s /usr/lib64/nginx/modules /etc/nginx/modules
Проверьте версию Nginx и установленные модули.
# nginx -V
nginx version: nginx/1.17.8 (CentOS)
built by gcc 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)
built with OpenSSL 1.1.1d 10 Sep 2019
TLS SNI support enabled
configure arguments: --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.17.8 --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 --with-http_perl_module=dynamic --with-perl_modules_path=/usr/lib64/perl5 --with-perl=/usr/bin/perl --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.44 --with-pcre-jit --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.1.1d --with-openssl-opt=no-nextprotoneg --with-debug
Создайте системную группу Nginx и пользователя.
# useradd --system --home /var/cache/nginx --shell /sbin/nologin --comment "nginx user" --user-group nginx
Создайте директорию.
# mkdir -p /var/cache/nginx
Проверьте синтаксис и возможные ошибки.
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Создайте Nginx systemd unit файл.
# nano /etc/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
Запустите и добавьте в автозагрузку службу.
# systemctl start nginx.service && sudo systemctl enable nginx.service
Проверьте, работает ли Nginx.
# systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2020-02-18 10:14:54 CET; 1min 5s ago
Docs: https://nginx.org/en/docs/
Main PID: 30581 (nginx)
Tasks: 2 (limit: 24434)
Memory: 1.7M
CGroup: /system.slice/nginx.service
├─30581 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─30582 nginx: worker process
Feb 18 10:14:54 uni systemd[1]: Starting nginx - high performance web server...
Feb 18 10:14:54 uni nginx[30578]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Feb 18 10:14:54 uni nginx[30578]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Feb 18 10:14:54 uni systemd[1]: Started nginx - high performance web server.
Вы можете открыть свой браузер и ввести свой домен / IP-адрес, чтобы увидеть страницу Nginx по умолчанию.
Теперь у вас установлена последняя версия Nginx, собранная из исходного кода.