Apache 配置学习

基本

  • 配置文件/etc/apach2/apache2.conf以及/etc/apache2/conf-enabled
  • DirectoryIndex index.html index.php 优先级从左到右降低
  • 包含外部配置文件 Include xxx

启动

apachectl脚本,从/etc/apache2/envavars中读取并设置一些环境变量

Rewrite

基于正则表达式的规则,动态修改 incoming URl requests。

Virtual Host

用同一个 Apache 服务器上搭建多个服务,根据用户请求中不同的 Host首部(或其他条件) 将请求转给不同的VirtualHost来处理。

Name-based vs IP-based Virtual Hosts

基于IP的虚拟主机,要求服务器有多个IP,Apache根据用户请求的IP来转发到相应的主机中
基于名字的虚拟主机,要求客户端在HTTP首部中包含主机的名字,这样多个主机就可以共享同一个IP。
基于名字(hostname)的虚拟主机,通常只需要将多个域名解析到同一个IP即可。
比如说,login.a.com和logout.a.com只想相同的IP,也使用相同的端口(即后台的web服务器是同一个),但是后台的web服务器却可以根据请求中的Host首部不同,为他们提供不同的服务。

服务器如何选择合适的 name-based virtual host?

先进行 IP-based的选择,也就是先根据请求的IP选择出为此IP服务的Virtual Host,然后再从中找出最符合的name-based virtual host。

先根据IP和端口选出一组最符合的hosts,然后根据ServerNameServerAlias作比较。如果没有符合的ServerName和ServerAlias,则选择列出的第一个。

省略ServerName则默认使用fully qualified domain name。

参考链接

PHP 基础回顾

include/require

如果没有提供路径,则会查找include_path,如果没有找到,则会查找当前脚本所在目录和当前工作目录。如果均失败的话,include 会产生一个 warnning,require会产生一个 fatal error.

当一个文件被 included时,被包含文件中的代码继承了include所在行的变量作用域。任何在include处可见的变量,对于被包含的代码也可见(相当于将被包含的代码复制并替换include命令。)但是,被包含文件中定义的函数和类都拥有全局作用域。

在解析被包含的文件时,解析器会切换为 HTML模式,所以任何没有包含在 php tag中的文本都会当做HTML处理,也就是会被原样 echo 给客户端。

include失败会返回FALSE,并产生一个warning。如果include成功,并且被包含文件中没有return语句,include会返回1。 如果被包含文件中有return语句,那么执行到return语句时,会停止对被包含文件的处理,并返回return提供的值,就像调用一个普通的函数一样。

如果被包含文件中定义了函数,并且被包含了两次,则会造成 fatal error,因为函数已经被声明过了。最好使用 include_once。

安装

  • sudo apt-add-repository ppa:ondrej/php5
  • php-xxx都是PHP的扩展包,比如php-gdphp-mysqlphp-xdebug需要手动安装
  • Apache 通过 libapache2-mod-php7.1模块与PHP互动,配置文件为/etc/apache2/mod-enabled/php7.0/
  • /usr/bin/php也只是一个alternative,可以使用update-alternative改变当前php命令使用的版本

安装过程中的处理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
Setting up php7.1-common (7.1.0-5+deb.sury.org~xenial+1) ...
Creating config file /etc/php/7.1/mods-available/calendar.ini with new version
Creating config file /etc/php/7.1/mods-available/ctype.ini with new version
Creating config file /etc/php/7.1/mods-available/exif.ini with new version
Creating config file /etc/php/7.1/mods-available/fileinfo.ini with new version
Creating config file /etc/php/7.1/mods-available/ftp.ini with new version
Creating config file /etc/php/7.1/mods-available/gettext.ini with new version
Creating config file /etc/php/7.1/mods-available/iconv.ini with new version
Creating config file /etc/php/7.1/mods-available/pdo.ini with new version
Creating config file /etc/php/7.1/mods-available/phar.ini with new version
Creating config file /etc/php/7.1/mods-available/posix.ini with new version
Creating config file /etc/php/7.1/mods-available/shmop.ini with new version
Creating config file /etc/php/7.1/mods-available/sockets.ini with new version
Creating config file /etc/php/7.1/mods-available/sysvmsg.ini with new version
Creating config file /etc/php/7.1/mods-available/sysvsem.ini with new version
Creating config file /etc/php/7.1/mods-available/sysvshm.ini with new version
Creating config file /etc/php/7.1/mods-available/tokenizer.ini with new version
Setting up php7.1-json (7.1.0-5+deb.sury.org~xenial+1) ...
Creating config file /etc/php/7.1/mods-available/json.ini with new version
Setting up php7.1-opcache (7.1.0-5+deb.sury.org~xenial+1) ...
Creating config file /etc/php/7.1/mods-available/opcache.ini with new version
Setting up php7.1-readline (7.1.0-5+deb.sury.org~xenial+1) ...
Creating config file /etc/php/7.1/mods-available/readline.ini with new version
Setting up php7.1-cli (7.1.0-5+deb.sury.org~xenial+1) ...
update-alternatives: using /usr/bin/php7.1 to provide /usr/bin/php (php) in auto mode
update-alternatives: using /usr/bin/phar7.1 to provide /usr/bin/phar (phar) in auto mode
update-alternatives: using /usr/bin/phar.phar7.1 to provide /usr/bin/phar.phar (phar.phar) in auto mode
Creating config file /etc/php/7.1/cli/php.ini with new version
Setting up libapache2-mod-php7.1 (7.1.0-5+deb.sury.org~xenial+1) ...
Creating config file /etc/php/7.1/apache2/php.ini with new version
libapache2-mod-php7.1: php7.0 module already enabled, not enabling PHP 7.1
Setting up libapache2-mod-php (1:7.1+49+deb.sury.org~xenial+4) ...
Setting up php7.0-common (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-mcrypt (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-mbstring (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-xml (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-readline (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-opcache (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-mysql (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-json (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up libwebp5:amd64 (0.4.4-1+deb.sury.org~xenial+1) ...
Setting up libgd3:amd64 (2.2.3-3+deb.sury.org~xenial+0) ...
Setting up php7.0-gd (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-curl (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-cli (7.0.14-2+deb.sury.org~xenial+1) ...
Replacing config file /etc/php/7.0/cli/php.ini with new version
Setting up libapache2-mod-php7.0 (7.0.14-2+deb.sury.org~xenial+1) ...
Installing new version of config file /etc/apache2/mods-available/php7.0.load ...
libapache2-mod-php7.0: not switching MPM - already enabled
Setting up php7.0 (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up libjbig0:amd64 (2.1-3.1+deb.sury.org~xenial+1) ...
Setting up libpcre16-3:amd64 (2:8.39-1+deb.sury.org~xenial+1) ...
Setting up libsodium18:amd64 (1.0.10-1+deb.sury.org~xenial+1) ...
Setting up libssl-doc (1.0.2j-1+deb.sury.org~xenial+1) ...
Setting up libssl-dev:amd64 (1.0.2j-1+deb.sury.org~xenial+1) ...
Setting up libwebpmux1:amd64 (0.4.4-1+deb.sury.org~xenial+1) ...
Setting up libzmq5:amd64 (4.2.0-1+deb.sury.org~xenial+1) ...
Setting up openssl (1.0.2j-1+deb.sury.org~xenial+1) ...
Setting up php7.1-curl (7.1.0-5+deb.sury.org~xenial+1) ...
Creating config file /etc/php/7.1/mods-available/curl.ini with new version
Setting up php-curl (1:7.1+49+deb.sury.org~xenial+4) ...
Setting up php7.1-gd (7.1.0-5+deb.sury.org~xenial+1) ...
Creating config file /etc/php/7.1/mods-available/gd.ini with new version
Setting up php-gd (1:7.1+49+deb.sury.org~xenial+4) ...
Setting up php7.1-xml (7.1.0-5+deb.sury.org~xenial+1) ...
Creating config file /etc/php/7.1/mods-available/dom.ini with new version
Creating config file /etc/php/7.1/mods-available/simplexml.ini with new version
Creating config file /etc/php/7.1/mods-available/wddx.ini with new version
Creating config file /etc/php/7.1/mods-available/xml.ini with new version
Creating config file /etc/php/7.1/mods-available/xmlreader.ini with new version
Creating config file /etc/php/7.1/mods-available/xmlwriter.ini with new version
Creating config file /etc/php/7.1/mods-available/xsl.ini with new version
Setting up php-xml (1:7.1+49+deb.sury.org~xenial+4) ...
Setting up php-pear (1:1.10.1+submodules+notgz-8+donate.sury.org~xenial+2) ...
Setting up php-gettext (1.0.11-2+deb.sury.org~xenial+1) ...
Setting up php7.1-mbstring (7.1.0-5+deb.sury.org~xenial+1) ...
Creating config file /etc/php/7.1/mods-available/mbstring.ini with new version
Setting up php-mbstring (1:7.1+49+deb.sury.org~xenial+4) ...
Setting up php7.1-mcrypt (7.1.0-5+deb.sury.org~xenial+1) ...
Creating config file /etc/php/7.1/mods-available/mcrypt.ini with new version
Setting up php-mcrypt (1:7.1+49+deb.sury.org~xenial+4) ...
Setting up php7.1-mysql (7.1.0-5+deb.sury.org~xenial+1) ...
Creating config file /etc/php/7.1/mods-available/mysqlnd.ini with new version
Creating config file /etc/php/7.1/mods-available/mysqli.ini with new version
Creating config file /etc/php/7.1/mods-available/pdo_mysql.ini with new version
Setting up php-mysql (1:7.1+49+deb.sury.org~xenial+4) ...
Setting up php-xdebug (2.5.0-1+deb.sury.org~xenial+1) ...
Processing triggers for libc-bin (2.23-0ubuntu5) ...
Processing triggers for libapache2-mod-php7.1 (7.1.0-5+deb.sury.org~xenial+1) ...
  • 问题,现在通过a2enmod php7.1启用了PHP7.1,那么对于PHP的请求,会交给PHP7.0呢?还是PHP7.1呢? =》 默认的还是PHP7.0,需要自己手动 a2dismod php7.0才行
  • php-pear是什么?

Posix Thread 基础

线程概念

一个线程包含在进程中表示一个 execution context 的必要信息,包括 thread ID, a set of register values, a stack, a scheduling priority and policy, a signal mask, an errno variable, and thread-specific data.

进程中的所有东西都是线程间共享的,包括 text of the executable program, the programs’s global and heap memory, the stacks, and the file descriptors.

阅读全文

Python logging 基础

一个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import logging
logger=logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# 添加handler
handler=logging.FileHandler('hello.log')
handler.setLevel(logging.INFO)
# handler可以有多个,所以用add
logger.addHandler(handler)
# 设置格式
formatter=logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s', '%H:%M:%S')
# formatter只能有一个
logger.setFormatter(formatter)

对于库来说,通常使用 __name__ 来获取 Logger对象,然后只添加一个NullHandler,表示默认不会输出任何log:

阅读全文