Linux服务器 nginx安装、nginx启动、重启常用命令

安装依赖

1yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-devel 2 3

下载nginx稳定版

1#进入/usr/local目录 2cd /usr/local 3#创建nginx文件夹 4mkdir nginx 5#进入/usr/local/nginx的目录 6cd /usr/local/nginx 7#解压下载好的压缩包 8wget http://nginx.org/download/nginx-1.16.1.tar.gz 9 10

解压–编译–安装

1tar -zxvf nginx-1.16.1.tar.gz 2 3cd nginx-1.16.1/ 4#配置vim,使其语法高亮 5mkdir .vim #自己创建一个.vim目录 6cp -r contrib/vim/* ~/.vim/ 7 8#查看编译参数 9./configure --help | more 10 11#编译 12./configure --prefix=/usr/local/nginx 13 14#安装: 15make && make install 16 17 18#去prefix指定的目录下 19cd /usr/local/nginx/ 20 21ls -l 22total 16 23drwxr-xr-x 2 root root 4096 Jul 9 13:37 conf 24drwxr-xr-x 2 root root 4096 Jul 9 13:37 html 25drwxr-xr-x 2 root root 4096 Jul 9 13:37 logs 26drwxr-xr-x 2 root root 4096 Jul 9 13:37 sbin 27 28 29#nginx服务器的主程序 30ls /usr/local/nginx/sbin/nginx 31/usr/local/nginx/sbin/nginx 32 33

开放linux对外访问的端口号80

1/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT 2 3

如果Centos7默认安装了firewalld
则使用如下命令

1firewall-cmd --add-port=端口号/tcp --zone=public --permanent 添加某端口号放行 2 3

开启后重启防火墙,使得开放端口生效

1firewall-cmd --reload 或者 service firewalld restart 2 3

nginx服务的启动操作

11: 默认配置文件启动 2cd /usr/local/nginx/sbin 3./nginx 4 5指定配置文件启动 6./nginx -c /usr/local/nginx/conf/nginx.conf 7 82: 查看帮助信息 9./ngxin =h #查看帮助信息 10 11可以找安装时候的配置 12./nginx -V 13nginx version: nginx/1.16.1 14built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 15configure arguments: --prefix=/usr/local/nginx 16 17 183:-t 检查配置文件是否正确 19./nginx -t 20nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 21nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 22 23

验证

1http://阿里云公网ip/ 2 3

js.png

nginx停服务

1cd /usr/local/nginx/sbin 2./nginx -s stop 3 4

nginx常用命令

1### 启动1: 2/usr/local/nginx/sbin/nginx 3 4### 启动2: 5 6cd /usr/local/nginx/sbin 7./nginx 8### 启动3: 9/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 10### 重新载入配置 11/usr/local/nginx/sbin/nginx -s reload 12### 重启 13/usr/local/nginx/sbin/nginx -s reopen 14### 关闭进程 15/usr/local/nginx/sbin/nginx -s stop 16### 平滑关闭nginx 17/usr/local/nginx/sbin/nginx -s quit 18### 查看nginx版本 19/usr/local/nginx/sbin/nginx -V 20### 检查配置文件nginx.conf的正确性 21/usr/local/nginx/sbin/nginx -t 22 23### 指定配置文件重启 24/usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf 25### 如果出现如下问题 26nginx启动提示nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 27这是端口80被占用了 28用以下命令查看端口被谁占用 29lsof -i :80 30可以杀掉指定线程 31再进行重启 32/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 33`` 34 35 36

代码交流 2021