Ubuntu上配置nginx及相关命令

一、安装nginx

在Ubuntu下安装Nginx有以下方法,但是如果想要安装最新版本的就必须下载源码包编译安装。

1、基于APT源安装:

sudo apt-get install nginx

安装好的文件位置:
/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
/usr/share/nginx:存放静态文件
/var/log/nginx:存放日志

其实从上面的根目录文件夹可以知道,Linux系统的配置文件一般放在/etc,日志一般放在/var/log,运行的程序一般放在/usr/sbin或者/usr/bin。

当然,如果要更清楚Nginx的配置项放在什么地方,可以打开/etc/nginx/nginx.conf

Nginx指定默认加载/etc/nginx/nginx.conf的配置文件。如果要查看加载的是哪个配置文件,可以用这个命令sudo nginx -t或者ps -ef | grep nginx

然后通过这种方式安装的,会自动创建服务,会自动在/etc/init.d/nginx新建服务脚本,然后就可以使用sudo service nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}的命令启动。

创建好的文件由于放在/usr/sbin目录下,所以能直接在终端中使用nginx命令而无需指定路径。

通过源码包编译安装参考:http://www.cnblogs.com/piscesLoveCc/p/5794926.html

二、相关命令

启动命令:sudo service nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}

查看版本:sudo nginx -v

检查配置文件:sudo nginx -t

查看端口被占用情况:netstat -apn

更精确的查找:netstat -apn | grep 80

检查是否已经安装有nginx及对应目录:root@localhost:/# find /|grep nginx.conf
                                                /etc/nginx/conf.d
                                                /etc/nginx/nginx.conf
                                                /etc/init/nginx.conf
                                                /root/oneinstack/config/nginx.conf
还可以用以下两个命令,找安装的路径
root@localhost:/# netstat -tnlp|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      980/nginx -g daemon
tcp6       0      0 :::80                   :::*                    LISTEN      980/nginx -g daemon
然后看到一行记录,复制最后的一个数据(进程ID)
ps -aux |grep 进程ID
就可以看到nginx的启动方式了。
root@localhost:/# ps -aux |grep 980
root       980  0.0  0.0 125096  1420 ?        Ss   12:19   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
root      1237  0.0  0.0  14220   940 pts/0    S+   13:02   0:00 grep --color=auto 980

注:reload,重新加载的意思,reload会重新加载配置文件,nginx服务不会中断,而且reload时会测试conf语法等,如果出错会rollback用上一次正确配置文件保持正常运行。
restart,重启,会重启nginx服务。这个重启会造成服务一瞬间的中断,当然如果配置文件出错会导致服务启动失败,那就是更长时间的服务中断了。

猜你喜欢

转载自blog.csdn.net/voilet_bin/article/details/83895882