LAMP

关于作者

周李杰,最后一批90后,现居湖北武汉

孤独患者,懒癌中期,无脑幻想者。但却人畜无害,无需远离。

兴趣众多,然无一精通。正努力成为一个有趣的人。

1
2
> print("👆以上是不完整的我") //请我吃大餐将会了解更多
>

[TOC]

LAMP

LAMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写。

虽然这些开放源代码程序本身并不是专门设计成同另几个程序一起工作的,但由于它们的廉价和普遍,这个组合开始流行(大多数Linux发行版本捆绑了这些软件)。当一起使用的时候,它们表现的像一个具有活力的“解决方案包”(Solution Packages)。其他的方案包有苹果WebObjects(最初是应用服务器),Java/J2EE微软.NET架构。

“LAMP包”的脚本组件中包括了CGIweb接口,它在90年代初期变得流行。这个技术允许网页浏览器的用户在服务器上执行一个程序,并且和接受静态的内容一样接受动态的内容。程序员使用脚本语言来创建这些程序因为它们能很容易有效的操作文本流,甚至当这些文本流并非源自程序自身时也是。正是由于这个原因系统设计者经常称这些脚本语言为胶水语言

Michael Kunze在一篇为德国计算机杂志《c’t》(1998,第12期,230页)而写的文章中使用了缩略语“LAMP”。这篇文章意在展示一系列的自由软件成为了商业包的替换物。由于IT世界众所周知的对缩写的爱好,Kunze提出“LAMP”这一容易被市场接受的术语来普及自由软件的使用。

web服务

WEB服务器也称为WWW(WORLD WIDE WEB,万维网)服务器,主要功能是提供网上信息浏览服务。

常见web服务器(web):

httpd(apache)、nginx+PHP

tomcat: jsp + htm

win:IIS

客户端:IE 、firefox、chrome、手机(浏览器)Browser

Apache

Apache HTTPD Server 简称 Apache,是 Apache 软件基金会的一个开源的网页服务器, 可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的 Web 服务器端软件之一。它快速、可靠并且可通过简单的 API 扩展,将 Perl/Python 等解释器编 译到服务器中!Apache HTTP 服务器是一个模块化的服务器,各个功能使用模块化进行插拔! 目前支持 Windows,Linux,Unix 等平台!

Apache 软件基金会(也就是 Apache Software Foundation,简称为 ASF),是专门为运 作一个开源软件项目的 Apache 的团体提供支持的非盈利性组织,这个开源软件项目就是 Apache 项目!那么我们的 HTTPD 也只是 Apache 的开源项目之一!

主要的开源项目:HTTP Server,Ant,DB,iBATIS,Jakarta,Logging,Maven,Struts, Tomcat,Tapestry,Hadoop 等等。只是最有名的是 HTTP Server,所以现在所说的 Apache 已 经就是 HTTPD Server 的代号了! 我们还见的比较多的是 Tomcat,Hadoop 等项目

官方网站:Apache

httpd:httpd.apache

MySQL

MySQL是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 旗下公司,他也是最流行的关系型数据库管理系统!特别是在 WEB 应用方面!

关系型数据库:它通过数据、关系和对数据的约束三者组成的数据模型来存放和管理数据

官方网站:mysql

PHP

PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通 用开源脚本语言。语法吸收了 C 语言、Java 和 Perl 的特点,利于学习,使用广泛,主要适 用于 Web 开发领域。PHP 独特的语法混合了 C、Java、Perl 以及 PHP 自创的语法。它可以 比 CGI 或者 Perl 更快速地执行动态网页。用 PHP 做出的动态页面与其他的编程语言相比, PHP 是将程序嵌入到 HTML(标准通用标记语言下的一个应用)文档中去执行,执行效率比 完全生成 HTML 标记的 CGI 要高许多;PHP 还可以执行编译后代码,编译可以达到加密和 优化代码运行,使代码运行更快。

简单的说,PHP 就是一个脚本解释器!

官方网站:php

LAMP工作原理

1566921823529

Web 服务器的工作模式和端口

工作模式是:B/S 模式

工作端口是:80/http 正常端口443/https SSL 端口

搭建网易yum源

准备之前我们需要搭建一下网易的yum源

1
2
3
4
5
> [root@server222 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
> [root@server222 ~]# cd /etc/yum.repos.d/
> [root@server222 yum.repos.d]# yum clean all
> [root@server222 yum.repos.d]# yum makecache
>

源码编译安装Apache

下载传到/usr/local/src/目录下面

安装gcc、pcre-devel

1
2
[root@server222 src]# yum install gcc -y
[root@server222 src]# yum install pcre pcre-devel -y

apr源码包编译

1
2
3
[root@server222 src]# tar -xzvf apr-1.6.5.tar.gz
[root@server222 src]# cd apr-1.6.5/
[root@server222 apr-1.6.5]# ./configure --prefix=/usr/local/apr ; make ; make install

apr-util源码包编译

1
2
3
4
[root@server222 apr-1.6.5]# cd ..
[root@server222 src]# tar -xzvf apr-util-1.6.1.tar.gz
[root@server222 src]# cd apr-util-1.6.1/
[root@server222 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr ; make ; make install

如果在编译途中遇到这样的错误:

1
2
3
4
5
6
7
8
> 编译中断。
> make[1]: *** [xml/apr_xml.lo] 错误 1
> make[1]: 离开目录“/usr/local/src/apr-util-1.6.1”
> make: *** [all-recursive] 错误 1
> /bin/sh /usr/local/apr/build-1/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/usr/local/src/apr-util-1.6.1/include -I/usr/local/src/apr-util-1.6.1/include/private -I/usr/local/apr/include/apr-1 -o xml/apr_xml.lo -c xml/apr_xml.c && touch xml/apr_xml.lo
> xml/apr_xml.c:35:19: 致命错误:expat.h:没有那个文件或目录
> #include <expat.h>
>

请运行yum install expat-devel -y 这么一个命令

再次编译就好了

创建用户与组

1
2
[root@server222 apr-util-1.6.1]# groupadd -r apache
[root@server222 apr-util-1.6.1]# useradd -r -g apache apache -s /sbin/nologin

httpd源码包编译

1
2
3
4
[root@server222 apr-util-1.6.1]# cd ..
[root@server222 src]# tar -xzvf httpd-2.4.38.tar.gz
[root@server222 src]# cd httpd-2.4.38/
[root@server222 httpd-2.4.38]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork

若遇到这种情况:

1
2
3
4
> configure: WARNING: OpenSSL version is too old
> no
> checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures
>

请执行yum install openssl-devel -y一条命令

再次执行上一步编译

1
[root@server222 httpd-2.4.38]# make ; make install

启动服务

1
2
3
4
5
6
7
8
[root@server222 httpd-2.4.38]# /usr/local/apache/bin/apachectl
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::1bd0:d77:f2c2:6f3. Set the 'ServerName' directive globally to suppress this message
[root@server222 httpd-2.4.38]# /usr/local/apache/bin/apachectl
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::1bd0:d77:f2c2:6f3. Set the 'ServerName' directive globally to suppress this message
httpd (pid 114412) already running
# 此处出来这么一大串鬼东西,其实httpd已经在运行了
[root@server222 httpd-2.4.38]# netstat -antup | grep httpd
tcp6 0 0 :::80 :::* LISTEN 114412/httpd

此处教一下字符界面的浏览器,命令yum install elinks.x86_64 -y

1
2
[root@server222 httpd-2.4.38]# yum install elinks.x86_64 -y
[root@server222 httpd-2.4.38]# elinks 192.168.176.222

第一次访问点击OK,之后就可以看到页面显示“It works!”,到此处 Apache搭建完成

添加systemd的服务脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@server222 httpd-2.4.38]# cd /usr/lib/systemd/system
[root@server222 system]# vim httpd.service
[root@server222 system]# cat httpd.service
# 添加内容如下
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=simple
ExecStart=/usr/local/apache/bin/httpd -DFOREGROUND
ExecReload=/usr/local/apache/bin/httpd -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}

KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target

启动服务测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@server222 system]# systemctl start httpd
[root@server222 system]# systemctl restart httpd
[root@server222 system]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 五 2019-08-30 20:31:20 CST; 1s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 114744 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
Main PID: 114756 (httpd)
CGroup: /system.slice/httpd.service
└─114756 /usr/local/apache/bin/httpd -DFOREGROUND

8月 30 20:31:20 server222 systemd[1]: Started The Apache HTTP Server.
8月 30 20:31:20 server222 systemd[1]: Starting The Apache HTTP Server...

YUM安装LAMP环境

LAMP=Linux(存在)+Apache(HTTPD 已安装)+Mysql+PHP

1
2
3
4
[root@server222 src]# yum -y install httpd mariadb mariadb-server php php-mysql
[root@server222 src]# systemctl start mariadb.service
[root@server222 src]# systemctl enable mariadb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

初始化数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
[root@server222 src]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

测试数据库

1
2
3
4
5
6
7
8
9
10
11
[root@server222 src]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit
Bye

测试apache是否支持PHP

1
2
3
4
5
6
7
8
[root@server222 src]# cd /var/www/html/
[root@server222 html]# vim index.php
[root@server222 html]# cat index.php
<?php
phpinfo();
?>
[root@server222 html]# systemctl restart httpd
[root@server222 html]# elinks 192.168.176.222

修改配置文件,实战举例

例1: 搭建一台测试web服务器

案例:

部门内部搭建一台WEB服务器,采用的IP地址和端口为192.168.0.63:80,首页采用index.html

文件。管理员E-mail地址为 775531910@qq.com,网页的编码类型采用UTF-8,所有网站资源都存放在/var/www/html目录下,并将Apache的配置文件根目录设置为/etc/httpd目录。

修改配置文件

1
2
3
4
5
6
7
8
[root@server222 html]# vim /etc/httpd/conf/httpd.conf
31 ServerRoot "/etc/httpd" #apache配置文件的根目录
42 Listen 80 #监听的端口
86 ServerAdmin r775531910@qq.com #设置管理员,e-mail 地址
95 #ServerName www.example.com:80 #服务器主机名
119 DocumentRoot "/var/www/html" #网站页面根目录
144 Options Indexes FollowSymLinks #当一个目录没有默认首页时,允许显示此目录列表
164 DirectoryIndex index.html index.php #指定默认首页

取消apache默认欢迎页:

1
2
3
4
5
6
[root@server222 html]# vim /etc/httpd/conf.d/welcome.conf
# 将下面的四行注释掉
#<LocationMatch "^/+$">
# Options -Indexes
# ErrorDocument 403 /.noindex.html
#</LocationMatch>

创建首页:

1
2
3
[root@server222 html]# systemctl restart httpd.service
[root@server222 html]# echo 'welcome to haahahahahah775531910' > /var/www/html/index.html
[root@server222 html]# systemctl restart httpd.service

测试

1
[root@server222 html]# elinks 192.168.176.222
打赏
  • © 2019-2020 Li Jie
  • Powered by Hexo Theme Ayer
    • PV:
    • UV:

喜欢就打赏吧~

支付宝
微信