Linux中安装Nginx很详细一、先到官网下载Nginx二、上传到服务器然后解压1、上传到指定的服务器地址2、解压三、初始化Nginx1、进行configure2、假如进行configure时报错1、执行命令 yum -y install pcre-devel2、执行命令 yum -y install openssl openssl-devel3、执行命令 yum install -y zlib zlib-devel 2026-7-14更新3、安装ssl2025-7-21更新4、进行make四、查看Nginx是否成功安装五、Nginx的一些常用命令1、启动Nginx2、关闭Nginx3、Nginx重启六、使用系统命令操作nginx1、弄清楚操作nginx文件的位置2、移动nginx配置文件的位置非必要1、移动到目标目录2、备份配置文件3、检查配置文件有没有错误4、杀掉所有nginx进程3、编写系统命令相关脚本4、让脚本文件生效1、上传文件2、赋予文件权限3、增加服务4、增加服务报错2026-4-20更新5、使用系统命令操作nginx1、启动nginx2、停止nginx3、重启nginx4、查看nginx状态一、先到官网下载Nginx官网地址http://nginx.org/en/download.html我下载的是最新稳定版二、上传到服务器然后解压1、上传到指定的服务器地址上传的地址自己决定我上传到 /usr/Nginx。2、解压使用命令 tar -zxvf “你的Nginx压缩包”我这里是tar-zxvf nginx-1.22.1.tar.gz等待解压结束。三、初始化Nginx1、进行configure进入Nginx的根目录执行命令./configure2、假如进行configure时报错有些朋友在进行configure时可能会报错我的也报错了。1、执行命令 yum -y install pcre-devel可以使用命令yum-y install pcre-devel然后再进行configure。2、执行命令 yum -y install openssl openssl-devel如果还报错可能你需要安装ssl相关的模块这里的命令是安装ssl的前置条件可以执行命令yum-y install openssl openssl-devel再进行configure应该不会报错了。太长了中间没截图。3、执行命令 yum install -y zlib zlib-devel 2026-7-14更新如果报如下错./configure: error: the HTTPgzipmodule requires the zlib library. You can either disable the module by using --without-http_gzip_module option, orinstallthe zlib library into the system, or build the zlib library statically from thesourcewith nginx by using --with-zlibpathoption.则需要执行如下命令yuminstall-yzlib zlib-devel3、安装ssl2025-7-21更新如果后面要配置ssl证书那就需要安装ssl模块如果不需要可以跳过./configure--prefix/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module中间太长就跳过了4、进行make执行命令make需要点时间然后执行命令make install四、查看Nginx是否成功安装执行命令whereis nginx出现Nginx的目录表示成功安装。五、Nginx的一些常用命令以下命令都是在进入安装Nginx的sbin目录中操作我这里是 /usr/local/nginx/sbin1、启动Nginx执行命令./nginx然后在浏览器中使用ip地址访问表示Nginx成功启动。也可以使用命令查看命令如下ps-ef|grep nginx2、关闭Nginx执行命令./nginx-s stop使用之后可以看到Nginx进程以及消失了。3、Nginx重启命令如下./nginx-s reload执行不报错就表示重启成功。这个命令相对用的多些修改了Nginx配置之后就需要重启为了在任何地方可以修改和配置我一般这样使用进入修改Nginx配置vim/usr/local/nginx/conf/nginx.conf重启Nginx/usr/local/nginx/sbin/nginx-s reload六、使用系统命令操作nginx通过以上步骤安装好nginx使用系统命令操作nginx可能会报错不同版本的Linux系统命令不一样我的是Centos7.5.1804所以是systemctl比如重启的时候1、弄清楚操作nginx文件的位置上面安装好后就是在 /usr/local/nginx/sbin 这个目录保持这样就好。2、移动nginx配置文件的位置非必要上面安装好后就是在 /usr/local/nginx/conf 这个目录1、移动到目标目录现在我想把它移动到 /etc/nginx 这个目录下先创建目录mkdir-p/etc/nginx再移动到这个目录cp/usr/local/nginx/conf/nginx.conf/etc/nginx/nginx.conf2、备份配置文件记得做一下备份cpnginx.conf nginx_bk.conf3、检查配置文件有没有错误最好看一下nginx.conf配置文件有没有错误/usr/local/nginx/sbin/nginx-c/etc/nginx/nginx.conf这样就表示配置文件没有问题了。如果报了如下的错这是把nginx的配置文件原原本本地搬了过来解决的方式有两种第一种是注释掉对应的引入第二种是在 /etc/nginx 目录加入一个名为 mime.types 的文件这是文件内容types{text/html html htm shtml;text/css css;text/xml xml;image/gif gif;image/jpeg jpeg jpg;application/javascript js;application/atomxml atom;application/rssxml rss;text/mathml mml;text/plain txt;text/vnd.sun.j2me.app-descriptor jad;text/vnd.wap.wml wml;text/x-component htc;image/avif avif;image/png png;image/svgxml svg svgz;image/tiff tif tiff;image/vnd.wap.wbmp wbmp;image/webp webp;image/x-icon ico;image/x-jng jng;image/x-ms-bmp bmp;font/woff woff;font/woff2 woff2;application/java-archive jar war ear;application/json json;application/mac-binhex40 hqx;application/msword doc;application/pdf pdf;application/postscriptpseps ai;application/rtf rtf;application/vnd.apple.mpegurl m3u8;application/vnd.google-earth.kmlxml kml;application/vnd.google-earth.kmz kmz;application/vnd.ms-excel xls;application/vnd.ms-fontobject eot;application/vnd.ms-powerpoint ppt;application/vnd.oasis.opendocument.graphics odg;application/vnd.oasis.opendocument.presentation odp;application/vnd.oasis.opendocument.spreadsheet ods;application/vnd.oasis.opendocument.text odt;application/vnd.openxmlformats-officedocument.presentationml.presentation pptx;application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx;application/vnd.openxmlformats-officedocument.wordprocessingml.document docx;application/vnd.wap.wmlc wmlc;application/wasm wasm;application/x-7z-compressed 7z;application/x-cocoa cco;application/x-java-archive-diffjardiff;application/x-java-jnlp-file jnlp;application/x-makeself run;application/x-perl pl pm;application/x-pilot prc pdb;application/x-rar-compressed rar;application/x-redhat-package-manager rpm;application/x-sea sea;application/x-shockwave-flash swf;application/x-stuffit sit;application/x-tcl tcl tk;application/x-x509-ca-cert der pem crt;application/x-xpinstall xpi;application/xhtmlxml xhtml;application/xspfxml xspf;application/zip zip;application/octet-stream bin exe dll;application/octet-stream deb;application/octet-stream dmg;application/octet-stream iso img;application/octet-stream msi msp msm;audio/midi mid midi kar;audio/mpeg mp3;audio/ogg ogg;audio/x-m4a m4a;audio/x-realaudio ra;video/3gpp 3gpp 3gp;video/mp2t ts;video/mp4 mp4;video/mpeg mpeg mpg;video/quicktime mov;video/webm webm;video/x-flv flv;video/x-m4v m4v;video/x-mng mng;video/x-ms-asf asx asf;video/x-ms-wmv wmv;video/x-msvideo avi;}4、杀掉所有nginx进程为避免影响到后续步骤先杀掉所有nginx进程killall nginx查看是否还有nginx相关的进程netstat-nap|grep nginx3、编写系统命令相关脚本创建名为 nginx 的文件注意没有后缀名如果不知道可以在Linux中的 /etc/rc.d/init.d 目录下复制一份进行编辑修改我拿的是这个具体如下#!/bin/sh# nginx - this script starts and stops the nginx daemin## chkconfig: - 85 15# description: Nginx is an HTTP(S) server, HTTP(S) reverse \# proxy and IMAP/POP3 proxy server# processname: nginx# config: /usr/local/nginx/conf/nginx.conf# 定义pid文件的位置需要和nginx.conf中的pid保持一致不然使用命令时会出现卡死但命令已经生效的现象pidfile:/var/run/nginx.pid# Source function library../etc/rc.d/init.d/functions# Source networking configuration../etc/sysconfig/network# Check that networking is up.[$NETWORKINGno]exit0# 启动nginx的命令根据实际情况修改nginx/usr/local/nginx/sbin/nginxprog$(basename$nginx)# nginx.conf文件的位置根据实际情况修改NGINX_CONF_FILE/etc/nginx/nginx.conflockfile/var/lock/subsys/nginxstart(){[-x$nginx]||exit5[-f$NGINX_CONF_FILE]||exit6echo-n $Starting$prog: daemon$nginx-c$NGINX_CONF_FILEretval$?echo[$retval-eq0] touch$lockfilereturn$retval}stop(){echo-n $Stopping$prog: killproc$prog-QUIT retval$?echo[$retval-eq0]rm-f$lockfilereturn$retval}restart(){configtest||return$? stopstart}reload(){configtest||return$?echo-n $Reloading$prog: killproc$nginx-HUP RETVAL$?echo}force_reload(){restart}configtest(){$nginx-t-c$NGINX_CONF_FILE}rh_status(){status$prog}rh_status_q(){rh_status /dev/null 21}case$1instart)rh_status_q exit0$1;;stop)rh_status_q||exit0$1;;restart|configtest)$1;;reload)rh_status_q||exit7$1;;force-reload)force_reload;;status)rh_status;;condrestart|try-restart)rh_status_q||exit0;;*)echo$Usage:$0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}exit2 esac主要改这三处根据自己的实际情况修改其中第一处和 nginx.conf 中的pid保持一致没有保持一致会出现卡死的感觉像这样其实命令已经生效了虽然可以 Ctrl C 退出但体验不太好。其他可以不改。4、让脚本文件生效1、上传文件将文件上传到 /etc/rc.d/init.d 目录2、赋予文件权限然后进入改目录cd/etc/init.d给脚本文件对应的权限chmod 755/etc/init.d/nginx解释755表示所有者具有读、写和执行权限而组和其他人只有读和执行权限。具体来说755中第一位7代表所有者owner具有读、写、执行权限第二位5代表组group只有读和执行权限第三位5代表其它人others也只有读和执行权限。原文链接https://www.linuxcool.com/zwlmlcdhyqss3、增加服务增加一项系统服务chkconfig--add nginx此时如果没有报错就可以看下一节了。4、增加服务报错2026-4-20更新如果是新系统可能添加之后启动还是找不到对应的服务这是因为chkconfig 是老系统用的现在 CentOS 7、Ubuntu、Debian 全都用 systemd没有 nginx.service 文件 → 必然报 Unit not found启动systemctlstartnginx可能会报如下错Failed tostartnginx.service: Unit nginx.service not found.查看系统版本cat/etc/os-release我现在的系统是阿里云 Linux 4Anolis 龙蜥CentOS8 同款 systemdchkconfig 已经淘汰不用了所以 chkconfig --add nginx 完全没用才一直报错 Unit not found这时候应该使用如下命令cat/usr/lib/systemd/system/nginx.service EOF[Unit]Descriptionnginx Afternetwork.target[Service]Typeforking PIDFile/usr/local/nginx/logs/nginx.pid ExecStart/usr/local/nginx/sbin/nginx ExecReload/usr/local/nginx/sbin/nginx-s reload ExecStop/usr/local/nginx/sbin/nginx-s quit PrivateTmptrue[Install]WantedBymulti-user.target EOF这是使用默认位置的配置文件因为我把配置文件放到了 /etc/nginx 目录下所以应该是指定目录cat/usr/lib/systemd/system/nginx.service EOF[Unit]Descriptionnginx Afternetwork.target[Service]Typeforking PIDFile/usr/local/nginx/logs/nginx.pid ExecStart/usr/local/nginx/sbin/nginx-c/etc/nginx/nginx.conf ExecReload/usr/local/nginx/sbin/nginx-s reload ExecStop/usr/local/nginx/sbin/nginx-s quit PrivateTmptrue[Install]WantedBymulti-user.target EOF刷新服务systemctl daemon-reload再次启动systemctlstartnginx5、使用系统命令操作nginx1、启动nginxsystemctlstartnginx2、停止nginxsystemctl stop nginx3、重启nginxsystemctl restart nginx4、查看nginx状态systemctl status nginx