Centos6 RPM包搭建LAMP环境

一、LAMP环境搭建的前期准备

1.1关闭防火墙和SELinux

如果您对于防火墙及SELinux很熟悉可以略过以下步骤,如果您并不熟悉防火墙及SELinux,请按照以下步骤关闭防火墙及SEKLinux。
注意:在生产服务器上,防火墙是不能直接关闭的,而是需要设置防火墙允许 80 端口(apache)、3306 端口(mysql)和 11211(memcache)端口访问的!
关闭命令如下:

[root@localhost ~]# setup

在图 4 所示的界面中选择“防火墙配置”,进入防火墙配置界面。
图4,防火墙配置
在图 5 所示的界面中,按空格键把“启用”前面的“*”去掉,选择“确定”保存,默认防火墙规则就会被关闭。
图5,防火墙配置
再查询一下防火墙规则:

[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
#默认filter表中的三条链中的规则都是空。防火墙已经被关闭了

除关闭防火墙之外,我们还需要关闭 SELinux 安全子系统。因为这个子系统会对我们的安装过程产生影响,所以先关闭它。
关闭命令如下:

[root@localhost ~]# vi /etc/selinux/config
#使用vi编辑器编辑配置文件
#操作:把 SELINUX=enforcing 改为 SELINUX=disabled

注意:如图12,是“SELINUX”而不是“SELINIXTYPE”,改错配置文件将导致无法开机!
图12,关闭SELinux注意事项
vi编辑器使用小帮助:按“i”进入编辑状态,按“ESC”退出编辑状态。退出编辑状态下按“:”输入“wq”回车,为保存并退出vi编辑器。输入“q”为退出vi编辑器,输入“q!”为强制退出vi编辑器。

[root@localhost ~]# shutdown -r now
#重启Linux系统

关闭 SELinux 之后,必须重启 Linux 系统,修改才会生效。

1.2配置网卡

:如果你还没有配置网卡的IP地址请按下面步骤配置一下,必须可以连接互联网!因为后面的步骤需要用到yum。
命令:

[root@localhost ~]#setup

如图6,我们选择第三项“Network configuration”,回车。
图6,配置网卡
如图7,我们选择第一项“Device configuration”,回车。
图7,配置网卡
如图8,选择你所需要配置的网卡,回车。
图8,配置网卡
如图9,请填写你的IP地址、子网掩码、网关、DNS。
图9,配置网卡
:按空格键取消“Use DHCP”后面的“*”号,才能填写IP地址……等内容。
最后记得按空格键在“On boot”后面添加引号,否则不生效。
Tab键换行至“OK”回车结束。

如图10,Tab键换行至“Save”回车保存。
图10,配置网卡
如图11,Tab键换行至“Save&Quit”回车。
图11,配置网卡
最后还有一步,选择“quit”退出即可!(不上截图了)

1.3关闭不必要的服务

这既是服务器的必须优化步骤,也会使我们在 LAMP 环境搭建过程中的报错可能性大大降低。
具体方法:使用 ntsysv 命令把不需要的服务前面的“*”去掉,然后重启 Linux 服务器。
具体哪些服务是必须启动的,哪些服务是不必要的,大家可以参考http://c.biancheng.net/view/1059.html

[root@localhost ~]#ntsysv [--level 运行级别]

如:
#[root@localhost ~]# ntsysv --level 235
#只设定2、3、5级别的服务自启动
#[root@localhost ~]# ntsysv
#按默认的运行级别设置服务自启动

二、安装Apache

2.1安装apache

[root@localhost ~]# yum install httpd httpd-devel

:如果遇到错误“Cannot retrieve repository metadata (repomd.xml) for repository: base. Please verify its path and try again”请重新执行一下命令~

2.2启动apache服务

#Centos7请使用命令:
[root@nmserver-7 ~]# systemctl start  httpd.service
#Centos6请使用命令:
[root@localhost ~]# service httpd start

2.3设置httpd服务开机启动

#Centos7请使用命令:
[root@nmserver-7 ~]# systemctl enable  httpd.service
#Centos6请使用命令:
[root@localhost ~]# chkconfig httpd on
#注:也可以使用
[root@localhost ~]#ntsysv

2.4查看服务状态

#Centos7请使用命令:
[root@nmserver-7 ~]# systemctl status httpd.service
#Centos6请使用命令:
[root@localhost ~]# service httpd status

2.5浏览器访问(见图13)

图13,浏览器访问

三、安装MySQL

3.1安装mysql

如果已经安装了mysql,卸载命令:

rpm -e --nodeps mysql

开始安装!
查看yum上提供的数据库可下载版本:

[root@localhost ~]# yum list | grep mysql

安装命令:

[root@localhost ~]# yum -y install mysql-libs mysql mysql-server mysql-devel mariadb-server

查看mysql服务是否成功安装:

[root@localhost ~]# rpm -qa |grep mariadb

3.2开启mysql服务、设置开机自启动、检查mysql服务状态

3.2.1开启mysql服务

#Centos7请使用命令:
[root@nmserver-7 ~]# systemctl start mariadb.service
#Centos6请使用命令:
[root@localhost ~]# service mysqld start

3.2.2设置开机自启动

#Centos7请使用命令:
[root@nmserver-7 ~]# systemctl enable mariadb.service
#Centos6请使用命令:
[root@localhost ~]# chkconfig mysqld on

3.2.3检查mysql服务状态

#Centos7请使用命令:
[root@nmserver-7 ~]# systemctl status mariadb.service
#Centos6请使用命令:
[root@localhost ~]# service mysqld status

3.2.4显示网络信息

-t (tcp)仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-l 仅列出有在 Listen (监听) 的服务状态
-p 显示建立相关链接的程序名

[root@localhost ~]# netstat -tulp

3.3数据库安全设置

[root@nmserver-7 ~]# mysql_secure_installation 
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
#注意:建议所有MariaDB运行此脚本的所有部分服务器在生产中使用!请仔细阅读每一步!

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.
#为了登录MariaDB来保护它,我们现在需要根用户的密码。如果你刚刚安装了MariaDB
你还没有设置根密码,密码将是空的,这里按回车。

Enter current password for root (enter for none): 
OK, successfully used password, moving on...
#为root用户输入当前密码(为none输入):
成功使用密码,继续…

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
#设置根密码可以确保没有人可以登录到MariaDB,没有适当授权的根用户。

Set root password? [Y/n] y
#设置root密码?[Y / n] Y

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.
#默认情况下,MariaDB安装有一个匿名用户,
允许任何人使用它登录MariaDB,而不需要创建任何用户帐户给他们。
这仅为了使测试和安装稍微顺一点。你应该在搬进数据之前把它们搬走生产环境。

Remove anonymous users? [Y/n] y
 ... Success!
#删除匿名用户?[Y / n] Y
…成功!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
#通常,应该不允许root从'localhost'连接。这确保无法从网络猜出根密码。

Disallow root login remotely? [Y/n] n
 ... skipping.
#不允许root远程登录?[Y / n] n
…跳过。

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.
#默认情况下,MariaDB提供了一个名为“test”的数据库,任何人都可以使用访问。
这也只是为了测试,应该删除,在转移到生产环境之前。

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
#删除测试数据库并访问它?[Y / n] Y
- 删除测试数据库...
...成功!
- 删除测试数据库上的特权...
...成功!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
#重新加载特权表将确保到目前为止所做的所有更改
将立即生效。

Reload privilege tables now? [Y/n] y
 ... Success!
#现在重新加载特权表?[Y / n] Y
...成功!

Cleaning up...
#清理...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
#全部完成!如果你已经完成了以上所有的步骤,你的MariaDB安装现在应该是安全的。

Thanks for using MariaDB!
#感谢使用MariaDB!

3.4登录数据库测试

[root@nmserver-7 ~]# mysql -uroot -p

Enter password: 
#输入密码:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
#欢迎来到MariaDB monitor。命令以;结尾;或\ g。
#您的MariaDB连接id是11

Server version: 5.5.52-MariaDB MariaDB Server
#服务器版本:5.5.52-MariaDB MariaDB服务器

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
#版权所有(c) 2000, 2016, Oracle, MariaDB Corporation Ab等

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#键入“help;”或“\h”以寻求帮助。键入“\c”以清除当前输入语句。

MariaDB [(none)]> show databases;
#MariaDB[(无)]>显示数据库;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.02 sec)
#3行(0.02秒)

MariaDB [(none)]>

四、安装PHP7

4.1安装php7

如果您安装了旧版本php,请卸载:
列出已安装的PHP

[root@localhost ~]#  rpm -qa | grep php

卸载已安装的PHP

[root@localhost ~]# yum remove php*

4.2安装最新的yum源

(一般国内的yum源中的php都版本较旧)

#CentOS 6 / RHEL 6
yum install http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
#CentOS 7 / RHEL 7
yum install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

查看yum源中的php

[root@localhost ~]# yum list|grep php*

4.3安装php7

yum --enablerepo=remi,remi-php70 install php php-cgi php-cli php-fpm php-common php-devel php-mysqlnd php-mysql php-sqlite3 php-mbstring php-msgpack php-mcrypt php-bcmath php-gd php-xml php-ldap php-xmlrpc php-opcache php-curl php-json php-odbc php-pdo php-bz2 php-xml php-ftp php-imap php-snmp php-redis php-memcached

验证安装:

[root@localhost ~]# php -v

4.4测试PHP

[root@nmserver-7 ~]# cd  /var/www/html/
#位置为,你的网页文件存放目录
[root@nmserver-7 html]# ls
[root@nmserver-7 html]# pwd
/var/www/html
[root@nmserver-7 html]# vi info.php

<?php
        phpinfo();
?>
~                                                                                        
~                                                                                        
~                                                                                        
~                                                                                        
~                                                                                        
~                                                                                        
~                                                                                        
~                                                                            
:wq

4.5重启apache服务器

#Centos7请使用命令:
[root@nmserver-7 html]# systemctl restart httpd.service
#Centos6请使用命令:
[root@localhost html]# service httpd restart

4.6测试php

浏览器输入:
[服务器ip地址]/info.php
如:192.168.8.88/info.php
你可以看到已安装的模块

到此,LAMP环境已经安装完成啦!

本文内容参考:
http://c.biancheng.net
https://home.cnblogs.com/u/me80/
https://blog.csdn.net/jxpxlinkui/article/details/79639501
https://www.cnblogs.com/feixiablog/p/9018819.html
https://www.cnblogs.com/junw_china/articles/1708967.html
https://www.cnblogs.com/sunshenggang/p/10816464.html
https://www.cnblogs.com/itcomputer/p/4632116.html
https://www.tangshuang.net/1765.html
http://c.biancheng.net/view/817.html
https://www.cnblogs.com/sirdong/p/12053429.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Deng_Xian_Shemg

捐助1元钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值