在ubuntu系统中从源码安装nginx可以让您自定义nginx的编译选项和模块,以满足特定需求。
以下是详细的步骤指南:
前提条件
更新系统包列表
sudo apt update sudo apt upgrade -y
安装必要的依赖包
sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev wget
- build-essential: 包含编译工具如gcc、make等。
- libpcre3 & libpcre3-dev: 用于正则表达式支持。
- zlib1g & zlib1g-dev: 用于压缩功能。
- libssl-dev: 提供ssl支持。
- wget: 用于下载源码。
步骤一:下载nginx源码
访问nginx官方主页,获取最新的稳定版本下载链接。
使用wget下载源码包(以nginx 1.24.0为例,请根据最新版本替换):
cd /usr/local/src sudo wget http://nginx.org/download/nginx-1.24.0.tar.gz
解压源码包
sudo tar -zxvf nginx-1.24.0.tar.gz cd nginx-1.24.0
步骤二:配置编译选项
配置编译选项可以自定义nginx的功能和模块。以下是一个常用的配置示例:
sudo ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-pcre --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module
说明:
--sbin-path: 指定nginx可执行文件的安装路径。--conf-path: 指定nginx配置文件的位置。--error-log-path&--http-log-path: 指定日志文件的位置。--with-pcre: 启用pcre支持(正则表达式)。--with-http_ssl_module: 启用ssl模块。--with-http_v2_module: 启用http/2支持。--with-http_gzip_static_module: 启用gzip静态压缩模块。--with-stream&--with-stream_ssl_module: 启用tcp/udp流支持及其ssl模块。
您可以根据需求添加或删除配置选项。运行./configure --help可以查看所有可用的配置选项。
步骤三:编译和安装
编译源码
sudo make
编译过程可能需要几分钟,具体时间取决于系统性能。
安装nginx
sudo make install
默认情况下,nginx将被安装到之前指定的路径(如 /usr/local/nginx/)。
步骤四:创建nginx用户和目录
创建一个专用用户运行nginx
sudo useradd -r -s /sbin/nologin nginx
设置正确的权限
sudo chown -r nginx:nginx /usr/local/nginx
步骤五:配置nginx为系统服务
为了方便管理nginx,建议将其配置为systemd服务。
创建systemd服务文件
sudo nano /etc/systemd/system/nginx.service
在文件中添加以下内容
[unit] description=the nginx http and reverse proxy server after=network.target [service] type=forking pidfile=/usr/local/nginx/logs/nginx.pid execstartpre=/usr/local/nginx/sbin/nginx -t execstart=/usr/local/nginx/sbin/nginx execreload=/usr/local/nginx/sbin/nginx -s reload execstop=/usr/local/nginx/sbin/nginx -s quit user=nginx group=nginx [install] wantedby=multi-user.target
重新加载systemd守护进程
sudo systemctl daemon-reload
启动nginx服务
sudo systemctl start nginx
设置开机自启动
sudo systemctl enable nginx
检查nginx状态
sudo systemctl status nginx
您应该看到nginx正在运行的状态。
步骤六:配置防火墙
确保防火墙允许http和https流量。
如果使用ufw防火墙
sudo ufw allow 'nginx full'
重新加载防火墙规则
sudo ufw reload
步骤七:验证安装
访问nginx默认页面
打开浏览器,访问服务器的ip地址(例如 http://your_server_ip/),您应该看到nginx的默认欢迎页面。
检查nginx版本
/usr/local/nginx/sbin/nginx -v
输出类似于:
nginx version: nginx/1.24.0
步骤八:管理nginx
启动nginx
sudo systemctl start nginx
停止nginx
sudo systemctl stop nginx
重启nginx
sudo systemctl restart nginx
重新加载配置
sudo systemctl reload nginx
附加步骤:配置nginx
nginx的主配置文件位于 /usr/local/nginx/conf/nginx.conf。
您可以根据需要编辑该文件进行进一步配置。
sudo nano /usr/local/nginx/conf/nginx.conf
编辑完成后,重新加载nginx以应用更改:
sudo systemctl reload nginx
注意事项
更新nginx
由于是源码安装,nginx不会自动更新。要更新nginx,需要手动下载新版本源码,编译并安装,或使用包管理工具。
卸载nginx
源码安装的nginx通常位于 /usr/local/nginx/。要卸载,只需删除该目录:
sudo rm -rf /usr/local/nginx/
以及删除systemd服务文件:
sudo rm /etc/systemd/system/nginx.service sudo systemctl daemon-reload
日志管理
确保定期轮转nginx日志以防止日志文件过大。可以使用 logrotate 工具进行配置。
总结
通过以上步骤,您已经成功在ubuntu系统中从源码编译并安装了nginx。源码安装提供了更高的灵活性,使您能够根据具体需求自定义nginx的功能。请确保在生产环境中仔细测试配置,并定期维护nginx以保持系统安全和稳定。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论