加载依赖包:
sudo apt-get update
sudo apt-get install libxml2-dev
sudo apt-get install build-essential
sudo apt-get install openssl
sudo apt-get install libssl-dev
sudo apt-get install make
sudo apt-get install curl
sudo apt-get install libcurl4-gnutls-dev
sudo apt-get install libjpeg-dev
sudo apt-get install libpng-dev
sudo apt-get install libtool-bin
sudo apt-get install bison
sudo apt-get install zlib1g-dev libpcre3 libpcre3-dev libssl-dev libxslt1-dev libgeoip-dev libgoogle-perftools-dev libperl-dev libtool
sudo apt-get install libxml2-dev build-essential openssl libssl-dev make curl libcurl4-gnutls-dev libjpeg-dev libpng-dev libtool-bin bison zlib1g-dev libpcre3 libpcre3-dev libssl-dev libxslt1-dev libgeoip-dev libgoogle-perftools-dev libperl-dev libtool
编译安装nginx:
1、下载新版本,到官网复制下载链接
wget http://nginx.org/download/nginx-1.17.4.tar.gz
2、解压tar -zxvf nginx-1.17.4.tar.gz
3、编译安装
# 进入解压目录:
cd nginx-1.17.4.tar.gz
# 配置并编译安装nginx:
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-mail=dynamic
sudo make
sudo make install
# 启动nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,可以通过 -h查看帮助命令。
# 查看nginx进程:
ps -ef|grep nginx
# 建立软链接(由于/usr/local/bin包含于$PATH当中,这样就不需要额外的设置环境变量了,这意味着可以在其他路径下直接运行nginx命令)
#(创建链接)
sudo ln -s /opt/dotnet/dotnet /usr/local/bin
编译选项说明:
--prefix=path 如果在编译的不指定安装位置,那么默认的位置/usr/local/nginx目录
--sbin-path=path 设置nginx执行脚本的位置,这里如果设置在path变量里面,就可以在bash环境下,任意使用nginx命令,默认位置prefix/sbin/nginx 注意这里的prefix是在配置文件里面配置的路径
--conf-path=path 配置nginx配置文件的路径,如果不指定这个选项,那么配置文件的默认路径就会是 prefix/conf/nginx.conf
--pid-path =path 配置nginx.pid file的路径,一般来说,进程在运行的时候的时候有一个进程id,这个id会保存在pid file里面,默认的pid file的放置位置是prefix/logs/nginx.pid
--error-log-path=path 设置错误日志的存放路径,如果不指定,就默认 prefix/logs/error.log
--http-log-path= path 设置http访问日志的路径,如果不指定,就默认 prefix/logs/access.log
--user=name 设置默认启动进程的用户,如果不指定,就默认 nobody
--group=name 设置这个用户所在的用户组,如果不指定,依然是nobody
--with-http_ssl_module 开启HTTP SSL模块,使NGINX可以支持HTTPS请求。需要安装了OPENSSL
--with-http_flv_module
--with-http_stub_status_module 启用 "server status" 页
--without-http_gzip_module 禁用 ngx_http_gzip_module. 如果启用,需要 zlib
--without-http_ssi_module 禁用 ngx_http_ssi_module
--without-http_referer_module 禁用 ngx_http_referer_module
--without-http_rewrite_module 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。
--without-http_proxy_module 禁用 ngx_http_proxy_module
--without-http_fastcgi_module 禁用 ngx_http_fastcgi_module
--without-http_memcached_module 禁用 ngx_http_memcached_module
--without-http_browser_module 禁用 ngx_http_browser_module
--http-proxy-temp-path=PATH 设置路径到the http proxy temporary files
--http-fastcgi-temp-path=PATH 设置路径到Set path to the http fastcgi temporary files
--without-http 禁用 HTTP server
--with-mail 启用 IMAP4/POP3/SMTP 代理模块
--with-mail_ssl_module 启用ngx_mail_ssl_module
--with-openssl=DIR 设置路径到OpenSSL library sources
--with-stream 用来实现四层协议的转发、代理或者负载均衡等
自动启动nginx:
编译安装需要自己进行设置方可自动启动
# 设置nginx自启动,在/lib/systemd/system/ 目录下创建一个服务文件
vim /lib/systemd/system/nginx.service
内容如下:
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
文件说明
[Unit]部分
Description:描述服务
After:依赖,当依赖的服务启动之后再启动自定义的服务
[Service]部分
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令(需要根据路径适配)
ExecReload为重启命令(需要根据路径适配)
ExecStop为停止命令(需要根据路径适配)
PrivateTmp=True表示给服务分配独立的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径
[Install]部分
服务安装的相关设置,可设置为多用户
# 设置了自启动后,任意目录下执行
systemctl enable nginx.service
# 启动nginx服务
systemctl start nginx.service
# 设置开机自动启动
systemctl enable nginx.service
# 停止开机自动启动
systemctl disable nginx.service
# 查看状态
systemctl status nginx.service
# 重启服务
systemctl restart nginx.service
# 查看所有服务
systemctl list-units --type=service