Nginx 入门指北
Nginx是一个高性能的HTTP和反向代理服务器。
前端开发人员迈向全栈,服务器相关的技术是不可绕过的一个门槛。先以简单的nginx为切入点进行学习。
环境准备
参考《Vagrant 入门指北》 快速的搭建一个Centos7虚机,并默认安装好nginx。
» mkdir nginx
» cd nginx
» vagrant init centos/7
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
$script = <<SCRIPT
echo "Installing dependencies ..."
yum install -y epel-release
yum install -y net-tools
echo "Installing nginx ..."
yum install -y nginx
SCRIPT
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.provision "shell", inline: $script
config.vm.network "private_network", ip: "172.30.30.10"
end
启动虚机,并登陆虚机
» vagrant up
» vagrant ssh
[vagrant@localhost ~]$
配置用户信息
切换到root用户
[vagrant@localhost ~]$ su
Password:
[root@localhost vagrant]#
创建用户
创建一个名为web的用户
[root@localhost vagrant]# useradd web
为web用户设置密码
[root@localhost vagrant]# passwd web
Changing password for user web.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
为web用户设置目录
[root@localhost vagrant]# usermod -md /opt/web web
允许web用户可ssh登陆到服务器
echo "web" >> /etc/sshusers
编辑ssh登陆服务的配置文件/etc/ssh/sshd_config
,启用密码验证
找到PasswordAuthentication no
改为PasswordAuthentication yes
重启ssh服务
systemctl restart sshd
在自己的主机上新开一个命令行窗口以web用户身份登陆虚机
» ssh web@172.30.30.10 nasa@nasawangdeMacBook-Pro
The authenticity of host '172.30.30.10 (172.30.30.10)' can't be established.
ECDSA key fingerprint is SHA256:HVaaVvkx98yK1QOWzeaJ5oJWUktbbTZdQr66DKtwH5k.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.30.30.10' (ECDSA) to the list of known hosts.
web@172.30.30.10's password:
Last login: Thu Jun 14 02:09:07 2018 from 172.30.30.1
[web@localhost ~]$
登陆成功
找一个ftp软件登陆服务器就可以直接上传前端的页面代码了。
配置 nginx
登录到虚机,切换到root用户
nginx在centos上安装后默认配置文件是/etc/nginx/nginx.conf
编辑这个文件,找到 root /usr/share/nginx/html;
改为root /opt/web;
找到 user nginx;
改为user root;
在命令行键入setenforce 0
设置selinux模式为宽容模式。
编辑文件可以用vi
对初学者来说操作有些复杂。 这里可以使用nano
来做编辑,操作相对来说稍微简单些。 用命令yum install -y nano
安装。键入nano /etc/nginx/nginx.conf
编辑nginx.conf
启动nginx,并检查nginx状态
[root@localhost nginx]# systemctl start nginx
[root@localhost nginx]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2018-06-14 02:46:41 UTC; 5s ago
Process: 4073 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 4071 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 4070 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 4075 (nginx)
CGroup: /system.slice/nginx.service
├─4075 nginx: master process /usr/sbin/nginx
└─4076 nginx: worker process
Jun 14 02:46:41 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jun 14 02:46:41 localhost.localdomain nginx[4071]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jun 14 02:46:41 localhost.localdomain nginx[4071]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jun 14 02:46:41 localhost.localdomain systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Jun 14 02:46:41 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.
在ftp中上传一个index.html文件。
打开浏览器输入 http://172.30.30.10/
就可以看到刚刚上传的页面了。
参考资料
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。