Linux环境Nginx的安装和配置

您所在的位置:网站首页 linux重启nginx服务命令 Linux环境Nginx的安装和配置

Linux环境Nginx的安装和配置

2023-03-21 15:01| 来源: 网络整理| 查看: 265

一、搭建环境

操作系统:Ubuntu 16.04

前提:开启root权限;如果没有,则所以在操作的时候需要使用sudo去获取一些执行权限。

二、安装编译器sudo apt-get install gcc sudo apt-get install g++ sudo apt-get install build-essential sudo apt-get install libtool

三、Nginx的安装

安装之前需要提前好Nginx的必备软件/库。

3.1、安装PCRE库

这里安装8.44版本。

wget https://sourceforge.net/projects/pcre/files/pcre/8.44/pcre-8.44.tar.gz tar -zxvf pcre-8.44.tar.gz cd pcre-8.44/ ./configure make sudo make install

3.2、安装zlib库

wget https://nchc.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz tar -zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11/ ./configure make sudo make install

3.3、安装OpenSSL开发库

wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz tar -zxvf openssl-1.1.1g.tar.gz cd openssl-1.1.1g/ ./config make sudo make install

3.4、安装Nginx

这里安装1.16版本。

wget http://nginx.org/download/nginx-1.16.1.tar.gz tar -zxvf nginx-1.16.1.tar.gz cd nginx-1.16.1/ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --withhttp_ssl_module --with-http_realip_module --with-http_v2_module --withopenssl=../openssl-1.1.1g make make install

四、Nginx的启动

默认情况下,Nginx被安装在目录/usr/local/nginx中。

cd usr/local/nginx ls

显示:

conf html logs sbin

其中Nginx的配置文件存放于conf/nginx.conf,bin文件是位于sbin目录下的nginx文件,logs是存放的启动日志、错误日志、运行日志等。

(1)默认方式启动Nginx服务器(需要sudo权限):

sudo /usr/local/nginx/sbin/nginx

这时,会自动读取配置文件:/usr/local/nginx/conf/nginx.conf

打开浏览器访问此机器的IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功:

nginx_http_show

(2)查看nginx进程:

sudo ps -ef|grep nginx

显示:

root 35768 1 0 11:12 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nobody 35769 35768 0 11:12 ? 00:00:00 nginx: worker process fly 35771 2396 0 11:12 pts/1 00:00:00 grep --color=auto nginx

注意,grep --color=auto nginx不是代表nginx启动,前面两行才是。

(3)指定配置文件启动服务器:

sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

(4)测试配置信息:

sudo /usr/local/nginx/sbin/nginx -t

提示:

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

(5)关于nginx启动出报错

比如,如下:

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] still could not bind()

这可能是nginx已经启动了,也可能是80端口被占用了。

可以使用lsof命令查询端口状态:

sudo lsof -i:80

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 35768 root 6u IPv4 62470 0t0 TCP *:http (LISTEN) nginx 35769 nobody 6u IPv4 62470 0t0 TCP *:http (LISTEN)

如果是其他进程占用,使用kill命令杀死进程就好,比如

kill -9 35768 kill -9 35769

五、error.log查看Nginx错误信息

当项目出现报错(比如上传文件),可以通过tail命令查看error.log文件排除问题。

sudo tail -f /usr/local/nginx/logs/error.log

六、重启nginx

# 停止: sudo /usr/local/nginx/sbin/nginx -s stop # 启动: sudo /usr/local/nginx/sbin/nginx



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3