1. Lighttpd 1.1 配置及验证 1.2 测试安装结果 1.3 基本日志记录 2. Nginx 2.1 安装和启动 2.2 配置及验证 2.2.1 服务器块 2.2.2 管理服务器入口 2.3 FastCGI 2.3.1 PHP实现 2.3.2 nginx配置 2.3.3 测试配置 3. Apache 3.1 安装和启动 3.2 配置及验证 3.2.1 选项 3.2.2 检查配置 3.3 用户目录 3.4 扩展PHP 3.4.1 安装php-fpm 3.4.2. 测试PHP是否有效
| Apache | Nginx, nginx-mainline | Lighttpd | ╦═╦ | Php-fpm |
| /etc/httpd/conf/httpd.conf | /etc/nginx/nginx.conf | /etc/lighttpd/lighttpd.conf | config | /etc/php/php-fpm.conf |
| $ apachectl configtest | $ nginx -t | $ lighttpd -t -f /etc/lighttpd/lighttpd.conf $ lighttpd -tt -f /etc/lighttpd/lighttpd.conf |
配置验证 | ../test.php <?php phpinfo(); |
| /srv/http ; ~/public_html | /usr/share/nginx/html/ | /srv/http/ | root | |
| httpd.service | nginx.service | lighttpd.service | service | php-fpm.service |
| http://localhost/ http://localhost/~tom/ |
配置工具: https://nginxconfig.io/ |
示例配置: /usr/share/doc/lighttpd/config/ |
Other | http://127.0.0.1/test.php |
1. Lighttpd
lighttpd是一个安全,快速,标准,且非常灵活的网页服务器,并对高性能环境做了最佳化。
相较于其他网页服务器,它占用的内存很少,且注重CPU负载量。
1.1 配置及验证
想要检查 lighttpd.conf 中的语法错误,可以使用以下命令来快速查找错误:
$ lighttpd -t -f /etc/lighttpd/lighttpd.conf
还可以进行更彻底的预检:
$ lighttpd -tt -f /etc/lighttpd/lighttpd.conf
示例配置文件在 /usr/share/doc/lighttpd/
1.2 测试安装结果
$ sudo chmod 755 /srv/http/index.html
$ sudo echo 'Test-lighttpd' >> /srv/http/index.html
修改日志目录权限:
$ sudo chown -R http:http /var/log/lighttpd
启动服务:
$ sudo systemctl start lighttpd
然后就可以使用浏览器打开网址 localhost ,你应该能见到测试页面。
http://127.0.0.1/
1.3 基本日志记录
server.modules += (
"mod_access",
"mod_accesslog",
)
server.errorlog = "/var/log/lighttpd/error.log"
accesslog.filename = "/var/log/lighttpd/access.log"
2. Nginx
Nginx (读作"engine X") 由Igor Sysoev(俄罗斯)于2005年编写,是一个免费、开源、高性能的HTTP服务器和反向代理,也可以作为一个IMAP/POP3代理服务器。
Nginx因为稳定,丰富的功能集,配置简单,资源占用低而闻名世界。这篇文章描述了如何设置nginx并且如何通过#FastCGI集成PHP.
2.1 安装和启动
推荐使用主线分支。使用稳定分支的主要原因是,你关注的新功能可能产生的影响,如与第三方模块或无意引入的错误的不兼容的新功能。
注意:官方存储库中可用的所有 nginx 模块都需要nginx包(而不是nginx-mainline)作为依赖项。
在做出nginx与nginx-mainline决定之前,查看您可能需要/想要的任何模块列表可能是明智的。nginx-mainline 的模块可以在Arch User Repository 中找到。
$ sudo systemctl start nginx.service
http://127.0.0.1
访问到的默认页面是: /usr/share/nginx/html/index.html
2.2 配置及验证
https://wiki.nginx.org/Configuration
https://nginx.org/en/docs/
使用 nginx 的第一步在初学者指南中有所描述。您可以通过编辑中的文件来修改配置/etc/nginx/主配置文件位于/etc/nginx/nginx.conf.
更多细节和示例可以在https://wiki.nginx.org/Configuration和官方文档中找到。
下面的示例涵盖了最常见的用例。假定您使用文档的默认位置 (/usr/share/nginx/html)。如果不是这种情况,请替换您的路径。
提示: DigitalOcean 提供了一个Nginx 配置工具。https://nginxconfig.io/
配置验证
$ sudo nginx -t
2021/06/02 19:46:07 [warn] 2420#2420: could not build optimal types_hash, you should increase either types_hash_max_size: 1024 or types_hash_bucket_size: 64; ignoring types_hash_bucket_size
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
2.2.1 服务器块
可以使用server块为多个域提供服务。这些类似于Apache HTTP Server 中的“VirtualHosts” 。另请参阅上游示例。
在下面的示例中,服务器侦听两个域的 IPv4 和 IPv6 端口 80 上的传入连接,domainname1.dom并且domainname2.dom:
/etc/nginx/nginx.conf
...
server {
listen 80;
listen [::]:80;
server_name domainname1.dom;
root /usr/share/nginx/domainname1.dom/html;
location / {
index index.php index.html index.htm;
}
}
server {
listen 80;
listen [::]:80;
server_name domainname2.dom;
root /usr/share/nginx/domainname2.dom/html;
...
}
重新启动 nginx.service以应用任何更改。
注意:通过设置像BIND或dnsmasq这样的 DNS 服务器来确保主机名是可解析的,或者查看网络配置#本地网络主机名解析。
2.2.2 管理服务器入口
创建以下文件夹:
$ sudo mkdir /etc/nginx/sites-available
$ sudo mkdir /etc/nginx/sites-enabled
在sites-available文件夹下创建一个文件包含一个或更多服务器模块:
/etc/nginx/sites-available/example.conf
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
}
把include sites-enabled/*;放到http 模块的末尾:
/etc/nginx/nginx.conf
...
http {
...
include sites-enabled/*;
}
...
如果要启用一个网站, 只需要简单的创建一个符号链接:
$ sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/example.conf
如果要移除一个网站
$ sudo unlink /etc/nginx/sites-enabled/example.conf
Reload/restart nginx.service 来让配置生效.
2.3 FastCGI
FastCGI 技术被引入 nginx 以与许多外部工具一起工作,例如Perl、PHP和Python。
2.3.1 PHP实现
安装 php-fpm并确保PHP已正确安装和配置。PHP-FPM 的主要配置文件是/etc/php/php-fpm.conf. 对于基本使用,默认配置应该足够了。
$ sudo systemctl status php-fpm.service
2.3.2 nginx配置
/etc/nginx/sites-available/example.conf
server {
root /usr/share/nginx/html;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
# 404
try_files $fastcgi_script_name =404;
# default fastcgi_params
include fastcgi_params;
# fastcgi settings
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
# fastcgi params
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
#fastcgi_param PHP_ADMIN_VALUE "open_basedir=$base/:/usr/lib/php/:/tmp/";
}
}
若需要处理PHP以外的文件(html, htm):
location ~ [^/]\.(php|html|htm)(/|$) {
...
}
非 .php 的PHP-FPM扩展处理都需要被直接加入到 /etc/php/php-fpm.d/www.conf:
security.limit_extensions = .php .html .htm
2.3.3 测试配置
要测试 FastCGI 实现,在web的根目录下(nginx.conf配置文件里server块的root项)创建包含如下信息的文件: test.php
<?php phpinfo();
在浏览器中导航此文件,您应该会看到包含当前 PHP 配置的信息页面。
http://127.0.0.1/test.php
PHP Version 8.0.6
System Linux tompc 5.12.8-arch1-1 #1 SMP PREEMPT Fri, 28 May 2021 15:10:20 +0000 x86_64
...
3. Apache
https://httpd.apache.org/docs/2.4/
https://wiki.apache.org/httpd/
https://wiki.apache.org/httpd/CommonMisconfigurations
https://wiki.archlinux.org/title/Apache_HTTP_Server
LAMP: 是指在许多web 服务器上使用的一个软件组合:Linux,Apache,MySQL/MariaDB以及PHP。
Apache HTTP 服务器,简称 Apache,是非常流行的Web服务器软件。
本文档描述了怎样安装设置 Apache 网页服务器。以及选择安装 PHP 并集成到Apache服务器中。
3.1 安装和启动
extra/apache 2.4.47-1 [installed]
$ sudo systemctl start httpd
3.2 配置及验证
对于简单的设置,默认配置文件应该没问题。默认情况下,它会将目录/srv/http提供给访问您网站的任何人。
$ sudo systemctl start httpd
Apache 现在应该正在运行。通过在 Web 浏览器中访问http://localhost/。它应该显示一个简单的索引页面。
http://localhost/
3.2.1 选项
请参阅Apache 配置指令的完整列表和指令快速参考。
- https://httpd.apache.org/docs/trunk/mod/directives.html
- https://httpd.apache.org/docs/trunk/mod/quickreference.html
常用设置: /etc/httpd/conf/httpd.conf
- User http: 出于安全原因,若 Apache 由 root 用户(直接或通过启动脚本)启动,它就会切换到此 UID。默认用户是http,它是在安装过程中自动创建的。
- Listen 80: 这是 Apache 将侦听的端口。使用路由器访问互联网,您必须转发端口。 如果您想为本地开发设置 Apache,您可能希望它只能从您的计算机访问。 然后将此行更改为Listen 127.0.0.1:80.
- ServerAdmin you@example.com: 这是管理员的电子邮件地址,可以在例如错误页面上找到。
- DocumentRoot "/srv/http": 这是您放置网页的目录。可修改,但不要忘记也更改<Directory "/srv/http">,否则可能会收到403错误缺乏权限)。 将Require all denied行更改为Require all granted,或收到403。 注: DocumentRoot 目录及其父文件夹必须允许其他人执行权限(chmod o+x /path/to/DocumentRoot),否则您将收到403错误。
- AllowOverride None: <Directory>部分中的此指令会导致 Apache 完全忽略.htaccess文件。 请注意,这现在是 Apache 2.4 的默认设置,因此如果您打算使用.htaccess文件,则需要明确允许覆盖。 如果您打算mod_rewrite在.htaccess文件中使用或 其他设置,您可以允许该文件中声明的哪些指令可以覆盖服务器配置。
更多设置: /etc/httpd/conf/extra/httpd-default.conf
- ServerSignature Off: 关闭服务器的签名
- ServerTokens Prod: 隐藏 Apache 和 PHP 版本等服务器信息
3.2.2 检查配置
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
3.3 用户目录
如果您不希望用户目录在 Web 上可用,请在/etc/httpd/conf/httpd.conf中注释掉以下行:
Include conf/extra/httpd-userdir.conf
$ chmod o+x ~
$ chmod o+x ~/public_html
$ chmod -R o+r ~/public_html
$ ls -l /home/
drwxr-xr-x 21 tom users 4096 Jun 2 14:16 tom
$ ls -l /home/tom/
drwxr-xr-x 2 tom users 4096 Jun 2 14:17 public_html
$ ls -l /home/tom/public_html/
-rw-r--r-- 1 tom users 17 Jun 2 13:54 test.php
-rwxr-xr-x 1 tom users 48 Jun 2 08:17 tom.html
重新启动httpd.service以应用任何更改。另请参阅Umask#Set the mask value。
https://wiki.archlinux.org/title/Umask#Set_the_mask_value
注: 不必+x为每个用户都设置,通过 ACL 只为 web 服务器设置它就足够了(请参阅访问控制列表#授予 web 服务器私有文件的执行权限)。
https://wiki.archlinux.org/title/Access_Control_Lists#Granting_execution_permissions_for_private_files_to_a_web_server
3.4 扩展PHP
3.4.1 安装php-fpm
注意: 与 ProxyPass 的广泛设置不同,SetHandler 的代理配置尊重其他 Apache 指令,如 DirectoryIndex。这确保了与为 libphp、mod_fastcgi 和 mod_fcgid 设计的软件更好的兼容性。如果您仍想尝试 ProxyPass,请尝试如下一行:
ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/run/php-fpm/php-fpm.sock|fcgi://localhost/srv/http/$1
/etc/httpd/conf/httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
DirectoryIndex index.php index.html
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php-fpm/php-fpm.sock|fcgi://localhost/"
</FilesMatch>
并将其包含在以下内容的底部/etc/httpd/conf/httpd.conf:
Include conf/extra/php-fpm.conf
注意: sock和fcgi之间不允许有空格!localhost可以用任何字符串替换。
更多配置在这里: /etc/php/php-fpm.d/www.conf,但默认设置应该可以正常工作。
启动并启用php-fpm.service. 重新启动 httpd.service。
3.4.2 测试PHP是否有效
要测试 PHP 是否已正确配置,请在您的 ApacheDocumentRoot目录(例如/srv/http/或~<username>/public_html/)中创建一个文件test.php,其中包含以下内容:
<?php phpinfo();
然后根据需要转到http://localhost/test.php或 http://localhost/~<username>/test.php
http://127.0.0.1/~tom/
http://127.0.0.1/~tom/test.php
