前言
LAMP 是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写
L:Linux 操作系统
A:Apache(httpd) 网页服务
M:MySQL(mariadb) 数据库服务
P:php/perl/python/ruby 脚本编程语言
本文主要以centos 7的环境下进行安装,centos 6兼带部分说明
一、http2.4的安装
Centos 7默认安装httpd 2.4,Centos 6默认安装httpd2.2
Centos 7:如果未安装http2.4,则通过yum安装
yum -y install httpd
Centos 6:只能通过编译安装
事先须安装Development Tools和Server Platform Development两个包组,同时还需要安装prce-devel程序包,prce包可通过yum安装
yum -y groupinstall "Development tools" yum -y groupinstall "Server Platform Development" yum -y prce-devel
httpd2.4需要1.4以上的版本apr和apr-util,故先get两者的源码包
apr的编译安装
tar xf apr-1.4.6.tar.bz2 -C /usr/local //将源码包tar至/usr/local目录之下 cd /usr/local/apr-1.4.6 ./configure --prefix=/usr/local/apr //对源码进行配置,指定安装目录 make && make install //对源码进行编译安装
apr-util的编译安装
tar xf apr-util-1.4.1.tar.bz2 -C /usr/local cd /usr/local/apr-util-1.4.1/ ./configure --prefix=/usr/local/apr-util make && make install
httpd的编译安装
tar xf httpd-2.4.10.tar.bz2 -C /usr/local cd /usr/local/httpd-2.4.10 ./configure --prefix=/usr/local/apache //指定安装目录 --sysconfdir=/etc/httpd24 //指定配置文件目录 --enable-so //启用模块功能 --enable-ssl //启用ssl加密功能 --enable-cgi //启用cgi功能 --enable-rewrite //可重载 --with-zlib //使用zlib数据 --with-pcre /使用pcre数据 --with-apr=/usr/local/apr //所关联程序apr及目录 --with-apr-util=/usr/local/apr-util //所关联程序apr-util及目录 --enable-modules=most //最大程度启用所有模块 --enable-mpms-shared=all //分离列出所有MPM模块 --with-mpm=perfork //使用perfork模式 make && make install
其中./configure的相关参数可以使用./configure --help查看
以上为分列出各参数意义,故在排版上进行了分割,在配置中相关参数用空格分隔
二、mariadb的安装
Centos 6中默认使用的为MySQL,在Centos 7中默认使用的mariadb
mariadb的安装
yum -y install mariadb-server.x86_64 systemctl start mariadb //启动mariadb服务
MySQL的安装
yum -y install mysql-server service mysqld start
mariadb配置文件:/etc/my.cnf,/etc/my.cnf.d/*.cnf
修改/etc/my.cnf文件
加入以下值
innodb_file_per_table = ON //生成数据库列表 skip_name_resolve = ON //禁止反解
安装完成后,运行一次mysql_secure_installation,对mariadb进行配置。
[root@chunlanyy ~]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS 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] //是否设置root用户密码,输入y并回车 New password: //设置root用户的密码 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] //是否删除匿名用户,删除 … 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] //是否禁止root远程登录,禁止 … 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] //是否删除test数据库,删除 - 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 MySQL installation should now be secure. Thanks for using MySQL!
进入mysql进行配置
相关格式:
mysql>GRANT ALL ON db_name.tbl_name To username@'host'IDENTIFIED BY "password"
GRANT ALL ON testdb.* TO 'root'@'172.16.%.%' IDENTIFIED BY "chunlanyy";
设置用户及密码
FLUSH PRIVILEGES; //清除权限 CREATE DATABASE testab; //创建testab数据表单
三、php的安装
yum -y install php
编译安装:
四、LAMP环境测试:
因为httpd作为http协议实现的服务器,只能静态的响应和处理客户端的请求,为了使得服务器能动态的响应客户端的请求,有三种方法实现:
1)使用CGI协议:httpd服务器通过CGI协议将请求转发至程序的解释器,解释器将运行结果返回httpd服务器,随后解释器销毁
2)使用module,把php编译成httpd的扩展模块,通过httpd动态调用模块来实现
3)fastCGI:通过fpm(fastcgi process manager)让php单独运行一个类型apache的模型服务,监听某个套接字,并生成多个子进程来处理响应,而主进程只负责管理请求和控制子进程的运行状况,此时http变成fastCGI的客户端,而fastCGI变成一个简化版的http协议使php和http进行通信使用。
php和http常用的结合方式通过编译httpd的php处理模块,让httpd自己处理php程序,或者使用专门的php应用程序服务器php-fpm,由它来负责处理php程序。
a.php与httpd结合的测试
修改之前配置好的虚拟主机的数据文件
将/data/virhost/www1/index.html更改为/data/virhost/www1/index.php
vim /data/virhost/www1/index.php
将内容修改如下php测试代码
<php? phpinfo(); ?>
重启httpd服务
systemctl restart httpd
通过浏览器访问172.16.45.21,即可看到phpinfo页面
其中虚拟主机的配置文件/etc/httpd/conf.d/virhost1.conf内容如下:
<VirtualHost *:80> servername www1.chunlanyy.com documentroot "/data/virhost/www1" <Directory "/data/virhost/www1"> options none allowoverride none require all granted </Directory> </VirtualHost>
[root@chunlanyy modules]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.16.45.21 netmask 255.255.0.0 broadcast 172.16.255.255
b.php连接mysql的测试
在前文已经配置好mariadb的情况下
将上述文件vim /data/virhost/www1/index.php内容修改如下:
<?php $conn = mysql_connect('172.16.45.21','root','chunlanyy'); if ($conn) echo "OK"; else echo "Failure"; ?>
当启动php服务时,通过浏览器访问172.16.45.21时,显示ok表明连接正常
systemctl stop php显示Failure