在ubuntu上使用源码安装nginx

186 阅读3分钟

在ubuntu上使用源码安装nginx

下载源码

wget https://nginx.org/download/nginx-1.27.3.tar.gz

解压源码

tar -zxvf nginx-1.27.3.tar.gz

编译安装

使用nginx开启brotli模块

  1. 下载brotli模块源码

把模块安装到nginx源码的modules目录下

git clone --recurse-submodules -j8 https://github.com/google/ngx_brotli
cd ngx_brotli/deps/brotli
mkdir out && cd out
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_CXX_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_INSTALL_PREFIX=./installed ..
cmake --build . --config Release --target brotlienc
cd nginx-1.27.3
export CFLAGS="-m64 -march=native -mtune=native -Ofast -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections"
export LDFLAGS="-m64 -Wl,-s -Wl,-Bsymbolic -Wl,--gc-sections"
./configure --add-module=/home/nginx/nginx-1.27.3/modules/ngx_brotli
make && make install

make成功之后

  • nginx path prefix: "/usr/local/nginx"

    • 这是Nginx安装的根目录。所有Nginx的文件和子目录都被安装在这里。
  • nginx binary file: "/usr/local/nginx/sbin/nginx"

    • 这是Nginx的二进制执行文件的路径。您可以通过运行这个文件来启动或停止Nginx服务。
  • nginx modules path: "/usr/local/nginx/modules"

    • 这个目录包含了所有Nginx模块。模块是Nginx功能扩展的组件,可以在这里找到。
  • nginx configuration prefix: "/usr/local/nginx/conf"

    • 这是Nginx配置文件所在的目录。通常,nginx.conf文件位于这个目录下,它是Nginx的主配置文件。
  • nginx configuration file: "/usr/local/nginx/conf/nginx.conf"

    • 这是Nginx的主配置文件的路径。所有的Nginx配置都从这个文件开始。
  • nginx pid file: "/usr/local/nginx/logs/nginx.pid"

    • 这是Nginx进程ID(PID)文件的路径。这个文件存储了当前运行的Nginx主进程的PID。
  • nginx error log file: "/usr/local/nginx/logs/error.log"

    • 这是Nginx错误日志文件的路径。所有的错误信息都会被记录在这个文件中。
  • nginx http access log file: "/usr/local/nginx/logs/access.log"

    • 这是Nginx访问日志文件的路径。所有的HTTP请求都会被记录在这个文件中。
  • nginx http client request body temporary files: "client_body_temp"

    • 这是客户端请求体临时文件的目录。Nginx在处理请求时,会将请求体内容暂存于此。
  • nginx http proxy temporary files: "proxy_temp"

    • 这是代理临时文件的目录。Nginx在作为代理服务器时,会将从后端服务器接收的数据暂存于此。
  • nginx http fastcgi temporary files: "fastcgi_temp"

    • 这是FastCGI临时文件的目录。Nginx在与FastCGI服务器通信时,会将数据暂存于此。
  • nginx http uwsgi temporary files: "uwsgi_temp"

    • 这是uWSGI临时文件的目录。Nginx在与uWSGI服务器通信时,会将数据暂存于此。
  • nginx http scgi temporary files: "scgi_temp"

    • 这是SCGI临时文件的目录。Nginx在与SCGI服务器通信时,会将数据暂存于此。

启动nginx

/usr/local/nginx/sbin/nginx

停止nginx

/usr/local/nginx/sbin/nginx -s stop

重启nginx

/usr/local/nginx/sbin/nginx -s reload

上面的比较麻烦,可以使用

export PATH=$PATH:/usr/local/nginx/sbin
source ~/.bashrc
nginx -v
nginx -t
nginx -s reload
  • 效果 效果

问题

  • 错误1
checking for OS
 + Linux 6.8.0-31-generic x86_64
checking for C compiler ... not found
​
./configure: error: C compiler cc is not found

解决方案:

sudo apt-get install build-essential
  • 错误2
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

解决方案:

sudo apt-get install  libpcre3 libpcre3-dev
  • 错误3
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

解决方案:

sudo apt-get install zlib1g-dev