Apache 安装和使用
Apache 可以在 macOS、Linux 和 Windows 等操作系统上安装使用。这里以 macOS 为例:
- 使用 Homebrew 安装:
brew install httpd - 修改配置文件:
vim /usr/local/etc/httpd/httpd.conf
找到 DirectoryIndex 选项,取消注释并修改为:
DirectoryIndex index.html - 启动Apache:
httpd
或在后台运行:
brew services start httpd - 在网站根目录下创建 index.html 文件,然后可以访问 http://localhost 看到测试页面。
- 停止Apache: Ctrl + C 或运行 brew services stop httpd。
Nginx 安装和使用
Nginx 也可以运行在多操作系统上,这里同样以 macOS 为例:
- 使用 Homebrew 安装:
brew install nginx - 修改配置文件:
vim /usr/local/etc/nginx/nginx.conf
找到 server 块,修改 root 指向网站根目录:
server {
listen 80;
root /path/to/website;
} - 启动Nginx:
nginx
或在后台运行:
brew services start nginx - 在网站根目录下创建 index.html 文件,然后可以访问 http://localhost 看到测试页面。
- 停止Nginx:
运行 nginx -s stop 或 brew services stop nginx。
Apache 和 Nginx 都是强大稳定的 Web 服务器,在实际使用过程中,可以根据需求选择使用其中一个或同时使用。
脚本安装nginx:
#!/bin/bash
cd /usr/local/src
yum install wget vim unzip pcre pcre-devel gcc* -y
yum install openssl openssl-devel -y
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar xf nginx-1.18.0.tar.gz
cd nginx-1.18.0/
./configure --prefix=/data/app/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module
make && make install
#mkdir /data/app/nginx -p
#ln -s /application/nginx-1.18.0/ /application/nginx
echo 'export PATH=/data/app/nginx/sbin:$PATH' >>/etc/profile
source /etc/profile
cd /data/app/nginx/conf
mkdir conf.d ssl
nginx -t
netstat -lntup|grep nginx
在主配置文件中,include 所有的虚拟主机配置:
include /data/app/nginx/conf/conf.d/*.conf;
include /data/app/nginx/conf/conf.d/*/*.conf;