Ubuntu服务器编译安装Nginx

张开发
2026/4/15 1:04:14 15 分钟阅读

分享文章

Ubuntu服务器编译安装Nginx
Ubuntu服务器部署Nginx1.切换到root权限sudosu2.安装依赖包apt-getupdate# 更新下apt源apt-getinstall-ygccmakelibz-dev libpcre3-dev libssl-dev3.进入下载目录例如/usr/src/ 源码包下载目录cd/usr/src/4.wget命令下载选择nginx版本的压缩包wgethttp://nginx.org/download/nginx-1.26.2.tar.gz5.tar解压tar-zxvfnginx-1.26.2.tar.gz6.进入解压的文件夹cd/usr/src/nginx-1.26.27.编辑编译内容./configure--prefix/usr/local/nginx-1.26\# 指定 Nginx 的安装目录--with-http_ssl_module\# 启用 HTTPS 模块--with-http_stub_status_module\# 启用 Stub Status 模块--with-http_gzip_static_module\# 启用 Gzip 静态模块--with-stream\# 启用 Stream 模块--with-http_realip_module\# 启用 Real IP 模块--with-http_flv_module\# 启用 FLV 模块--with-http_random_index_module\# 启用 Random Index 模块--with-mail\# 启用 Mail 模块--with-pcre\# 启用 PCRE--usernginx\# 指定 Nginx 用户--groupnginx\# 指定 Nginx 组--with-file-aio\# 启用文件异步 IO--with-http_rewrite_module\# 启用 Rewrite 模块1.26以上版本的nginx不用加8.编译安装make#执行编译生成Makefilemakeinstall#编译安装9.启动Nginxcd/usr/local/nginx-1.26.2/sbin#进入执行文件目录./nginx#启动nginx10.查看Nginx后台信息ps-ef|grepnginx返回以下信息↓root13730051016:00 ? 00:00:00 nginx: master process ./nginx nobody13730061373005016:00 ? 00:00:00 nginx: worker process root13730271358582016:00 pts/6 00:00:00grep--colorauto nginx11.测试连接curl http://localhost返回以下信息↓!DOCTYPEhtmlhtmlheadtitleWelcome to nginx!/titlestylehtml{color-scheme:light dark;}body{width:35em;margin:0 auto;font-family:Tahoma,Verdana,Arial,sans-serif;}/style/headbodyh1Welcome to nginx!/h1pIf you see this page, the nginx web server is successfully installed and working. Further configuration is required./ppFor online documentation and support please refer toahrefhttp://nginx.org/nginx.org/a.br/Commercial support is available atahrefhttp://nginx.com/nginx.com/a./ppemThank you for using nginx./em/p/body/html12.Nginx服务的操作./nginx #启动 ./nginx -s stop #关闭 ./nginx -s reload #重启通过以上步骤就完成了在Ubuntu系统下从源码成功安装并配置Nginx。13.配置Systemctl控制nginx创建一个名为nginx.service的文件通常放在/etc/systemd/system/目录下[Unit]DescriptionThe NGINX HTTP and reverse proxy serverAfternetwork.target[Service]TypeforkingExecStart/usr/local/nginx/sbin/nginx#启动nginx执行文件路径ExecReload/usr/local/nginx/sbin/nginx-sreload#启动nginx执行文件路径ExecStop/usr/local/nginx/sbin/nginx-squit#启动nginx执行文件路径PrivateTmptrue[Install]WantedBymulti-user.target设置开机自启动# 启用 Nginx 服务使其在系统启动时自动启动sudosystemctlenablenginx# 启动 Nginx 服务sudosystemctl start nginx# 检查 Nginx 服务的状态sudosystemctl status nginx# 停止 Nginx 服务sudosystemctl stop nginx# 禁用 Nginx 服务使其不会在系统启动时自动启动sudosystemctl disable nginx

更多文章