本文主要介绍如何在Ubuntu (12.04/14.04/16.04)系统上安装当前相当流行的web 服务器Nginx. 越来越多的商业应用已经将web服务器从Apache转到Nginx. 因为Nginx服务会比Apache 服务器更加节省系统资源,而且也可以用来做反向代理服务器。
Nginx 是什么
Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。 -来自百度百科
Ubuntu 下安装配置Nginx
1# 安装nginx软件包
在ubuntu命令行下,可以通过执行apt-get命令来安装nginx相关的包,执行下面的命令:
$sudo apt-get update $sudo apt-get install nginx
命令输出如下:
root@devops:~# sudo apt-get update sudo: 无法解析主机:devops 命中:1 http://mirrors.aliyun.com/ubuntu xenial InRelease 命中:2 http://mirrors.aliyun.com/ubuntu xenial-security InRelease 命中:3 http://mirrors.aliyun.com/ubuntu xenial-updates InRelease 命中:4 http://mirrors.aliyun.com/ubuntu xenial-proposed InRelease 命中:5 http://mirrors.aliyun.com/ubuntu xenial-backports InRelease 忽略:6 http://dl.google.com/linux/chrome/deb stable InRelease 命中:7 http://dl.google.com/linux/chrome/deb stable Release 忽略:8 http://archive.ubuntu.com/ubuntu trusty InRelease 命中:9 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial InRelease 命中:11 http://archive.ubuntu.com/ubuntu trusty Release 正在读取软件包列表... 完成
root@devops:~# apt-get install nginx 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 下列软件包是自动安装的并且现在不需要了: java-common libappindicator1 libindicator7 linux-headers-4.10.0-28 linux-headers-4.10.0-28-generic linux-image-4.10.0-28-generic linux-image-extra-4.10.0-28-generic 使用'apt autoremove'来卸载它(它们)。 将会同时安装下列软件: nginx-common nginx-core 建议安装: fcgiwrap nginx-doc 下列【新】软件包将被安装: nginx nginx-common nginx-core 升级了 0 个软件包,新安装了 3 个软件包,要卸载 0 个软件包,有 207 个软件包未被升级。 需要下载 458 kB 的归档。 解压缩后会消耗 1,482 kB 的额外空间。 您希望继续执行吗? [Y/n] y 获取:1 http://mirrors.aliyun.com/ubuntu xenial-security/main amd64 nginx-common all 1.10.3-0ubuntu0.16.04.2 [26.6 kB] 获取:2 http://mirrors.aliyun.com/ubuntu xenial-security/main amd64 nginx-core amd64 1.10.3-0ubuntu0.16.04.2 [428 kB] 获取:3 http://mirrors.aliyun.com/ubuntu xenial-security/main amd64 nginx all 1.10.3-0ubuntu0.16.04.2 [3,490 B] 已下载 458 kB,耗时 0秒 (1,540 kB/s) 正在预设定软件包 ... 正在选中未选择的软件包 nginx-common。 (正在读取数据库 ... 系统当前共安装有 290959 个文件和目录。) 正准备解包 .../nginx-common_1.10.3-0ubuntu0.16.04.2_all.deb ... 正在解包 nginx-common (1.10.3-0ubuntu0.16.04.2) ... 正在选中未选择的软件包 nginx-core。 正准备解包 .../nginx-core_1.10.3-0ubuntu0.16.04.2_amd64.deb ... 正在解包 nginx-core (1.10.3-0ubuntu0.16.04.2) ... 正在选中未选择的软件包 nginx。 正准备解包 .../nginx_1.10.3-0ubuntu0.16.04.2_all.deb ... 正在包 nginx (1.10.3-0ubuntu0.16.04.2) ... 正在处理用于 ureadahead (0.100.0-19) 的触发器 ... 正在处理用于 ufw (0.35-0ubuntu2) 的触发器 ... 正在处理用于 systemd (229-4ubuntu21.1) 的触发器 ... 正在设置 nginx-common (1.10.3-0ubuntu0.16.04.2) ... 正在设置 nginx-core (1.10.3-0ubuntu0.16.04.2) ... 正在设置 nginx (1.10.3-0ubuntu0.16.04.2) ... 正在处理用于 systemd (229-4ubuntu21.1) 的触发器 ... 正在处理用于 ureadahead (0.100.0-19) 的触发器 ... 正在处理用于 ufw (0.35-0ubuntu2) 的触发器 ...
这样我们已经安装好Nginx服务区所需要的软件包。
2# 配置防火墙,开启http访问
在ubuntu系统里,配置防火墙的命令是ufw,首先我们可以查看下ufw配置列表,执行命令如下:
$sudo ufw app list
命令输出如下:
root@devops:~# ufw app list 可用应用程序: CUPS Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
从上面的输出,我们可以考到通过ufw工具可以控制的应用程序的防火墙设置。
配置说明如下:
- Nginx Full –可以可以打开Nginx web服务器所需要的80 和443 端口号
- Nginx HTTP – 这个配置用来打开Http端口号80,允许通过80端口访问web服务
- Nginx HTTPS – 只打开HTTPS端口号443.
通过执行下面的命令来开启HTTP端口,添加防火墙策略:
$ sudo ufw allow ‘Nginx HTTP’
命令输出如下:
root@devops:~# ufw allow 'Nginx HTTP' 防火墙规则已更新 规则已更新(v6)
查看防火墙状态
执行下面的命令:
$sudo ufw status
命令输出如下:
root@devops:~# ufw status 状态: 激活 至 动作 来自 - -- -- Nginx HTTP ALLOW Anywhere OpenSSH ALLOW Anywhere Nginx HTTP (v6) ALLOW Anywhere (v6) OpenSSH (v6) ALLOW Anywhere (v6)
从上面的输出我们可以看到HTTP 端口允许来自任何主机的访问请求。
3# 查看Nginx服务的状态
当我们安装完Nginx软件包后,会自动开启Nginx服务,执行下面的命令查看服务是否正常启动:
#systemctl status nginx
命令输出如下:
root@devops:~# systemctl status nginx nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since 二 2018-03-27 21:52:10 CST; 14min ago Main PID: 2482 (nginx) CGroup: /system.slice/nginx.service ├─2482 nginx: master process /usr/sbin/nginx -g daemon on; master_process on └─2484 nginx: worker process 3月 27 21:52:09 devops systemd[1]: Starting A high performance web server and a reverse proxy server... 3月 27 21:52:10 devops systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument 3月 27 21:52:10 devops systemd[1]: Started A high performance web server and a reverse proxy server.
从输出看到状态是Active, 说明已经正常启动。
4# 查看是否能够通过web页面正常访问
在浏览器里输入Nginx服务器的网络地址,查看Nginx页面是否能够正常显示。
5# 将nginx服务加到自启动里
如果想让系统重启后能够自动启动Nginx服务,可以执行下面的命令来开启开机自动启动。
#systemctl enable nginx
命令输出如下:
root@devops:~# systemctl enable nginx Synchronizing state of nginx.service with SysV init with /lib/systemd/systemd-sysv-install... Executing /lib/systemd/systemd-sysv-install enable nginx
6# Nginx 服务重启/停止/启动
如果需要启动Nginx服务,执行命令:
#systemctl start nginx
如果要停止Nginx 服务,执行命令如下:
#systemctl stop nginx
如果需要重新启动Nginx服务,执行命令:
#systemctl restart nginx
Nginx 服务器的主配置是/etc/nginx/nginx.conf,如果需要修改nginx的变量值,可以编辑这个文件。
Nginx服务器的日志文件为/var/log/nginx/access.log 和 /var/log/nginx/error.log 错误日志文件。
ubuntu nginx php-fpm 安装
执行apt-get命令来安装php-fpm,如下:
#apt-get install php5-fpm
99%的人还阅读了: