Ubuntu安装nginx

apt-get安装nginx

  1. 切换至root用户
1
2
sudo su root
apt-get install nginx
  1. 查看nginx是否安装成功
1
nginx -v
  1. 启动nginx
1
service nginx start

启动后,在网页重输入ip地址,即可看到nginx的欢迎页面。至此nginx安装成功

在这里插入图片描述

nginx文件安装完成之后的文件位置:

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

下载nginx包安装

由于上面已经安装了nginx,所以我们先卸载nginx。再重新上传nginx包,解压下载。有输入提示时,输入Y即可

  1. 卸载apt-get安装的nginx
1
2
# 彻底卸载nginx
apt-get --purge autoremove nginx
  1. 查看nginx的版本号
    1
    nginx -v
在这里插入图片描述
  1. 安装依赖包
1
2
3
apt-get install gcc
apt-get install libpcre3 libpcre3-dev
apt-get install zlib1g zlib1g-dev

Ubuntu14.04的仓库中没有发现openssl-dev,由下面openssl和libssl-dev替代

1
2
3
#apt-get install openssl openssl-dev
sudo apt-get install openssl
sudo apt-get install libssl-dev
  1. 安装nginx
1
2
3
4
5
cd /usr/local
mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz
在这里插入图片描述
  1. 编译nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 进入nginx目录

/usr/local/nginx/nginx-1.13.7

# 执行命令

./configure

# 执行make命令

make

# 执行make install命令

make install
  1. 启动nginx
1
2
3
4
5
6
#进入nginx启动目录
cd /usr/local/nginx/sbin

# 启动nginx

./nginx
在这里插入图片描述
  1. 访问nginx

网页输入ip地址,访问成功,到此,nginx安装完毕

在这里插入图片描述

使用 OneinStack安装

参考下面命令,一步步自定义安装。

1
2
3
4
5
6
7
yum -y install wget screen #for CentOS/Redhat
# apt-get -y install wget screen #for Debian/Ubuntu
wget http://mirrors.linuxeye.com/oneinstack-full.tar.gz #包含源码,国内外均可下载
tar xzf oneinstack-full.tar.gz
cd oneinstack #如果需要修改目录(安装、数据存储、Nginx日志),请修改options.conf文件
screen -S oneinstack #如果网路出现中断,可以执行命令`screen -R oneinstack`重新连接安装窗口
./install.sh
img

如何添加附加组件?

注意

如果之前没有安装组件,后续补充安装,统一入口为./install.sh,addons.sh脚本不在提供,如之前没有安装php redis扩展,补充安装命令:./install.sh --php_extensions redis

1
~/oneinstack/addons.sh
img

如何添加虚拟主机?

1
~/oneinstack/vhost.sh
img

启动Nginx

1
service nginx start

重载 Nginx 使配置生效

验证 nginx 配置

1
nginx -t

如果输出如下提示则代表配置有效:

1
2
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

重载 Nginx 配置:

1
nginx -s reload

重启Nginx

1
service nginx restart

可能遇到的问题:

nginx使用service nginx restart报错

启动nginx服务时如果遇到这个错误 Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

可能原因:

1、配置文件语法有误,执行nginx -t

查看输出提示信息 并检查端口是否被占用netstat -tnlp

2、nginx的配置文件nginx.conf中监听了其他端口,这些端口的子进程仍然运行,导致端口占用。需要首先关闭子进程,才能使用该命令。

此时可以kill -9 pid

img

Linux环境Nginx命令

Linux环境Nginx命令功能
sudo nginx -t查看配置文件语法
sudo nginx -c /usr/local/nginx/conf/nginx.conf指定配置文件启动
sudo nginx -s stop强制停止nginx
sudo nginx -s reload重新加载配置文件
sudo systemctl(或者service) start nginx启动nginx
sudo systemctl(或者service) enable nginx开机自启动
sudo systemctl(service) restart nginx重启nginx

参考:oneinstack安装步骤


Ubuntu安装nginx
https://www.blog.spphoto.top/2022/03/07/ubuntu安装nginx-ubuntu-an-zhuang-nginx/
Author
Champagne
Posted on
March 7, 2022
Licensed under