本文主要介绍了在华为云上如何使用弹性云服务器的Linux实例手工搭建LNMP平台的web环境。该指导具体操作以CentOS 7.2 64位操作系统为例。本文主要适用 CentOS 7,centos 通常自带的yum源提供的各种软件包都是比较老的版本, 如果想用yum安装最新的软件,推荐第三源ius,通过添加 IUS 软件源手动安装尽可能新的 LNMP 建站环境。对于新手,建议在全新系统环境下操作,并先运行 yum -y update
更新系统软件包,以及一些可选系统基础配置。
华为云官网
点击直达华为云活动页面
前言
注 1:IUS 软件包名称带版本号,随着时间流逝,文中安装命令请相应修改,以确保安装最新版软件。
注 2:IUS 跟随上游软件支持周期,当某个版本结束支持后,IUS 会在一个月后将其移至存档库。如果要安装已结束支持的软件版本,请使用 yum --enablerepo="ius-archive" install php70u-fpm
安装命令(自行替换其中软件包名,下同)。如果要在存档库里搜索软件,则用 yum --enablerepo="ius-archive" search all php70u
命令。
注 3:为了方便,下文使用了 sed
命令修改配置文件。这种修改方式依赖匹配文本,如果文本有差别可能导致修改失败。建议在部署生产环境时用编辑器手动修改,或在修改后验证修改,以免漏掉配置。
添加 IUS 软件源
IUS 里有许多软件包与 EPEL 存在依赖项,因此需要同时添加 EPEL 源。
# 添加 EPEL 软件源 yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm # 添加 IUS 软件源 yum -y install https://repo.ius.io/ius-release-el7.rpm
添加后可以用下面命令检查软件源启用情况。
# 安装查询工具 yum -y install yum-utils # 列出所有软件源 yum repolist all # 列出已启用软件源 yum repolist enabled # 列出已禁用软件源 yum repolist disabled
安装 Nginx
EPEL 软件源里有 Nginx,并且版本不是太旧,这里直接从 EPEL 安装。
yum -y install nginx
安装后做以下基本配置。
修改 /etc/nginx/nginx.conf
配置文件。在 http{...}
内添加下面红色参数,以关闭 Nginx 版本号输出,延长 fastcgi 进程超时时间(避免 504 Gateway Time-out 问题),以及设置合适 buffer 值。
http { ... sendfile on; tcp_nopush on; tcp_nodelay on; server_tokens off; fastcgi_read_timeout 300s; fastcgi_buffer_size 16k; fastcgi_busy_buffers_size 24k; fastcgi_buffers 8 16k; ... }
继续修改 server{...}
,拒绝通过服务器 IP 访问 Web 默认目录。
server { listen 80 default_server; listen [::]:80 default_server; server_name _; return 403; # root /usr/share/nginx/html; }
修改 FastCGI 配置文件 SERVER_SOFTWARE
参数,删除版本号输出。
sed -i 's|nginx/$nginx_version|nginx|' /etc/nginx/fastcgi.conf sed -i 's|nginx/$nginx_version|nginx|' /etc/nginx/fastcgi_params
firewall-cmd --state
检查是否安装启用防火墙。如果没有,用下面命令安装启用。
yum -y install firewalld systemctl start firewalld systemctl enable firewalld
防火墙放行 HTTP
HTTPS
端口。
firewall-cmd --permanent --zone=public --add-service={http,https} firewall-cmd --reload
创建一个 nginx 配置文件,方便后续申请 Let’s Encrypt 证书使用。
# 创建 nginx 片段配置存储目录 mkdir -p /etc/nginx/snippets # 创建 Let’s Encrypt 证书申请目录验证配置文件 cat > /etc/nginx/snippets/letsencrypt-acme-challenge.conf