阿里云 ECS 服务器 CentOS 7.4 搭建 LAMP WordPress 环境:Apache HTTP Server

二、搭建 WordPress 环境

安装 HTTPs

首先
HTTPS = HTTP + SSL 证书

HTTP

Apache HTTP Server 安装前准备

Step 01

// 临时关闭防火墙
[root@~]# systemctl stop firewalld      

Step 02

// 临时 关闭 SELinux
[root@~]# setenforce 0

网上查过,会导致 安全狗 + Apache 无法重启的情况,而且它设置起来也麻烦,但是我还没有感受到它的麻烦,所以我选择临时关闭,而不是永久关闭。

Apache HTTP Server 安装与配置

Step 00

查看 CentOS 有没有 EPEL 扩展库,扩展源后面的 Certbot / MySQL / PHP 的安装也都要用到

[root@~]# ls /etc/yum.repos.d/ | grep "epel"
epel.repo

如果没有安装, EPEL 源,目前最新是 7-12 ,

按照这个路径查找 7 -> x86_64 -> Packages -> e, 直接搜索 epel-release-

epel.png

[root@~]# wget https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm

[root@~]# ls
epel-release-7-11.noarch.rpm

引入库

[root@~]# sodu yum -y localinstall epel-release-7-11.noarch.rpm

查看库

[root@~]# ls /etc/yum.repos.d | grep "epel"
epel.repo
epel.repo.rpmnew
epel-testing.repo

[root@~]# yum repolist enabled | grep "epel"
!epel/x86_64                       Extra Packages for Enterprise Linux 7  12,663

Step 01

yum 进行安装,或者直接在命令行上使用 man yum 查看命令的使用方式

安装

// -y 是指所有的问题都填 yes
[root@~]# sudo yum -y install httpd
......
Total                                   13 MB/s | 2.8 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : httpd-tools-2.4.6-80.el7.centos.1.x86_64            1/2 
  Installing : httpd-2.4.6-80.el7.centos.1.x86_64                  2/2 
  Verifying  : httpd-tools-2.4.6-80.el7.centos.1.x86_64            1/2 
  Verifying  : httpd-2.4.6-80.el7.centos.1.x86_64                  2/2 

Installed:
  httpd.x86_64 0:2.4.6-80.el7.centos.1                                     

Dependency Installed:
  httpd-tools.x86_64 0:2.4.6-80.el7.centos.1                                  

Complete!

Step 02

查看安装的包 yum 或 rpm

// 指令
[root@~]# yum list installed | grep "httpd"
httpd.x86_64                        2.4.6-80.el7.centos.1           @updates    
httpd-tools.x86_64                  2.4.6-80.el7.centos.1           @updates 

[root@~]# rpm -qa | grep httpd
httpd-2.4.6-80.el7.centos.1.x86_64
httpd-tools-2.4.6-80.el7.centos.1.x86_64

// 设备目录的 httpd
[root@~]# ls /etc/ | grep httpd
httpd

Step 03

查看 Apache 服务 systemctl :

[root@~]# systemctl list-unit-files httpd.service
UNIT FILE     STATE   
httpd.service disabled

1 unit files listed.

如果是 disable 状态,要先 enable 服务

[root@~]# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service
to /usr/lib/systemd/system/httpd.service.

[root@~]# systemctl list-unit-files httpd.service
UNIT FILE     STATE  
httpd.service enabled

1 unit files listed.

启动 Apache 服务

[root@~]# systemctl start httpd.service

查看启动状态:

[root@~]# systemctl -l status httpd.service 
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; 
           enabled; vendor preset: disabled)
   Active: active (running) since Wed 2018-09-12 10:45:52 CST; 5min ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 29191 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─29191 /usr/sbin/httpd -DFOREGROUND
           ├─29192 /usr/sbin/httpd -DFOREGROUND
           ├─29193 /usr/sbin/httpd -DFOREGROUND
           ├─29194 /usr/sbin/httpd -DFOREGROUND
           ├─29195 /usr/sbin/httpd -DFOREGROUND
           └─29196 /usr/sbin/httpd -DFOREGROUND

Sep 12 10:45:52 izwz9dnfbgdn5tleje5eitz systemd[1]:
  Starting The Apache HTTP Server...
Sep 12 10:45:52 izwz9dnfbgdn5tleje5eitz httpd[29191]: 
  AH00558: httpd: Could not reliably determine the server's fully 
  qualified domain name, using 172.18.236.230. 
  Set the 'ServerName' directive globally to suppress this message
Sep 12 10:45:52 izwz9dnfbgdn5tleje5eitz systemd[1]:
  Started The Apache HTTP Server.

修复错误

AH00558: httpd: Could not reliably determine the server's 
fully qualified domain name, using 私网 IP. 
Set the 'ServerName' directive globally to suppress this message

打开配置文件

[root@~]# vim /etc/httpd/conf/httpd.conf

找到 ServerName 并修改

ServerName

重新启动服务 systemctl restart httpd.service

[root@ ~]# systemctl restart httpd.service

[root@ ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; 
           enabled; vendor preset: disabled)
   Active: active (running) since Wed 2018-09-12 11:20:51 CST; 7s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 29334 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
 Main PID: 29339 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─29339 /usr/sbin/httpd -DFOREGROUND
           ├─29340 /usr/sbin/httpd -DFOREGROUND
           ├─29341 /usr/sbin/httpd -DFOREGROUND
           ├─29342 /usr/sbin/httpd -DFOREGROUND
           ├─29343 /usr/sbin/httpd -DFOREGROUND
           └─29344 /usr/sbin/httpd -DFOREGROUND

Sep 12 11:20:51 xxx systemd[1]: Starting The Apache HTTP Server...
Sep 12 11:20:51 xxx systemd[1]: Started The Apache HTTP Server.

查看 Apache 的监听是否启动:

[root@~]# netstat -tnlp | grep ':80\>'
tcp        0      0 0.0.0.0:80          0.0.0.0:*          LISTEN      29339/httpd 

查看 Apache 的服务进程是否启动:

[root@~]# ps -axu | grep 'httpd\>' | grep -v '\<grep\>'
root   29339  0.0  0.1 224020  4952 ?   Ss  11:20  0:00 /usr/sbin/httpd -DFOREGROUND
apache 29340  0.0  0.0 224020  2952 ?   S   11:20  0:00 /usr/sbin/httpd -DFOREGROUND
apache 29341  0.0  0.0 224020  2952 ?   S   11:20  0:00 /usr/sbin/httpd -DFOREGROUND
apache 29342  0.0  0.0 224020  2952 ?   S   11:20  0:00 /usr/sbin/httpd -DFOREGROUND
apache 29343  0.0  0.0 224020  2952 ?   S   11:20  0:00 /usr/sbin/httpd -DFOREGROUND
apache 29344  0.0  0.0 224020  2952 ?   S   11:20  0:00 /usr/sbin/httpd -DFOREGROUND

Step 04

浏览器访问服务器

服务器实例 公网 IP 或 域名

Screenshot of 云服务器管理控制台 ips.jpg

在浏览器网址栏处输入服务器的 公网 IP 地址 或 域名,成功访问 Apache 会出现如下页面:

Apache HTTP Server Test Page powered by CentOS

如果不想打开浏览器查看可以安装 elinks (elinks 指令使用) 来查看:

[root@~]# yum -y install elinks

elinks 查看,Ctrl +C 强制退出

[root@~]# elinks 公网 IP
elinks Test 1234

直接鼠标点击 OK 就可以了,因为现在什么都没配置,肯定有问题的; 可以上下滚动查看里面的内容;按 q 键就可以弹出退出对话框了。

如果无法访问,在阿里云服务器上添加安全组:

云服务器管理控制台
云服务器管理控制台
云服务器管理控制台

继续进行相关配置

Screenshot of Apache HTTP Server Test Page powered by CentOS settings

网站的内容都在 /var/www/html 里面

查看网站 home 目录 ( grepsedawk )

[root@~]# ls -la /var/www/html/

重点 重点 重点

查看 Apache 的设置项,grep -v "^ *#" 去除 # 开头的行, grep -v "^$" 去除空行

[root@~]# cat /etc/httpd/conf/httpd.conf | grep -v "^ *#" | grep -v "^$"

Apache 的所有模块 Modules ,如:dir_module 就属于 mod_dir 模块;log_config_module 属于 mod_log_config 模块.

Apache 的所有指令 Commands,如:ServerRoot, Listen, AllowOverride , Require, Directory ... 就属于指令.

[root@~]# cat /etc/httpd/conf/httpd.conf | grep -v "^ *#" | grep -v "^$"

ServerRoot "/etc/httpd"              // Apache 的安装目录
Listen 80                            /* Apache 监听的 IP 地址和端口号,HTTP 端口号是 80
                                        HTTPS 是 443 */
Include conf.modules.d/*.conf        // 引入其它配置文件
User apache                          // 服务器应答请求(requests)的进程用户 id
Group apache                         // 服务器应答请求(requests)的进程组
ServerAdmin root@localhost           // 邮件地址,服务器会把错误信息发送到这个邮件地址中
ServerName 私网 IP : 80              // 服务器名
<Directory />                        // 服务器根目录的访问设置
    AllowOverride none               // none 忽略 .htaccess 文件(AllowOverrideList 
                                        也设置成了 `None`,那么服务器会完全忽略 
                                        .htaccess 文件), all 允许有 .htaccess 文件上
                                        下文的目录访问 .htaccess 文件 
    Require all denied               // granted 表示无条件运行所有访问,
                                        denied 表示无条件拒绝所有访问
</Directory>
DocumentRoot "/var/www/html"         // 网站的 home 目录
<Directory "/var/www">               // "/var/www" 目录文件访问设置
    AllowOverride None               // none 忽略 .htaccess 文件(AllowOverrideList 也
                                        设置成了 `None`,那么服务器会完全忽略 .htaccess 
                                        文件), all 允许有 .htaccess 文件上下文的目录访
                                        问 .htaccess 文件
    Require all granted              // granted 表示无条件运行所有访问,
                                        denied 表示无条件拒绝所有访问
</Directory>
<Directory "/var/www/html">          // "/var/www/html" 目录文件访问设置
    Options Indexes FollowSymLinks   // 设置可用的属性
    AllowOverride None               // none 忽略 .htaccess 文件(AllowOverrideList 也
                                        设置成了 `None`,那么服务器会完全忽略 .htaccess 
                                        文件), all 允许有 .htaccess  文件上下文的
                                        目录访问 .htaccess 文件
    Require all granted              // granted 表示无条件运行所有访问,
                                        denied 表示无条件拒绝所有访问
</Directory>
<IfModule dir_module>                // mod_dir 模块的标识符
    DirectoryIndex index.html        // 用于目录遍历的资源文件,多个文件用空格分隔
</IfModule>
<Files ".ht*">                       // 所有的 “.ht” 开头的文件
    Require all denied               // granted 表示无条件运行所有访问,  
                                        denied 表示无条件拒绝所有访问
</Files>
ErrorLog "logs/error_log"            // 服务器错误信息保存的路径
LogLevel warn                        // 控制 ErrorLog 信息的冗长程度
<IfModule log_config_module>         // mod_log_config 模块的标识符
    // 信息格式
    LogFormat 
    "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 
    LogFormat 
    "%h %l %u %t \"%r\" %>s %b" common 
    <IfModule logio_module>          // mod_logio 模块的标识符
      LogFormat 
      "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined // 设置文件名和信息格式
</IfModule>
<IfModule alias_module>              // mod_alias 模块标识符
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" // 构建文件映射
</IfModule>
<Directory "/var/www/cgi-bin"> 
    AllowOverride None
    Options None
    Require all granted
</Directory>
<IfModule mime_module>                   // mod_mime 模块的标识符,
                                            关联请求的文件的后缀
    TypesConfig /etc/mime.types          // mime.types 文件的路径
    AddType application/x-compress .Z    // .Z 后缀的文件映射到 application/x-compress
    AddType application/x-gzip .gz .tgz  // .gz .tgz 后缀的文件
                                            映射到 application/x-gzip
    AddType text/html .shtml             //  .shtml 后缀的文件映射到 text/html
    AddOutputFilter INCLUDES .shtml      // .shtml 后缀的文件映射到 INCLUDES 过滤器中,
                                            用于处理服务器的响应
</IfModule>
AddDefaultCharset UTF-8                  // 设置响应的 text/plain or text/html 
                                            文件的字符集类型
<IfModule mime_magic_module>             // mod_mime_magic 模块标识符,
                                            用于判断文件的 MIME 类型
    MIMEMagicFile conf/magic             // 基于 conf/magic 的文件来
                                            判断文件的 MIME 类型
</IfModule>
EnableSendfile on                        // 使用内核的发送服务来传输文件到客户端
IncludeOptional conf.d/*.conf

如果想快速浏览里面的内容可以使用 more 翻页查看,按 Space 空格键查看下一页

[root@~]# more -30 -s -u /etc/httpd/conf/httpd.conf
意思 意思 more more

完成基本的 httpd.conf 配置

ServerName: xxx@xxx.com // 邮箱

查看 welcome.conf 的文件内容,上面看到的欢迎页面就是这个文件配置出来的

[root@~]# cat /etc/httpd/conf.d/welcome.conf | grep -v "^ *#" | grep -v "^$"
<LocationMatch "^/+$">
    Options -Indexes
    ErrorDocument 403 /.noindex.html
</LocationMatch>
<Directory /usr/share/httpd/noindex>
    AllowOverride None
    Require all granted
</Directory>
Alias /.noindex.html /usr/share/httpd/noindex/index.html
Alias /noindex/css/bootstrap.min.css /usr/share/httpd/noindex/css/bootstrap.min.css
Alias /noindex/css/open-sans.css /usr/share/httpd/noindex/css/open-sans.css
Alias /images/apache_pb.gif /usr/share/httpd/noindex/images/apache_pb.gif
Alias /images/poweredby.png /usr/share/httpd/noindex/images/poweredby.png

我们需要自己的网页,所以可以注释掉 welcome.conf,vim 打开后,复制粘贴下面的命令就可以了

:8,22 s/^/#/g 添加 # 注释;
:8,22 s/^#//g 移除 # 注释;

动图演示

welcom.conf

修改完成后,重新刷新页面

welome nothing.png

什么也没有是因为,/var/www/html/ 文件夹下什么也没有,现在添加一个简单的 Hello Word !,并重启 httpd.service 服务

[root@~]# touch /var/www/html/index.html

[root@~]# echo 'Apache HTTP Server... Hello World !' > /var/www/html/index.html // 单引号哦

[root@~]# cat /var/www/html/index.html
Apache HTTP Server... Hello World !

[root@~]# systemctl restart httpd.service

刷新网页

apache hello world

扩展知识:

Apache : https://httpd.apache.org/docs/2.4/bind.html

Vim
如果文件没有高亮也没有行号的,在 /etc/vimrc 增加几行设置

[root@~]# vim /etc/vimrc
// 打开后, 在文件的末尾增加
" line number 行号
set nu 
" Syntax highlighting 代码高亮
syntax on
" tab 
set tabstop=4
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,185评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,652评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,524评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,339评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,387评论 6 391
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,287评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,130评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,985评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,420评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,617评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,779评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,477评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,088评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,716评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,857评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,876评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,700评论 2 354

推荐阅读更多精彩内容