lamp框架搭建

lamp

lamp简介

LAMP指的是Linux(操作系统)、Apache(HTTP服务器)、MySQL(也指MariaDB,数据库软件)和PHP(有时也是指Perl或Python)的首字母,一般用来建立web应用平台。

lamp平台构建

环境说明:

系统平台 IP 需要安装的服务
centos8
or
redhat8
192.168.102.134 httpd-2.4
mysql-5.7
php
php-mysql

lamp平台软件安装次序:

httpd --> mysql --> php

注意:php要求httpd使用prefork MPM

配置yum源

//下载阿里云官方centos源,然后把文件中的$releasever改成系统对应的版本
[root@cst yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

[root@cst yum.repos.d]# vim CentOS-Base.repo

:%s/$releasever/8/g

//下载阿里云官方epel源,并修改配置
[root@cst yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm

[root@cst yum.repos.d]# sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@cst yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@cst yum.repos.d]# sed -i 's/$releasever/8/g' epel*

//清理并重新生成缓存
[root@cst yum.repos.d]# yum clean all
[root@cst yum.repos.d]# yum makecache

安装httpd

//安装会用到的各种工具和其他依赖关系
[root@cst ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ wget make

//安装开发工具包
[root@cst ~]# yum -y groups mark install 'Development Tools'

//创建apache服务的用户和组
[root@cst ~]# useradd -r -M -s /sbin/nologin apache

//下载httpd、apr和apr-util
[root@cst ~]# wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.46.tar.bz2

[root@cst ~]# wget http://mirror.bit.edu.cn/apache/apr/apr-1.7.0.tar.bz2

[root@cst ~]# wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2

[root@cst ~]# ls
anaconda-ks.cfg    apr-util-1.6.1.tar.bz2
apr-1.7.0.tar.bz2  httpd-2.4.46.tar.bz2

[root@cst ~]# tar xf httpd-2.4.46.tar.bz2 
[root@cst ~]# tar xf apr-1.7.0.tar.bz2 
[root@cst ~]# tar xf apr-util-1.6.1.tar.bz2 
[root@cst ~]# ls
anaconda-ks.cfg    apr-util-1.6.1          httpd-2.4.46.tar.bz2
apr-1.7.0          apr-util-1.6.1.tar.bz2
apr-1.7.0.tar.bz2  httpd-2.4.46

//编译安装apr
[root@cst apr-1.7.0]# vim configure
   
    cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    $RM "$cfgfile"                 //删除或注释掉这一行

[root@cst apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@cst apr-1.7.0]# make && make install

//编译安装apr-util
[root@cst apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@cst apr-util-1.6.1]# make && make install

//编译安装httpd
[root@cst httpd-2.4.46]# ./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
[root@cst httpd-2.4.46]# make && make install

//设置环境变量和软链接
[root@cst ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@cst ~]# source /etc/profile.d/httpd.sh 
[root@cst ~]# which apachectl
/usr/local/apache/bin/apachectl
[root@cst ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@cst ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
[root@cst ~]# vim /etc/httpd24/httpd.conf
#ServerName www.example.com:80      //去掉注释

[root@cst ~]# apachectl restart
[root@cst ~]# ss -antl
State   Recv-Q   Send-Q      Local Address:Port     Peer Address:Port   
LISTEN  0        128               0.0.0.0:22            0.0.0.0:*      
LISTEN  0        128                     *:80                  *:*      
LISTEN  0        128                  [::]:22               [::]:*    

安装mysql

//下载mysql5.7的包
[root@cst ~]# ls
anaconda-ks.cfg    apr-util-1.6.1.tar.bz2
apr-1.7.0          httpd-2.4.46
apr-1.7.0.tar.bz2  httpd-2.4.46.tar.bz2
apr-util-1.6.1     mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
[root@cst ~]# tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@cst ~]# cd /usr/local/
[root@cst local]# ll
total 0
drwxr-xr-x. 13 root root  152 Jan  4 19:24 apache
drwxr-xr-x.  6 root root   58 Jan  4 19:15 apr
drwxr-xr-x.  5 root root   43 Jan  4 19:18 apr-util
drwxr-xr-x.  2 root root    6 Aug 12  2018 bin
drwxr-xr-x.  2 root root    6 Aug 12  2018 etc
drwxr-xr-x.  2 root root    6 Aug 12  2018 games
drwxr-xr-x.  2 root root    6 Aug 12  2018 include
drwxr-xr-x.  2 root root    6 Aug 12  2018 lib
drwxr-xr-x.  2 root root    6 Aug 12  2018 lib64
drwxr-xr-x.  2 root root    6 Aug 12  2018 libexec
drwxr-xr-x.  9 7161 31415 129 Jun  2  2020 mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x.  2 root root    6 Aug 12  2018 sbin
drwxr-xr-x.  5 root root   49 Dec 31 16:34 share
drwxr-xr-x.  2 root root    6 Aug 12  2018 src

//创建mysql系统账户
[root@cst local]# useradd -r -M -s /sbin/nologin mysql

//安装依赖文件
[root@cst local]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs

//做一个软链接方便使用,并修改属主和属组
[root@cst local]# ln -s mysql-5.7.31-linux-glibc2.12-x86_64 mysql
[root@cst local]# chown -R mysql.mysql mysql*
[root@cst local]# ll
total 0
drwxr-xr-x. 13 root  root  152 Jan  4 19:24 apache
drwxr-xr-x.  6 root  root   58 Jan  4 19:15 apr
drwxr-xr-x.  5 root  root   43 Jan  4 19:18 apr-util
drwxr-xr-x.  2 root  root    6 Aug 12  2018 bin
drwxr-xr-x.  2 root  root    6 Aug 12  2018 etc
drwxr-xr-x.  2 root  root    6 Aug 12  2018 games
drwxr-xr-x.  2 root  root    6 Aug 12  2018 include
drwxr-xr-x.  2 root  root    6 Aug 12  2018 lib
drwxr-xr-x.  2 root  root    6 Aug 12  2018 lib64
drwxr-xr-x.  2 root  root    6 Aug 12  2018 libexec
lrwxrwxrwx.  1 mysql mysql  35 Jan  4 20:27 mysql -> mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x.  9 mysql mysql 129 Jun  2  2020 mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x.  2 root  root    6 Aug 12  2018 sbin
drwxr-xr-x.  5 root  root   49 Dec 31 16:34 share
drwxr-xr-x.  2 root  root    6 Aug 12  2018 src

//建立数据存放目录
[root@cst ~]# mkdir /opt/mydata
[root@cst ~]# chown -R mysql.mysql /opt/mydata/
[root@cst ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Jan  4 20:38 mydata

//添加环境变量
[root@cst ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@cst ~]# source /etc/profile.d/mysql.sh 
[root@cst ~]# which mysql
/usr/local/mysql/bin/mysql
[root@cst ~]# which mysqld
/usr/local/mysql/bin/mysqld

//初始化数据库
[root@cst ~]# mysqld --initialize --user=mysql --datadir=/opt/mydata
2021-01-04T12:53:48.553434Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-01-04T12:53:51.466480Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-01-04T12:53:51.682713Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-01-04T12:53:51.739461Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e52eae3a-4e8b-11eb-a2dd-000c29732351.
2021-01-04T12:53:51.740298Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-01-04T12:53:52.436005Z 0 [Warning] CA certificate ca.pem is self signed.
2021-01-04T12:53:52.948000Z 1 [Note] A temporary password is generated for root@localhost: J>elQw5FkqeJ   //这里是随机生成的临时密码

//生成配置文件
[root@cst ~]# vim /etc/my.cnf

[mysqld]
basedir = /usr/local/mysql
datadir = /opt/mydata
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mydata/mysql.pid
user = mysql
skip-name-resolve

//配置服务启动脚本
[root@cst ~]# cd /usr/local/mysql/support-files/
[root@cst support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@cst support-files]# cp mysql.server /etc/init.d/mysqld
[root@cst support-files]# vim /etc/init.d/mysqld 

basedir=/usr/local/mysql  //加上路径
datadir=/opt/mydata       //加上路径

//启动数据库并修改密码
[root@cst ~]# service mysqld restart 
 ERROR! MySQL server PID file could not be found!
Starting MySQL. SUCCESS! 
[root@cst ~]# mysql -uroot -p'J>elQw5FkqeJ'

mysql> set password = password('1234');
Query OK, 0 rows affected, 1 warning (0.01 sec)

//配置mysql
[root@cst ~]# ln -s /usr/local/mysql/include /usr/include/mysql/
[root@cst ~]# vim /etc/man_db.conf 

MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/mysql/man  //加上这一行

[root@cst ~]# vim /etc/ld.so.conf.d/mysql.conf

/usr/local/mysql/lib  //写上这一行
[root@cst ~]# ldconfig

安装php

//安装所需的依赖关系
[root@cst ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd

//yum安装php
[root@cst ~]# yum -y install php-*

//设置监听端口号


listen = 127.0.0.1:9000

//启动服务
[root@cst ~]# systemctl start php-fpm.service

配置apache

//启用代理模块

在apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩展,因此,这两个模块都要加载,编辑httpd.conf文件,取消以下两行内容的注释:

*   LoadModule proxy_module modules/mod_proxy.so
*   LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

[root@cst ~]# cd /etc/httpd24/
[root@cst httpd24]# vim httpd.conf

//创建虚拟主机并生成php测试页面
[root@cst ~]# cd /usr/local/apache/htdocs/
[root@cst htdocs]# ls
index.html
[root@cst htdocs]# mkdir test
[root@cst htdocs]# ls
index.html  test
[root@cst htdocs]# cd test/
[root@cst test]# vim index.php

<?php
    phpinfo();
?>

[root@cst ~]# chown -R apache.apache /usr/local/apache/htdocs/

//修改配置文件
[root@cst ~]# vim /etc/httpd24/httpd.conf
在最后一行加上如下内容
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/test"
    ServerName www.wangqing.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/test/$1
    <Directory "/usr/local/apache/htdocs/test">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>

搜索AddType
AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php        //添加此行
    AddType application/x-httpd-php-source .phps        //添加此行

搜索index.html,在前面加上index.php
<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

//重启apache
[root@cst ~]# apachectl restart
[root@cst ~]# ss -antl
State   Recv-Q   Send-Q      Local Address:Port     Peer Address:Port   
LISTEN  0        128               0.0.0.0:22            0.0.0.0:*      
LISTEN  0        128             127.0.0.1:9000          0.0.0.0:*      
LISTEN  0        128                     *:80                  *:*      
LISTEN  0        128                  [::]:22               [::]:*      
LISTEN  0        80                      *:3306                *:*   

验证

lamp框架搭建
配置成功

上一篇:源码编译搭建LAMP架构


下一篇:树莓派wiringPi安装及交叉编译