跳转至

Web Server: Nginx V.S. Apache2

常见的web服务器

常见的web服务器有Apache、nginx、IIS

  1. Apache
    1. Apache音译为阿帕奇, 是全世界最受欢迎的web服务器,因其快速、可靠并且可通过简单的API扩充,能将Python\Perl等解释器部署在其上面等优势,受到广泛的关注与使用。
    2. 但是现在用的人少了,而且性能没nginx好
  2. nginx
    1. Apache的致命缺陷就是在同时处理大量的(一万个以上)请求时,显得有些吃力,所以“战斗民族”的人设计的一款轻量级的web服务器——nginx, 在高并发下nginx 能保持比Apache低资源低消耗高性能 ,
  3. IIS
    1. iis是Internet Information Services的缩写,意为互联网信息服务,是由微软公司提供的基于运行Microsoft Windows的互联网基本服务,

判断nginx/apach2

随便输出返回404页面显示 nginx/1.14.0 (Ubuntu)

sudo service apache2 status
sudo service nginx status
sudo vim /etc/ssh/sshd_config
#PasswordAuthentication yes
sudo service ssh restart

nginx

配置文件

  1. /etc/nginx/nginx.conf
  2. 全局块、
  3. events块
  4. http块
    1. http全局块
    2. 多个server块
      1. server全局块
      2. 多个location块
        1. Nginx中location的作用是根据Url来决定怎么处理用户请求(转发请求给其他服务器处理或者查找本地文件进行处理)。location支持正则表达式,配置十分灵活。我们可以在一个虚拟主机(nginx中的一个server节点)下配置多个location以满足如动静分离,防盗链等需求。

全局块

全局块是默认配置文件从开始到events块之间的一部分内容,主要设置一些影响Nginx服务器整体运行的配置指令,因此,这些指令的作用域是Nginx服务器全局。

# 指定可以运行nginx服务的用户和用户组,只能在全局块配置
user www-data;
# 指定工作线程数
worker_processes auto;
# 指定pid文件存放的路径,这个指令只能在全局块配置
pid /run/nginx.pid;
# include指令,用于包含其他的配置文件,可以放在配置文件的任何地方,但是要注意你包含进来的配置文件一定符合配置规范,比如说你include进来的配置是worker_processes指令的配置,而你将这个指令包含到了http块中,着肯定是不行的,上面已经介绍过worker_processes指令只能在全局块中。
include /etc/nginx/modules-enabled/*.conf;

events块

events 模块用于配置 Nginx 的事件处理机制。事件可以是网络连接、定时器等。一般来说,你不太需要直接修改 events 模块的配置,除非你对 Nginx 的事件处理机制有特殊的需求。默认的配置通常是适用于大多数情况的。

events {
    ; worker_connections 配置项定义每个 worker 进程可以同时处理的连接数
    worker_connections  768;
}

html块

http 模块是配置 Nginx HTTP 服务器的主要部分。在这个模块中,你可以配置服务器的行为、代理、日志、gzip 压缩等等。这是你放置虚拟主机(server 块)配置的地方。

http {
        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

include /etc/nginx/sites-enabled/*;是最重要的。

include /etc/nginx/sites-enabled/*

  • 在 Nginx 中,文件加载和生效的顺序是由 include 指令定义的。如果在这些文件中有重复的配置,后加载的配置将覆盖先加载的配置。因此,后加载的配置文件具有更高的优先级
  • 默认server块在/etc/nginx/sites-enabled/default
  • 默认填写了root /var/www/html;
server {
        listen 80 default_server;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
}

新配置

# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
server {
      listen 80;
      listen [::]:80;

      server_name example.com;

      root /var/www/example.com;
      index index.html;

      location / {
              try_files $uri $uri/ =404;
      }
}

Apache2

httpd是Apache超文本传输协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池。

Ubuntu 18.04下 Apache2 web 服务器的安装 (在node5测试)

sudo apt install apache2 -y

判断是否正常运行

systemctl status apache2
service apache2 status

开启、关闭和重启服务器,需要sudo

/etc/init.d/apache2 start    //启动Apache服务
/etc/init.d/apache2 stop    //停止Apache服务 
/etc/init.d/apache2 restart    //重启Apache服务

修改根目录/把文件传到根目录下

vim /etc/apache2/sites-available/000-default.conf
    DocumentRoot /var/www/html    // 默认
    DocumentRoot /home/shaojiemike/Network/HUGO/shaojiemike/public //但是没有访问文件的权限,最后是把静态网页放到/var/www/html下
sudo apache2ctl -k restart //重启
基础配置解释

默认内网

  • You should replace this file (located at /var/www/html/index.html) before continuing to operate your HTTP server.
  • Configuration Overview
  • Ubuntu's Apache2 default configuration is different from the upstream default configuration, and split into several files optimized for interaction with Ubuntu tools.
  • The configuration system is fully documented in /usr/share/doc/apache2/README.Debian.gz. Refer to this for the full documentation. Documentation for the web server itself can be found by accessing the manual if the apache2-doc package was installed on this server.
  • The configuration layout for an Apache2 web server installation on Ubuntu systems is as follows:

  • /etc/apache2/ |-- apache2.conf | `-- ports.conf |-- mods-enabled | |-- *.load | `-- *.conf |-- conf-enabled | `-- *.conf |-- sites-enabled | `-- *.conf

  • apache2.conf is the main configuration file. It puts the pieces together by including all remaining configuration files when starting up the web server.

  • ports.conf is always included from the main configuration file. It is used to determine the listening ports for incoming connections, and this file can be customized anytime.
  • Configuration files in the mods-enabled/, conf-enabled/ and sites-enabled/ directories contain particular configuration snippets which manage modules, global configuration fragments, or virtual host configurations, respectively.
  • They are activated by symlinking available configuration files from their respective *-available/ counterparts. These should be managed by using our helpers a2enmod, a2dismod, a2ensite, a2dissite, and a2enconf, a2disconf . See their respective man pages for detailed information.
  • The binary is called apache2. Due to the use of environment variables, in the default configuration,
  • apache2 needs to be started/stopped with /etc/init.d/apache2 or apache2ctl.
  • Calling /usr/bin/apache2 directly will not work with the default configuration.
  • Document Roots
  • By default, Ubuntu does not allow access through the web browser to any file apart of those located in /var/www, public_html directories (when enabled) and /usr/share (for web applications).
  • If your site is using a web document root located elsewhere (such as in /srv) you may need to whitelist your document root directory in /etc/apache2/apache2.conf.
    • 最简单实现mount -t none -o bind,ro /targetPath /var/www/html
  • The default Ubuntu document root is /var/www/html. You can make your own virtual hosts under /var/www. This is different to previous releases which provides better security out of the box.

遇到的问题

You don't have permission to access / on this server.

把静态网页传上去

$ sudo apache2ctl -k restart
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message

没有访问权限

Forbidden
You don't have permission to access / on this server.

这是因为没有修改运行访问的目录,而且能通过别名

sudo vim apache2.conf
alias /testtsj/ "/home/shaojiemike/Network/"
<Directory "/home/shaojiemike/Network/">
    Options Indexes FollowSymLinks #首页不存在,允许访问当前目录下其它内容
    AllowOverride None
    Require all granted #允许访问所有
</Directory>

testtsj

Miscellaneous

topdown-like 的网站设计

  1. 注意字体随页面滑动的颜色的变化1

网站证书

买域名,再asme.sh 挂载部署过apache或者nginx

免费ssl证书2

上面那个是手动的,下面那个是手动完成之后,自动更新的。 git shared

参考文献

https://blog.csdn.net/weixin_39212776/article/details/81192847

https://blog.csdn.net/weixin_41843699/article/details/90390562