LAMP architecture and installation configuration

1. Introduction to LAMP Architecture

LAMP is the abbreviation of Linux Apache MySQL PHP. In fact, it is a script language that installs Apache, MySQL and PHP on a Linux system to form an environment to run php. Apache is the most commonly used WEB service software, and MySQL is a relatively small database software. Both of these software and PHP can be installed on Windows machines.

1.1 LAMP=Linux+Apache (httpd--web service)+MySQL (data repository)+PHP (scripting language)

  • PHP website: Google, Taobao, Baidu, 51CTO blog, ape class forum
  • The three roles can be on one machine, or they can be separated (but httpd and PHP must be together)
  • The popular languages ​​in the past 2 years are python, JAVA, GO

1.2 LAMP Architecture Diagram

How httpd, PHP, MySQL workimage

2. Introduction to MySQL and MariaDB

  • MySQL is a relational database, developed by mysql ab company, mysql was acquired by sun company in 2008 ($1 billion), and sun company was acquired by oracle company in 2009 ($7.4 billion)
  • MySQL official website https://www.mysql.com latest version 5.7GA/8.0DMR
  • Since MySQL 5.6, the changes have been relatively large, and the performance of 5.7 has been greatly improved
  • Mariadb is a branch of MySQL, the official website https://mariadb.com/ latest version 10.2
  • MariaDB is mainly maintained by SkySQL Company (now renamed MariaDB Company), SkySQL Company was founded by the original MySQL author and most of the original team.
  • Mariadb 5.5 version corresponds to MySQL 5.5, 10.0 corresponds to MySQL 5.6
  • Community version, Enterprise version, GA (Generally Available) refers to the general version, used in the production environment, DMR (Development Milestone Release) development milestone release version, RC (Release Candidate) release candidate version, Beta open beta x version, Alpha internal beta x version

3. MySQL installation

  • Several common installation packages of MySQL: rpm, source code, binary free compilation
  • The rpm package cannot be customized and is not recommended; the source code needs to be compiled, which is very slow, but it can improve performance, so I will not learn it for the time being; first learn the compilation-free package compiled by others

3.1 Download packages

[root@localhost ~]# cd /usr/local/src   //进入目录下,以后会默认把该目录作为软件包的下载存放目录
[root@localhost src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz    //下载包到该目录下

3.2 Initialize the Mysql installation file

[root@localhost src]# tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz    //解压免编译包
-  mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql   //移动目录/usr/local/,并重命名为MySQL
# 确认/usr/local/mysql没有创建,不然下面的命令会变成移动,而不是重命名
-  useradd mysql    创建MySQL用户
-  mkdir /data/      创建MySQL的数据存放目录
-  ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql  格式化mysql文件

[root@localhost src]# ls
mysql-5.6.35-linux-glibc2.5-x86_64  mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
[root@localhost src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
[root@localhost src]# cd !$
cd /usr/local/mysql
[root@localhost mysql]# ls
bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files
[root@localhost mysql]# useradd mysql     //添加mysql的专门用户
[root@localhost mysql]# mkdir -p /data/mysql     //创建datadir,数据库的文件就存在这里
[root@localhost mysql]# chown -R mysql:mysql /data/mysql   //更改权限,合理分配权限
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql   //初始化
[root@localhost mysql]# echo $?
0
# 查询上一条编译命令是否成功,返回0表示成功,返回1表示失败

3.2.1 Errors encountered during initialization

Error 1:

FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper

Solution:

# yum -y install autoconf            //安装缺少的autoconf

Error 2:

Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

Solution:

# yum install -y libaio-devel.x86_64   //安装缺少的libaio包,一般只用装devel库文件就好了

3.3 Configure Mysql

# 拷贝配置文件
[root@localhost mysql]# cp support-files/my-default.cnf  /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
# 拷贝启动脚本文件并修改其属性
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# chmod 755 /etc/init.d/mysqld
# 修改启动脚本
[root@localhost mysql]# vim /etc/init.d/mysqld
# 需要修改的地方,basedir和datadir
basedir=/usr/local/mysql
datadir=/data/mysql

# 把启动脚本添加到开机启动项,并设定开机启动
[root@localhost mysql]# chkconfig --add mysqld   //将mysqld服务加入开机服务
[root@localhost mysql]# chkconfig  --list       //查看是否加入开机服务成功   

# 命令启动mysql服务
[root@localhost mysql]# service mysqld start
Starting MySQL.Logging to '/data/mysql/localhost.localdomain.err'.
 SUCCESS!

# 查看mysql进程是否启动和它的端口
ps aux | grep mysql
netstat -lntp      mysql默认端口3306

# 查看mysql无法正常启动的错误日志
[root@localhost mysql]# cat /data/mysql/localhost.localdomain.err  //通常是/data/mysql/主机名.err文件

3.3.1 command line to start mysql method

  • If there is no way to put the script into the startup service, or there is no startup script template copy, to start the mysql service, use the command line /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf -- user=mysql --datadir=/data/mysql&ps

  • Use the service command to start mysql, you can use the /etc/init.d/mysqld stopcommand to close, but open it with the above command line, it will not work. So how to stop it?

  • You can only use killall mysqlclose, not kill mysqlclose, because kill will directly terminate the process, and killall will wait for the data being read and written to complete before ending. If killall does not respond in the later work, it is because of the large amount of data, so it will keep waiting. , do not hurry.

  • The killall command centos7 is not installed by default, the installation command is as follows

# yum install -y psmisc

4. Apache (httpd) installation

4.1 Download the source package

  • Apache official website address: http://www.apache.org/dyn/closer.cgi
  • Apache is the name of a foundation, and httpd is the software package we want to install. In the early days, its name was apache, and now the mainstream version is 2.4
  • The installation methods of 2.2 and 2.4 are different, and the version of the APR general function library involved is different. The APR package that comes with our CentOS7 is 2.2, and the 2.4 needs to be downloaded and compiled by itself.
[root@localhost mysql]# cd /usr/local/s
[root@host src]# wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.32.tar.gz      //httpd的二进制包
[root@host src]# wget http://mirrors.hust.edu.cn/apache/apr/apr-1.6.3.tar.gz        //APR库源码包
[root@host src]# wget http://mirrors.hust.edu.cn/apache/apr/apr-util-1.6.1.tar.gz  //APR-util库源码包

4.2 Decompression

[root@localhost src]# tar zxvf httpd-2.4.32.tar.gz
[root@localhost src]# tar zxvf apr-1.6.3.tar.gz 
[root@localhost src]# tar zxvf apr-util-1.6.1.tar.gz

4.3 Install apr

[root@localhost src]# cd /usr/local/src/apr-1.6.3
[root@localhost apr-1.6.3]# ./configure --prefix=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
Configuring APR library
Platform: x86_64-pc-linux-gnu
checking for working mkdir -p... yes
APR Version: 1.6.3
checking for chosen layout... apr
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/local/src/apr-1.6.3':
configure: error: no acceptable C compiler found in $PATH     // !报错!
See `config.log' for more details

# 编译出现报错,解决办法是安装gcc套件
[root@localhost apr-1.6.3]# yum install -y gcc

# 再次编译出现报错,错误代码如下:
... ...
config.status: executing libtool commands
rm: cannot remove 'libtoolT': No such file or directory
config.status: executing default commands

# 解决办法是编辑 configure文件,查找 $RM "$cfgfile" 这个地方,用#注释掉

[root@localhost apr-1.6.3]# vim configure   
/$RM "$cfgfile        //查找这个,并用#注释掉

[root@localhost apr-1.6.3]# make && make install  //编译
[root@localhost apr-1.6.3]# echo $?
0

4.4 Install APR-util

[root@localhost apr-1.6.3]# cd /usr/local/src/apr-util-1.6.1
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr   //初始化
[root@localhost apr-util-1.6.1]# echo $?
0
[root@localhost apr-util-1.6.1]# make && make install   //编译
... ... # 报错
xml/apr_xml.c:35:19: 致命错误:expat.h:没有那个文件或目录
 #include <expat.h>
# 解决方案:安装expat库
[root@localhost apr-util-1.6.1]# yum install expat-devel
[root@localhost apr-util-1.6.1]# make && make install   //再编译
[root@localhost apr-util-1.6.1]# echo $?
0

4.5 Install httpd

[root@localhost apr-util-1.6.1]# cd /usr/local/httpd-2.4.32
[root@localhost httpd-2.4.32]# ./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most
####
--enable-so \       //表示开启支持动态扩展模块,PHP还有Appache都支持以模块的形式存在
--enable-mods-shared=most  //表示开启大多数模块
####
# 编译报错
...  ...
checking for pcre-config... false
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

# 解决方案,安装pcre库
[root@localhost httpd-2.4.32]# yum install -y pcre-devel.x86_64

# 再初始化
[root@localhost httpd-2.4.32]# ./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most
[root@localhost httpd-2.4.32]# echo $?
0

# 编译
[root@localhost httpd-2.4.32]# make && make install
[root@localhost httpd-2.4.32]# echo $?
0

# 如果编译的过程中,出现如下报错:
……
#报错,也可能不出现,但是后面出现了的话,注意是因为apr包被破坏的原因
collect2: error: ld returned 1 exit status
make[2]: *** [htpasswd] Error 1
make[2]: Leaving directory `/usr/local/src/httpd-2.4.27/support'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/local/src/httpd-2.4.27/support'
# 解决方案:是因为没有apr包,可能是第一次初始化的时候,出过一次错,把apr包弄乱了,回头重新安装apr包和apr-util包

# 启动apache
[root@localhost httpd-2.4.32]# /usr/local/apache2.4/bin/apachectl start    //下面有提示,但并不是错误的
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message

[root@localhost httpd-2.4.32]# ps aux | grep httpd
root     63343  0.0  0.1  70904  2212 ?        Ss   01:10   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon   63344  0.0  0.1 359868  2212 ?        Sl   01:10   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon   63345  0.0  0.1 359868  2212 ?        Sl   01:10   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon   63346  0.0  0.1 359868  2212 ?        Sl   01:10   0:00 /usr/local/apache2.4/bin/httpd -k start
root     63429  0.0  0.0 112676   984 pts/0    R+   01:10   0:00 grep --color=auto httpd

[root@localhost httpd-2.4.32]# netstat -lntp     //查看端口,httpd默认监听的端口是80端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      852/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1069/master         
tcp6       0      0 :::80                   :::*                    LISTEN      63343/httpd         
tcp6       0      0 :::22                   :::*                    LISTEN      852/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1069/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1884/mysqld

5. Compile and install PHP

Putting PHP in the last installation is because the path of apache and mysql needs to be specified when PHP is compiled. The order in which apache and mysql are installed does not matter.

The current mainstream versions of PHP are 5.6 and 7.1

5.1 Download PHP 5.6

[root@localhost httpd-2.4.32]# cd /usr/local/src
[root@localhost src]# wget http://cn2.php.net/distributions/php-5.6.30.tar.bz2
[root@localhost src]# tar jxf php-5.6.30.tar.bz2
tar (child): bzip2: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now   //说明没有安装(bzip2)这个软件包。解决方法:安装(bzip2)软件包
[root@localhost src]# yum -y install bzip2
[root@localhost src]# tar jxf php-5.6.30.tar.bz2

5.2 Configure compilation parameters, compile and install

[root@localhost src]# cd php-5.6.30
# 初始化 (LAMP,M=mysql)
[root@localhost php-5.6.30]# ./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 --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
——————
# 初始化 (LAMP,M=mariadb)
[root@localhost php-5.6.30]# ./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/mariadb --with-pdo-mysql=/usr/local/mariadb --with-mysqli=/usr/local/mariadb/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
——————

# 解释
# --prefix 安装目录
#--with-apxs2 apache的工具,能让我们不用人工干涉它,帮忙把扩展模块放到apache的modules目录里,并且在它的配置文件里加上一行mod-modules,自动的给你配置上这个模块,一般可以给你加上so文件,但是不会自动加载配置模块,这就是为什么要后安装PHP
#--with-mysql --with-pdo-mysql --with-mysqli 定义给mysql通信的相关函数或者驱动,让php能和mysql交换数据
#--with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif 这些都是一些必要的参数

#在编译的过程中,会出现一些错误,往往是因为缺少相关的库文件

# 报错
1. configure: error: xml2-config not found. Please check your libxml2 installation.
yum install libxml2-devel -y

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

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

4. configure: error: jpeglib.h not found.
yum install -y libjpeg-devel

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

6. configure: error: freetype-config not found.
yum install -y freetype-devel

7. configure: error: mcrypt.h not found. Please reinstall libmcrypt.
(centos源不能安装libmcrypt-devel,由于版权的原因,没有自带mcrypt的包rpm -qa |grep limcrypt limcrypt-devel,此源为rethot社区版的源)
安装第三方yum源
wget http://www.atomicorp.com/installers/atomic
sh ./atomic
yum install php-mcrypt libmcrypt libmcrypt-devel

# 编译
[root@localhost php-5.6.30]# echo $?
0
[root@localhost php-5.6.30]# make && make install
[root@localhost php-5.6.30]# echo $?
0

5.3 Copy the php configuration file

# 把参考配置文件拷贝到定义的路径 
[root@localhost php-5.6.30]# cp php.ini-production /usr/local/php/etc/php.ini 

# 也可以自己找一下, /usr/local/php/bin/php -i | less 查看一些php的参数
# php.ini-production 使用于生产环境的配置文件,还有适用于开发和实验环境的配置文件php.ini-development

5.4 View

[root@localhost php-5.6.30]# ls /usr/local/php/    //php核心文件
bin etc include lib php
[root@localhost php-5.6.30]# ls /usr/local/php/bin/       //php核心的二进制文件在这个文件夹里面
pear peardev pecl phar phar.phar php php-cgi php-config phpize
[root@localhost php-5.6.30]# du -sh /usr/local/php/bin/php     
36M	/usr/local/php/bin/php
[root@localhost php-5.6.30]# du -sh /usr/local/apache2.4/modules/libphp5.so   //PHP和Apache结合的模块
37M	/usr/local/apache2.4/modules/libphp5.so

# 查看php加载的模块(静态)
[root@localhost php-5.6.30]# /usr/local/php/bin/php -m 

# 查看php加载的模块(静+动态)
[root@localhost php-5.6.30]# /usr/local/php/bin/php -M

# apache配置文件
[root@localhost php-5.6.30]#vi  /usr/local/apache2.4/conf/httpd.conf

5.5 Install PHP7

Similar steps to installing PHP5

cd /usr/local/src/
wget http://cn2.php.net/distributions/php-7.1.6.tar.bz2
tar zxf php-7.1.6.tar.bz2
cd php-7.1.6
./configure --prefix=/usr/local/php7 --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php7/etc --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
make && make install
ls /usr/local/apache2.4/modules/libphp7.so
cp php.ini-production /usr/local/php7/etc/php.ini

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325091707&siteId=291194637