Centos 7 yum 安装LAMP

作者:闫涛

E-mail:coderyantao@qq.com

备注:实验环境为虚拟机,selinux、firewalld都已经关闭

Apache 2.4 官方文档https://httpd.apache.org/docs/2.4/

#查看端口
[root@localhost ~]# firewall-cmd --query-port=80/tcp
no#开启端口
[root@localhost ~]# firewall-cmd --permanent --add-port=80/tcp
success#重新加载规则
[root@localhost ~]# firewall-cmd --reload
success#再次查看
[root@localhost ~]# firewall-cmd --query-port=80/tcp
yes

1. Apache

1.1 删除原有yum源,更换阿里源

[root@localhost ~]# rm -rf /etc/yum.repos.d/*
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# yum clean all
[root@localhost ~]# yum makecache

1.2 安装httpd

[root@localhost ~]# yum install httpd

1.3 启动httpd

[root@localhost ~]# systemctl start httpd.service

1.4 设置httpd开机启动

[root@localhost ~]# systemctl enable httpd.service

1.5 备份、编辑httpd配置文件

[root@localhost ~]# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
#修改服务器名,不改也可以,但是总是报一个 AH00558 提示
#ServerName www.example.com:80
修改为
ServerName www.example.com:80
或者
ServerName localhost:80
#优先解析php文件
<IfModule dir_module>DirectoryIndex index.html
</IfModule>
#修改为
<IfModule dir_module>DirectoryIndex index.php  index.html
</IfModule>

1.6 检查配置文件语法

[root@localhost ~]# httpd -t
Syntax OK

1.7 重启httpd

[root@localhost ~]# systemctl restart httpd.service

2. PHP

2.1 安装epel源和ius源

ius源依赖epel源

[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# wget https://centos7.iuscommunity.org/ius-release.rpm
[root@localhost ~]# ls
anaconda-ks.cfg  ius-release.rpm
[root@localhost ~]# yum install ius-release.rpm
[root@localhost ~]# yum clean all
[root@localhost ~]# yum makecache
#查看yum源是否都已安装
[root@localhost ~]# ls /etc/yum.repos.d/
CentOS-Base.repo  epel.repo  ius-archive.repo  ius.repo  ius-testing.repo

2.2 安装php7.2

[root@localhost ~]# yum install php72u*

2.3 创建php文件

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim index.php

输入下面内容

<?phpphpinfo();

重启apache,浏览器输入ip即可看见php信息。

3. MySQL

可参考 https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/

3.1 下载、安装mysql的yum源

[root@localhost ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
[root@localhost ~]# yum install mysql80-community-release-el7-3.noarch.rpm
#/etc/yum.repos.d/ 目录下就会生成 mysql-community.repo

3.2 mysql的yum文件

这个yum源默认是开启了mysql8,下面介绍开启其他版本的方法

[root@localhost ~]# yum list |grep mysql-community-server
mysql-community-server.x86_64             8.0.19-1.el7                 mysql80-community
[root@localhost ~]# vim /etc/yum.repos.d/mysql-community.repo

想要下载那个版本,将 enabled=1 即可

# Enable to use MySQL 5.5
[mysql55-community]
name=MySQL 5.5 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

我安装的是5.6版本

[root@localhost ~]# yum list |grep mysql-community-server
mysql-community-server.x86_64             5.6.47-2.el7                 mysql56-community

3.3 安装mysql

[root@localhost ~]# yum install mysql-community-server.x86_64

3.4 启动mysql

[root@localhost ~]# systemctl start mysqld.service

3.5 开机自启

[root@localhost ~]# systemctl enable mysqld.service

3.6 初始化

5.7版本以下使用这个

[root@localhost ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQLSERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, 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 MySQL
root user without the proper authorisation.Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..... Success!By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL 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] y... 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] y... Success!By default, MySQL 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] y- Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist... Failed!  Not critical, keep moving...- 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] y... Success!All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.Thanks for using MySQL!Cleaning up...

3.7 创建远程登录用户

mysql>  create user 'yantao'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql>  grant all on *.* to 'yantao'@'%';
Query OK, 0 rows affected (0.00 sec)

4. Apache实验

1. 虚拟主机

可参考 https://httpd.apache.org/docs/2.4/zh-cn/vhosts/name-based.html

Apache 不支持多个https的虚拟主机

1.1 准备两个域名

www.web1.com

www.web2.com

1.2 生成主机目录

在网站根目录下创建l和t两个目录和index.html

[root@localhost ~]# mkdir /var/www/html/web1
[root@localhost ~]# vim /var/www/html/web1/index.html
#例如下入 www.web1.com[root@localhost ~]# mkdir /var/www/html/web2
[root@localhost ~]# vim /var/www/html/web2/index.html
#例如下入 www.web2.com

1.3 修改配置文件

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
#最下面这行代码为开启加载conf.d/下的子配置文件
IncludeOptional conf.d/*.conf
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim web1.conf
#写入下面内容
<VirtualHost *:80>ServerName www.web1.comDocumentRoot "/var/www/html/web1"
</VirtualHost>[root@localhost conf.d]# vim web2.conf
<VirtualHost *:80>ServerName www.web2.comDocumentRoot "/var/www/html/web2"
</VirtualHost>

1.4 检查语法

[root@localhost ~]# httpd -t
Syntax OK

1.5 重启apache

[root@localhost ~]# systemctl restart httpd.service

1.6 修改电脑的hosts文件

#添加如下内容
192.168.1.49    www.l.com
192.168.1.49    www.t.com

这是用可以用浏览器访问这两个域名了

2.隐藏版本号

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
#在最下面添加如下内容,隐藏Apache版本
ServerTokens Prod
ServerSignature Off#隐藏php版本
[root@localhost ~]# vim /etc/php.ini
expose_php = On
#修改如下
expose_php = Off

3.用户认证

参考 https://docs.huihoo.com/apache/httpd/2.0-cn/howto/auth.html

先新建好一个web3.com

3.1生成账号文件

新建目录存放账号文件
[root@localhost ~]# mkdir /etc/httpd/passwd
#创建账号文件
[root@localhost ~]# htpasswd -c /etc/httpd/passwd/web3 yantao
New password:
Re-type new password:
Adding password for user yantao

3.2修改虚拟主机文件

[root@localhost ~]# vim /etc/httpd/conf.d/web3.conf
<Directory /var/www/html/web3>AuthType BasicAuthName "Restricted Files"AuthUserFile /etc/httpd/passwd/web3Require user yantao
</Directory>
<VirtualHost *:80>ServerName www.web3.comDocumentRoot "/var/www/html/web3"
</VirtualHost>

检查语法、重启

4.域名跳转

让web2跳转到web1

4.1修改web2虚拟主机文件

[root@localhost ~]# vim /etc/httpd/conf.d/web2.conf
#如下
<Directory /var/www/html/web2>Options Indexes FollowSymLinksAllowOverride AllRequire all granted
</Directory><VirtualHost *:80>ServerName www.web2.comDocumentRoot "/var/www/html/web2"
</VirtualHost>

4.2在web2目录下创建.htaccess

[root@localhost ~]# vim /var/www/html/web2/.htaccess
#如下
#开启rewrite功能
RewriteEngine on#把www.web2.com 开头的内容赋值给HTTP_HOST变量
RewriteCond %{HTTP_HOST} ^www.web2.comRewriteRule ^(.*) http://www.web1.com/$1 [R=permanent,L]
# ^(.*)$ 代指客户端要访问的资源
# $1 把 .* 所指代的内容赋值给 $1 变量中
# Rpermanent 永久重定向 = 301
# L 指定该规则为最后一条生效的规则,下面的不再生效

检查语法、重启

5.https

a:Https确保所有经过服务器传输的数据包都是经过加密的。

b:建立了一个官方数字证书证书,使得假冒服务器无法冒充真正的服务器。

证书来源

1.自签名证书被推荐用于测试目的和个人项目。自签名证书,也可以用于服务提供商,不过一般适用于用户互相信任的情形。另外,自签名证书不用花钱购买。

2.证书可以由社区为基础的认证供应商如StartSSL和CACERT办法。这些证书也不需要花钱,但建议为个人项目。

3.对于全球性商业网站,建议从值得信赖的知名证书颁发机构购买证书。这些证书需要花钱,但他们增加了网络服务提供商的信誉。

我们采用自签名证书,openssl用来生成证书(已安装),mod_ssl实现https(未安装)

安装mod_ssl

[root@localhost ~]# yum install mod_ssl
#安装之后就会在/etc/httpd/modules生成mod_ssl.so

5.1生成私钥

#生成2048位的加密私钥
[root@localhost ~]# openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
............................................+++
.......................................+++
e is 65537 (0x10001)

5.2生成证书签名请求

[root@localhost ~]# openssl req -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
#国家
Country Name (2 letter code) [XX]:CN
#省份
State or Province Name (full name) []:LN
#城市
Locality Name (eg, city) [Default City]:YS
#公司
Organization Name (eg, company) [Default Company Ltd]:CompanyName
#部门
Organizational Unit Name (eg, section) []:PHP
#域名
Common Name (eg, your name or your server's hostname) []:www.web1.com
Email Address []:Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

5.3生成自签名证书

#生成类型为X509的自签名证书。有效期设置3650天,即有效期为10年
[root@localhost ~]# openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=/C=CN/ST=LN/L=YS/O=CompanyName/OU=PHP/CN=www.web1.com
Getting Private key

5.4将三个文件放到对应目录

[root@localhost ~]# cp server.crt /etc/pki/tls/certs/
[root@localhost ~]# cp server.key /etc/pki/tls/private/
[root@localhost ~]# cp server.csr /etc/pki/tls/private/

5.5修改ssl.conf

#SSLCertificateFile /etc/pki/tls/certs/localhost.crt
#SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
修改如下
SSLCertificateFile /etc/pki/tls/certs/server.crt
SSLCertificateKeyFile /etc/pki/tls/private/server.key

5.6虚拟主机配置文件

[root@localhost ~]# vim /etc/httpd/conf.d/web1.conf
<VirtualHost *:443>SSLEngine onSSLCertificateFile "/etc/pki/tls/certs/server.crt"SSLCertificateKeyFile "/etc/pki/tls/private/server.key"DocumentRoot /var/www/html/web1ServerName www.web1.com
</VirtualHost>

配好之后检查语法,重启apache

5.7开启443端口

方法同上

访问 https://www.web1.com/ 选择信任证书即可浏览网站

5.8强制跳转https

[root@localhost ~]# vim /etc/httpd/conf.d/web1.conf
<VirtualHost *:80>ServerName www.web1.comRedirect permanent / https://www.web1.com/
</VirtualHost><VirtualHost *:443>SSLEngine onSSLCertificateFile "/etc/pki/tls/certs/server.crt"SSLCertificateKeyFile "/etc/pki/tls/private/server.key"DocumentRoot /var/www/html/web1ServerName www.web1.com
</VirtualHost>

6.静态缓存

[root@localhost ~]# vim /etc/httpd/conf.d/web1.conf
<VirtualHost *:80>ServerName www.web1.comRedirect permanent / https://www.web1.com/
</VirtualHost><VirtualHost *:443>SSLEngine onSSLCertificateFile "/etc/pki/tls/certs/server.crt"SSLCertificateKeyFile "/etc/pki/tls/private/server.key"DocumentRoot /var/www/html/web1ServerName www.web1.com
</VirtualHost><IfModule mod_expires.c>ExpiresActive onExpiresByType image/jpeg "access plus 24 hours"ExpiresByType image/png "access plus 24 hours"ExpiresByType text/css  "now plus 2 hours"ExpiresByType application/javascript "now plus 2 hours"
</IfModule>

Centos 7yum安装LAMP相关推荐

  1. linux - centos yum安装LAMP环境

    1. centos 7.2 一般机器都带yum命令,并且yum包源都是可以用的,直接yum -y install 后面加上你所需要安装的软件,他会自动下载自动安装,非常方便. 例如 yum -y in ...

  2. centos 一键安装lamp

    适用环境: 系统支持:CentOS-5 (32bit/64bit).CentOS-6 (32bit/64bit) 内存要求:≥256M 安装了什么: 1.Apache 2.2.22或Apache 2. ...

  3. CentOS一键安装LAMP

    用环境: §                                系统支持:CentOS-5(32bit/64bit).CentOS-6 (32bit/64bit) §            ...

  4. Centos 7 安装LAMP环境

    一.安装Centos 官网下载Centos 7刻录成光盘后安装 二.安装apache yum install httpd #根据提示,输入Y安装即可成功安装 systemctl start httpd ...

  5. linux yum lamp环境,linux centos yum安装LAMP环境

    4.2 安装mysql yum install mysql mysql-server mysql-devel 同样,完成后,用/etc/init.d/mysqld start 启动mysql 配置my ...

  6. 在docker centos 6.8安装lamp脚本

    #!/bin/bash #在docker centos:6安装lamp脚本 #renphp  20160706 #一安装更改yum源 yum install -y wget yum install - ...

  7. CentOS 6.5系统使用yum方式安装LAMP环境和phpMyAdmin,mysql8.0.1/mysql5.7.22+centos7,windows mysql安装、配置...

    介绍如何在CentOs6.2下面使用YUM配置安装LAMP环境,一些兄弟也很喜欢使用编译的安装方法,个人觉得如果不是对服务器做定制,用yum安装稳定简单,何必去download&make&am ...

  8. centos 7 安装 mantisbt-2.12.0 —— 安装LAMP环境、安装mantisbt-2.12.0

    一.安装LAMP环境 1 二. 环境的启动 6 三. Mantis数据库的创建 6 四. 安装mantis-2.12.0 9 1>安装配置 9 2>登录管理员账号 13 3>新建用户 ...

  9. 使用阿里云CentOS安装LAMP时,安装PHP扩展需要注意的事情

    2019独角兽企业重金招聘Python工程师标准>>> 阿里云的CentOS中,repo是更改过的,有安装epel的repo,之前使用命令: yum install httpd ph ...

最新文章

  1. 14Facade(门面)模式
  2. 第四章第五章 环境搭建和24个命令总结
  3. 第八篇:Docker镜像结构原理
  4. 1.3 Quick Start中 Step 3: Create a topic官网剖析(博主推荐)
  5. python教程:apscheduler模块使用教程
  6. 红黑树的原理_红黑树插入算法实现原理分析
  7. Android引入library失败的可能原因
  8. python 文本处理2
  9. VMware日志收集方法总结
  10. 分形图案c语言源程序,Mandlbrot集图形的一个C语言实现
  11. IE报错,VS定位不到错误的常见原因
  12. dll文件编写、查看和调用
  13. 软考计算机网络与最大流量,软考程序员考点计算机网络之性能
  14. voip|网络电话,软件实现电信座机
  15. 微信支付证书如何部署在linux,微信支付平台证书更新指引
  16. 09——规范数据库设计
  17. 一周市场摘抄20210208
  18. 学算法怎么样?算法工程师薪资前景好吗?
  19. 地图定位之3D 地图
  20. 兄弟hl3150cdn打印测试页6_打印性能测试:LED高效输出_兄弟 3150CDN_办公打印评测试用-中关村在线...

热门文章

  1. Array.slice 的常用用法
  2. 计算机基础应用期末卷,计算机应用基础期末试卷(B卷)
  3. Hypermesh案例学习笔记
  4. 企业终端安全问题的痛点分析
  5. 信息系统安全实验——Week 8
  6. python画魔法阵_半年了,这画我已经找不出什么出错的地方了,诚心请大佬们指点,我如何能更进一步?...
  7. 软管金属穿线管蛇形管免焊接式M12航空插头螺丝压紧锁线
  8. CTF编码和加密总结
  9. 如何通俗的理解函数的极限_如何理解函数的极限?
  10. 服务器做网站空间,服务器做网站空间