LAMP环境搭建与配置

  • 基本概念

LAMP是 Linux  Apache  MySQL  PHP 的简写,LAMP一般用来组件Web应用平台

Apache是最常用的Web服务软件。

MySQL是小型的数据库软件。

PHP是主要用于服务器端的应用程序软件,是一种脚本语言。

注意:Apache和PHP必须装在一台机器上,因为PHP是作为Apache的一个模块存在的。

  • 安装MySQL
# 源码包不区分平台,二进制包是区分平台的
# CentOS 7之前是区分32位和64位的,CentOS 7开始都是64位

uname -i         # 查看Linux是多少位的

1)下载源码包或免编译二进制包

cd /usr/local/src           # 所有软件包都放到这一目录下
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz  # 下载二进制包

2)初始化

tar zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz      # 解压
[ -d /usr/local/mysql ] && mv /usr/local/mysql /usr/local/mysql_old # 若该目录已存在则重命名
mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql      # 挪动位置

useradd -s /sbin/nologin mysql      # 不能用mysql用户登录系统,只能用来启动mysql数据库
cd /usr/local/mysql
mkdir -p /data/mysql    # 创建datadir,数据库文件会放到这里
chown -R mysql:mysql /data/mysql    # 更改权限,不更改后续操作就会出问题

./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
# 如提示错误信息“please install the following Perl modules before ...”
# 使用命令 yum install -y perl-Module-Install 来安装它

3)配置MySQL

# 1)复制并编辑配置文件
cp support-files/my-default.cnf /etc/my.cnf
vim /etc/my.cnf
# 2)配置文件改成如下内容
[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
server_id = 128             # 这个参数用在做主从配置,后续会介绍
socket = /tmp/mysql.sock    # 定义MySQL服务监听的socket套接字地址

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
join_buffer_size = 128M         # 这3个buffer是关于内存的配置参数,保持默认即可
sort_buffer_size = 2M
read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# 3)复制启动脚本并修改其属性
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
vim !$
# 启动脚本续修改的内容如下
basedir=/usr/local/mysql
datadir=/data/mysql
# 4)将启动脚本加入系统服务项,并将其设为开机启动
chkconfig --add mysqld      # 把mysqld加到系统服务列表中
chkconfig mysqld on         # 使其开机启动
service mysqld start        # 启动mysqld服务

ps aux |grep mysqld         # 查看进程
netstat -lnp |grep 3306     # 查看3306端口监听情况
  • 安装Apache
# 1)下载并解压源码包
cd /usr/local/src       # httpd是依赖apr和apr-util的
wget http://mirrors.cnnic.cn/apache/apr/apr-1.6.3.tar.gz
wget http://mirrors.cnnic.cn/apache/apr/apr-util-1.6.1.tar.gz
wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.33.tar.gz

tar zxvf apr-1.6.3.tar.gz
tar zxvf apr-util-1.6.1.tar.gz
tar zxvf httpd-2.4.33.tar.gz
# 2)安装apr和apr-util
cd /usr/local/src/apr-1.6.3
./configure --prefix=/usr/local/apr     # --prefix制定安装目录
make && make install 

cd /usr/local/src/apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install 
# 安装apr-util时如出现错误“libtool: link: cannot find the library ...”
# 解决办法是: yum install -y expat-devel
# 3)继续安装httpd

cd /usr/local/src/httpd-2.4.33

./configure 
    --prefix=/usr/local/apache2.4 
    --with-apr=/usr/local/apr 
    --with-apr-util=/usr/local/apr-util 
    --enable-so            # 表示启用DSO,DSO是把某些功能以模块的形式展现出来
    --enable-mods-shared=most       # 以共享方式安装大多数功能模块,安装后会在modules目录下看到这些文件

yum install -y pcre pcre-devel      # 为避免make出错,提前安装好一些库文件

make
make install 
echo $?

# ls /usr/local/apache2.4/modules/    # .so文件就是模块,之前定义了most参数,所以会有这么多模块
# /usr/local/apache2.4/bin/apachectl -M # 查看已加载的模块
  • 安装PHP
# 1)下载并解压源码包

cd /usr/local/src
wget http://cn2.php.net/distributions/php-5.6.32.tar.bz2
tar jxvf php-5.6.32.tar.bz2
# 2)配置编译参数

cd php-5.6.32
./configure 
    --prefix=/usr/local/php 
    --with-apxs2=/usr/local/apache2.4/bin/apxs 
    --with-config-file-path=/usr/local/php/etc 
    --with-mysql=/usr/local/mysql 
    # apxs为httpd的一个工具,因为有它才会自动把PHP模块安装到httpd的modules目录下
    # 以上4个参数为必选
    # 下面的参数比较常用,没特殊要求直接使用这些参数即可,也可根据具体需要的功能来来选择所需参数
    --with-libxml-dir 
    --with-gd 
    --with-jpeg-dir 
    --with-png-dir 
    --with-freetype-dir 
    --with-iconv-dir 
    --with-zlib-dir 
    --with-bz2 
    --with-openssl 
    --enable-soap 
    --enable-gd-native-ttf 
    --enable-mbstring 
    --enable-sockets 
    --enable-exif
# 配置编译参数时可能会遇到如下错误,请执行对应的操作
'''
configure: error: xml2-config not found. Please check your libxml2 installation.
'''
yum install -y libxml2-devel

'''
configure: error: Cannot find OpenSSL's <evp.h>
'''
yum install -y openssl openssl-devel

'''
configure: error: Please reinstall the BZip2 distribution
'''
yum install -y bzip2 bzip2-devel

'''
configure: error: png.h not found
'''
yum install -y libpng libpng-devel

'''
configure: error: freetype.h not found
'''
yum install -y freetype freetype-devel

'''
configure: error: mcrypt.h not found. Please reinstall libmcrypt
'''
yum install -y epel-release
yum install -y libmcrypt-devel
# CentOS默认的yum源没有libmcrypt-devel这个包,所以只能借助epel yum扩展源
# 3)编译和安装并复制配置文件

make
make install
# make时间会在5分钟以上,执行echo $?查看make是否成功,如有报错,安装缺少的库即可

copy php.ini-production /usr/local/php/etc/php.ini      # 复制配置文件
  • 配置httpd支持PHP
# httpd的主配置文件为/usr/local/apache2.4/conf/httpd.conf,编辑该文件
vim /usr/local/apache2.4/conf/httpd.conf

# 1)搜索ServerName,把#ServerName www.exaple.com:80前面的井号删除
ServerName www.example.com:80

# 2)搜索Require,将Require all denied 改为Require all granted
<Directory />
    AllowOverride none
    Require all granted
</Directory>
# 改它的目的是允许所有请求,否则访问时会报403错误

# 3)搜索AddType application/x-gzip .gz .tgz,在该行下面添加一行
AddType application / x - compress.Z
AddType application / x - gzip.gz.tgz
AddType application / x - httpd - php.php

# 4)搜索DirectoryIndex index.html,将其改为DirectoryIndex index.html index.php
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>
  • 测试LAMP是否成功

1)测试httpd配置文件是否正确

# 启动httpd之前要先检验配置文件是否正确,如正确会显示"Syntax OK"
/usr/local/apache2.4/bin/apachectl -t

# 启动httpd
/ust/local/apache2.4/bin/apachectl start

# 查看httpd是否启动
netstat -lnp |grep httpd        # 显示如下表示OK
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      31881/httpd

# 也可使用curl命令来测试
curl localhost      # 显示如下表示OK
<html><body><h1>It works!</h1></body></html>

2)测试是否正确解析PHP

# 编写如下测试脚本
vim /usr/local/apache2.4/htdocs/1.php   # 写入如下内容
<?php
    echo "php is OK";
?>

# 保存脚本后继续测试
curl localhost/1.php        # 显示如下说明PHP解析正确
php is OK
  • LAMP环境配置
'''
LAMP环境搭建好后,其实仅仅是安装上了软件,还有很多集体的配置要做。包括httpd配置和PHP配置,如下:

1. httpd配置
    1.1 默认虚拟主机
    1.2 用户认证
    1.3 域名跳转
    1.4 访问日志
    1.5 静态元素过期时间
    1.6 防盗链
    1.7 访问控制
2. PHP配置
    2.1 disable_functions
    2.2 error_log
    2.3 open_basedir

# 详细配置命令,请听下回分解! 

'''
原文地址:https://www.cnblogs.com/karl-python/p/9094126.html