Ubuntu安装配置Nginx(一)——部署Web服务

1、安装环境

ubuntu版本:16.04php

2、安装

一、安装

// 更新包
sudo apt-get update
// 下载安装nginx
sudo apt-get install nginx

二、测试安装

在命令行中输入:html

sudo nginx -t

窗口显示:nginx

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

在浏览器中输入ip地址:
图片描述web

三、重启nginx

sudo service nginx restart

2、卸载

一、删除nginx,-purge包括配置文件

sudo apt-get --purge remove nginx

二、移除所有不使用的软件包

sudo apt-get autoremove

三、罗列出与nginx相关的软件并删除

dpkg --get-selections|grep nginx
sudo apt-get --purge remove nginx
sudo apt-get --purge remove nginx-common
sudo apt-get --purge remove nginx-core

四、查看nginx正在运行的进程,若是有就kill掉

ps -ef |grep nginx
sudo kill -9 XXX

3、配置Nginx

最新版本nginx配置是由4个文件构成:apache

  1. conf.d:用户本身定义的conf配置文件
  2. sites-available:系统默认设置的配置文件
  3. sites-enabled:由sites-available中的配置文件转换生成
  4. nginx.conf:汇总以上三个配置文件的内容,同时配置咱们所须要的参数

在部署须要的web服务时,咱们能够拷贝sites-enabled中的default文件到conf.d而且修更名字为**.conf,而后进行配置ubuntu

server {
    #服务启动时监听的端口
    listen 80 default_server;
    listen [::]:80 default_server;
    #服务启动时文件加载的路径
    root /var/www/html/wordpress;
    #默认加载的第一个文件
    index index.php index.html index.htm index.nginx-debian.html;
    #页面访问域名,若是没有域名也能够填写_
    server_name www.xiexianbo.xin;

    location / {
        #页面加载失败后所跳转的页面
        try_files $uri $uri/ =404;
    }
    
      
    #如下配置只服务于php
    # 将PHP脚本传递给在127.0.0.1:9000上监听的FastCGI服务器
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        # With php7.0-cgi alone:
        #fastcgi_pass 127.0.0.1:9000;
        # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    # 若是Apache的文档为root,则拒绝访问.htaccess文件
    location ~ /\.ht {
        deny all;
    }
}

注意事项:浏览器

  1. apache的端口也是80,因此咱们能够选择关闭apache或者,在这里更换端口,例如81,82等,可是咱们须要吧这个端口开放出来
  2. React、Vue等因为是单页面应用,因此咱们在刷新的会遇到资源加载不到的错误,这时咱们须要把页面重定向到index.html服务器

    try_files $uri /index.html;
  3. 每次配置完成后,都须要重启nginx。

下期内容

nginx如何优化Web服务php7

相关文章
相关标签/搜索