Một số lý do gì đó mà bạn không thể install nginx khi dùng lệnh yum install nginx. Nếu lười fix, suy nghĩ thì follow theo bài viết này để tiến hành cài nginx, setup config để chuẩn bị cho các mục đích như reverse proxy cho lẹ….
Cài đặt nginx
Step 1: Chạy Nginx 1.16.0 yêu cầu một số gói từ kho lưu trữ. Cài đặt bằng lệnh này:
yum install \
gcc \
zlib-devel \
openssl-devel \
make \
pcre-devel \
libxml2-devel \
libxslt-devel \
libgcrypt-devel \
gd-devel \
perl-ExtUtils-Embed \
GeoIP-devel

Step 2: download nginx ở github. Này mình chọn version 1.16
curl -L https://github.com/nginx/nginx/archive/release-1.16.0.tar.gz > nginx-release-1.16.0.tar.gz

tar xf nginx-release-1.16.0.tar.gz


Step 3: Auto configure nginx
Tiếp tục cd vào thư mục vừa giải nén. Thực thi lệnh dưới đây, bạn có thể custom user/group của nginx cũng như đường dẫn sẽ cài nginx
auto/configure \
--with-pcre \
--prefix=/u01/app/nginx/nginx-release-1.16.0/nginx \
--user=root \
--group=root \
--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 \
--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 \
--without-http_charset_module \
--with-http_perl_module \
--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

Step 4: Thực thi tuần tự 2 câu lệnh make và make install

Sau khi chạy xong thì nó sẽ tự generate ra folder nginx như hình, folder nằm dựa trên đường dẫn bạn đã cấu hình ở lệnh auto/configure bên trên

Cấu hình với systemd
Để tạo file service, thực thi tuần tự theo các lệnh sau:
cd /etc/systemd/system
vi nginx.service
[Unit]
Description=nginx 1.16.0
After=syslog.target network.target
[Service]
Type=forking
EnvironmentFile=/u01/app/nginx/sysconfig/nginx-1.16.0
ExecStart=/u01/app/nginx/nginx-release-1.16.0/nginx/sbin/nginx $CLI_OPTIONS
ExecReload=/u01/app/nginx/nginx-release-1.16.0/nginx/sbin/nginx -s reload
ExecStop=/u01/app/nginx/nginx-release-1.16.0/nginx/sbin/nginx -s quit
[Install]
WantedBy=multi-user.target
cd /u01/app/nginx
mkdir sysconfig
vi nginx-1.66.0
# Command line options to use when starting nginx
#CLI_OPTIONS=""
systemctl daemon-reload
systemctl start nginx


Cài đặt Sites-Available
Để tiện hơn cho việc cấu hình các domain,… thay vì các bạn dùng chung nginx.conf thì thường ta sẽ setup thêm các conf
Step 1: Cấu hình sites-available, enabled
cd vào /u01/app/nginx/nginx-release-1.16.0/nginx
mkdir sites-available
mkdir sites-enabled
Thêm include /u01/app/nginx/nginx-release-1.16.0/nginx/sites-enabled/*; vào file /config/nginx.conf

cd sites-available
vi example.conf
server { listen 80; root /u01/app/html; index index.html index.htm index.nginx.debian.html; location / { try_files $uri $uri/ =404; } }
Step 2: Tạo symlink
ln -s /u01/app/nginx/nginx-release-1.16.0/nginx/sites-available/example.conf /u01/app/nginx/nginx-release-1.16.0/nginx/sites-enabled/
Step 3: Làm mẫu một file html để test
cd /u01/app
mkdir html
vi index.html => thêm vào nội dung test site-available
systemctl restart nginx

Chúc mọi người thành công!!!