本文介绍了手动编译安装 nginx 的具体步骤。
RedHat 系
$ yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel
Debian 系
$ sudo apt install libpcre3 libpcre3-dev libssl-dev zlib1g-dev zlib1g
$ ./configure --prefix=/etc/nginx \
--sbin-path=/etc/nginx/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--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_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-http_slice_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module
checking for OS Linux 3.10.0-327.36.1.el7.x86_64 x86_64 checking for C compiler … not found ./configure: error: C compiler cc is not found
$ yum install -y gcc gcc-c++
./configure: error: the HTTP rewrite module requires the PCRE library.You can either disable the module by using –without-http_rewrite_moduleoption, or install the PCRE library into the system, or build the PCRE librarystatically from the source with nginx by using –with-pcre=option.
$ yum install pcre-devel
$ sudo apt install libpcre3 libpcre3-dev
./configure: error: SSL modules require the OpenSSL library.You can either do not enable the modules, or install the OpenSSL libraryinto the system, or build the OpenSSL library statically from the sourcewith nginx by using –with-openssl=option.
$ yum install openssl-devel
$ sudo apt install libssl-dev
./configure: error: the HTTP gzip module requires the zlib library.
$ yum install zlib-devel
$ sudo apt install zlib1g-dev zlib1g
$ make
$ groupadd -r nginx \
&& useradd -r -g nginx -s /bin/false -M nginx
$ sudo make install
编辑 ~/.bash_profile
export PATH=/etc/nginx/sbin:$PATH
编辑 /etc/sudoers
注意:必须编辑此文件,否则 sudo 会找不到命令。
Defaults secure_path="/etc/nginx/sbin:..."
在 /etc/systemd/system/
下增加 nginx.service
文件,以下路径根据实际自己修改。
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/etc/nginx/run/nginx.pid
ExecStartPre=/etc/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/etc/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
$ sudo nginx -t
$ sudo nginx