Cài đặt Nginx từ source – Ubuntu server

bởi | LPI, Ubuntu

Home » LPI » Cài đặt Nginx từ source – Ubuntu server

+ Cài đặt thư viện cần thiết
sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libgd-dev libxml2 libxml2-dev uuid-dev
apt-get install libxml2-dev libxslt-dev python-dev libgeoip-dev

+ Download và giải nén source, thư viện cần thiết

[root@thanhdd ~]# wget https://nginx.org/download/nginx-1.21.6.tar.gz

[root@thanhdd ~]# wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz

[root@thanhdd ~]# wget https://www.zlib.net/zlib-1.2.12.tar.gz
[root@thanhdd ~]# wget https://www.openssl.org/source/openssl-1.1.1o.tar.gz

[root@thanhdd ~]# tar xf pcre-8.45.tar.gz
[root@thanhdd ~]# tar xf zlib-1.2.12.tar.gz
[root@thanhdd ~]# tar xf openssl-1.1.1o.tar.gz
[root@thanhdd ~]# tar xf nginx-1.21.6.tar.gz

+ Thực hiện tạo file cấu hình và biên dịch chương trình mã nguồn

[root@thanhdd ~]# cd nginx-1.21.6
[root@thanhdd nginx-1.21.6]# sudo cp man/nginx.8 /usr/share/man/man8
[root@thanhdd nginx-1.21.6]# sudo gzip /usr/share/man/man8/nginx.8
[root@thanhdd nginx-1.21.6]# sudo man nginx

[root@thanhdd nginx-1.19.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=Ubuntu \
–builddir=nginx-1.21.6 \
–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.45 \
–with-pcre-jit \
–with-zlib=../zlib-1.2.12 \
–with-openssl=../openssl-1.1.1o \
–with-openssl-opt=no-nextprotoneg \
–with-debug

[root@thanhdd nginx-1.19.2]# make
[root@thanhdd nginx-1.19.2]# make install

[root@thanhdd ~]# ln -s /usr/lib64/nginx/modules /etc/nginx/modules
[root@thanhdd ~]# cd /etc/nginx/modules
[root@thanhdd modules]# ls
ngx_http_geoip_module.so ngx_http_xslt_filter_module.so ngx_stream_geoip_module.so
ngx_http_image_filter_module.so ngx_mail_module.so ngx_stream_module.so

[root@thanhdd ~]# useradd –system –home /var/cache/nginx –shell /sbin/nologin –comment “nginx user” –user-group nginx

[root@thanhdd ~]# 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

[root@thanhdd ~]# mkdir -p /var/cache/nginx/client_temp
[root@thanhdd ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

– Tạo ra scripts để khởi động dịch vụ Nginx

[root@thanhdd ~]# vi /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

+ Kiểm tra và start dịch vụ
[root@srv nginx]# systemctl daemon-reload
[root@thanhdd ~]# systemctl start nginx
[root@thanhdd ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.