安装nginx(与Apache共存)在CentOS7上
这是在CentOS7上安装nginx(EngineX)时的备忘录。
这次我们将使用最简单的方法来实现,而不使用最新版本。
此外,由于CentOS7已经安装了Apache,所以我们将尝试在不同的端口上共存。
标准的操作是添加最新的软件仓库,但这次我们将跳过这一步,安装旧版本。
以下是可供參考的資料。
-
- 【入門】Nginx(エンジンエックス)の特徴・デメリット、インストールと初期設定も解説
- Nginx を CentOS 7 にインストールする手順
安装
首先安装nginx。
# yum install nginx
2023年1月3日时点,最新版本是1.23.3,但实际安装的是1.20.1。
# nginx -version
nginx version: nginx/1.20.1
设置
接下来,将配置文件(/etc/nginx/nginx.conf)的监听端口从80修改为9999。
# vi /etc/nginx/nginx.conf
将 “listen” 两处更改为 80 到 9999。
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 9999; # <- 80から9999に変更
listen [::]:9999; # <- 80から9999に変更
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
3. 开始运行
最后启动服务。
# systemctl start nginx
用Web浏览器访问指定的端口。
http://127.0.0.1:9999/
如果页面显示,则表示成功。最后保持持久运行状态。
# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
结束了。