Nginx是一個高性能的HTTP和反向代理服務器。
前端開發人員邁向全棧,服務器相關的技術是不可繞過的一個門檻。先以簡單的nginx爲切入點進行學習。
參考《Vagrant 入門指北》 快速的搭建一個Centos7虛機,並默認安裝好nginx。html
» 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
啓動虛機,並登錄虛機linux
» vagrant up » vagrant ssh [vagrant@localhost ~]$
切換到root用戶nginx
[vagrant@localhost ~]$ su Password: [root@localhost vagrant]#
建立一個名爲web的用戶web
[root@localhost vagrant]# useradd web
[root@localhost vagrant]# passwd web Changing password for user web. New password: Retype new password: passwd: all authentication tokens updated successfully.
[root@localhost vagrant]# usermod -md /opt/web web
echo "web" >> /etc/sshusers
編輯ssh登錄服務的配置文件/etc/ssh/sshd_config
,啓用密碼驗證
找到PasswordAuthentication no
改成PasswordAuthentication yes
shell
重啓ssh服務segmentfault
systemctl restart sshd
在本身的主機上新開一個命令行窗口以web用戶身份登錄虛機centos
» 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軟件登錄服務器就能夠直接上傳前端的頁面代碼了。ruby
登陸到虛機,切換到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/
就能夠看到剛剛上傳的頁面了。
參考資料