Linux Private Kitchen——LAMP Architecture Enterprise Website | Apache Source Installation | MySQL Source Installation | PHP Source Installation

LAMP structure corporate website

1.LAMP overview

LAMP architecture is one of the current mature enterprise website application modes. It refers to a set of systems and related software that work together, which can provide static and dynamic WEB site services and other referenced development environments. LAMP specifically includes Linux, Apache, MySQL, PHP

2. Introduction to the role of LAMP components

2.1 Linux[Basic Platform]

The Linux system is stable and has a very small footprint and is open source and free. It provides an operating system that supports Web sites [Of course, other platforms such as Win systems are also supported]

2.2 Apache[ front desk]

Powerful and stable web service program, and directly provide users with website access, sending web page pictures and other file content

2.3 MySQL[Backstage]

MySQL is a free and open source relational database management system. Relational databases store data in different tables, effectively improving flexibility

2.4 PHP/Python/Perl[Intermediate Connection]

Three programming languages ​​for developing dynamic webpages, responsible for interpreting dynamic webpage files, communicating with web servers and database systems to work together, and providing a development and permission environment for web applications, among which PHP is a widely used open source code Multi-purpose scripting language It can be embedded in HTML, especially suitable for Web application development

  • Installation order Linux -> Apache -> MySQL -> PHP
  • The order of Apache and MySQL can be arbitrary

3. Source code compile and install Apache service

3.1 Installation environment dependent packages

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

httpd-2.4.29.tar.gz
apr-1.6.2.tar.gz
apr-util-1.6.0.tar.gz

yum install -y gcc gcc-c++ pcre pcre-devel expat-devel perl
Insert picture description here
  • gcc C language compiler
  • gcc-c++ C++ compiler
  • make source code compiler
  • pcre is a Perl function library, including Perl compatible regular expression library
  • pcre-devel is a perl interface development kit
  • expat-devel is used to support website parsing of HTML and XML files
  • perl perl language compiler

3.2 Configuration module

  • Prepare httpd package apr package apr-until package
  • After decompressing the three packages, place the two apr packages in the srclib of httpd
mv apr-1.6.2 /opt/httpd-2.4.29/srclib/apr
mv apr-util-1.6.0 /opt/httpd-2.4.29/srclib/apr-util
Insert picture description here


Insert picture description here
  • Enter httpd directory cd /httpd
./configure \
--prefix=/usr/local/httpd \	#指定安装目录
--enable-so \			#启用动态华仔模块,可以进一步扩展的功能
--enable-rewrite \		#启动网页地址重写功能
--enable-charset-lite \#启用字符集支持
--enable-cgi			#启用通用网关接口脚本程序支持,便于网站外部扩展应用访问能力
Insert picture description here


Insert picture description here

3.3 Start to compile and install

make -j	[数字]			#使用全部资源进行编译
make install  			#开始安装
ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/
Insert picture description here
  • There are many executable files in the bin directory, so that the instructions can be recognized anywhere after linking

3.5 Add httpd system service

method one

  • Use the built-in service startup program [Use the service command for management]
cp /home/httpd/bin/apachectl /etc/init.d/httpd
chmod +x /etc/init.d/httpd
vim /etc/init.d/httpd
#!/bin/bash
# chkconfig: 35 85 21			#允许级别
# description:					 #描述
chkconfig --add httpd			#添加到系统服务中

Method Two

  • Custom create httpd.service in system
vim /lib/systemd/system/httpd.service
[Unit]
Description=The Apache HTTP Server						#描述
After=network.target									#描述服务类别
[Service]
Type=forking											#后台运行方式
PIDFile=/usr/local/httpd/logs/httpd.pid					#PID文件位置
ExecStart=/usr/local/bin/apachectl $OPTIONS				#启动服务
ExecReload=/bin/kill -HUP $MAINPID						#根据PID重载配置
[Install]
WantedBy=multi-user.target

systemctl start httpd.service
systemctl enable httpd.service

3.5 Modify the httpd service configuration file

vim /etc/httpd.conf
Listen 192.168.148.15:80			#指定IP地址和端口
ServerNmae www.gt.com:80	#指定域名和端口
Insert picture description here


Insert picture description here

3.6 Start service

systemctl start httpd
httpd -t		#检查配置文件语法有没有问题
Insert picture description here
Insert picture description here


Insert picture description here


Insert picture description here

3.7 Shell script one-click installation

#!/bin/bash
export getIP=`ifconfig | grep "inet" | awk 'NR==1 {print $2}'`
#Apache
systemctl stop firewalld
systemctl disable firewalld &> /dev/null  
setenforce 0 
yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel perl &> /dev/null     
cd /opt/
tar zxvf apr-1.6.2.tar.gz &> /dev/null 
tar zxvf apr-util-1.6.0.tar.gz &> /dev/null 
tar jxvf httpd-2.4.29.tar.bz2 &> /dev/null 
mv apr-1.6.2 /opt/httpd-2.4.29/srclib/apr
mv apr-util-1.6.0 /opt/httpd-2.4.29/srclib/apr-util
cd /opt/httpd-2.4.29/
./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi &> /dev/null
make -j &> /dev/null
make install &> /dev/null
ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/
echo "
[Unit]
Description=The Apache HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/httpd/logs/httpd.pid
ExecStart=/usr/local/bin/apachectl $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
" > /lib/systemd/system/httpd.service
systemctl start httpd.service
systemctl enable httpd.service &> /dev/null  
sed -i "52c Listen ${getIP}:80" /etc/httpd.conf
read -p "请输入域名:" domain
sed -i "197c ${domain}:80" /etc/httpd.conf
systemctl restart httpd.service
netstat -anpt | grep 80 &> /dev/null
if [ $? -eq 0 ];then
   echo "部署完毕!"


fi

4. Compile and install mysql service

4.1 Prepare the packages required by mysql

yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake
Insert picture description here
  • ncurses dynamic library of character terminal graphical interactive functions
  • ncurses-devel ncurses development kit
  • bison parser
  • cmake mysql needs to be compiled and installed with cmake

4.2 Configuration module

mv boost_1_59_0 boost	#重命名
Insert picture description here
  • Enter the MySQL directory cd /mysql-5.7.17
cmake \
-DCMAKE_INSTALL_PREFIX=/home/mysql \		#指定mysql的安装路径
-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock \ 	#指定mysql进程监听套接字文件(数据库连接文件)的存储路径
-DSYSCONFDIR=/etc \                         #指定配置文件的存储路径
-DSYSTEMD_PID_DIR=/home/mysql \             #指定进程文件的存储路径
-DDEFAULT_CHARSET=utf8  \                   #指定默认使用的字符集编码,如 utf8
-DDEFAULT_COLLATION=utf8_general_ci \	    #指定默认使用的字符集校对规则
-DWITH_EXTRA_CHARSETS=all \					#指定支持其他字符集编码
-DWITH_INNOBASE_STORAGE_ENGINE=1 \          #安装INNOBASE存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \           #安装ARCHIVE存储引擎 
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \         #安装BLACKHOLE存储引擎 
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \        #安装FEDERATED存储引擎 
-DMYSQL_DATADIR=/home/mysql/data \     	    #指定数据库文件的存储路径
-DWITH_BOOST=/home/boost \          		#指定boost的路径,若使用mysql-boost集成包安装则-DWITH_BOOST=boost
-DWITH_SYSTEMD=1							#生成便于systemctl管理的文件

Storage engine options:

  • MYISAM, MERGE, MEMORY, and CSV engines are compiled into the server by default and do not need to be explicitly installed.
  • To statically compile a storage engine to the server, use -DWITH_engine_STORAGE_ENGINE=1. The
    available storage engine values ​​are: ARCHIVE, BLACKHOLE, EXAMPLE, FEDERATED, INNOBASE (InnoDB), PARTITION (partitioning support), and PERFSCHEMA (Performance Schema)

4.3 Start to compile and install

make && make install

4.4 Create mysql user

useradd -M -s /sbin/nologin mysql
Insert picture description here

4.5 Modify the mysql configuration file

vim /etc/my.cnf								#删除原配置项,再重新添加下面内容
[client]									#客户端设置
port = 3306
socket=/home/mysql/mysql.sock			

[mysql]
port = 3306
socket=/home/mysql/mysql.sock	
auto-rehash									#开启自动补全功能

[mysqld]									#服务全局设置
user = mysql       							#设置管理用户
basedir=/home/mysql							#指定数据库的安装目录
datadir=/home/mysql/data					#指定数据库文件的存储路径
port = 3306									#指定端口
character-set-server=utf8					#设置服务器字符集编码格式为utf8
pid-file = /home/mysql/mysqld.pid			#指定pid 进程文件路径
socket=/home/mysql/mysql.sock				#指定数据库连接文件
bind-address = 0.0.0.0						#设置监听地址,0.0.0.0代表允许所有,如允许多个IP需空格隔开
skip-name-resolve							#禁用DNS解析
max_connections=2048						#设置mysql的最大连接数
default-storage-engine=INNODB				#指定默认存储引擎
max_allowed_packet=16M						#设置数据库接收的数据包大小的最大值
server-id = 1								#指定服务ID号
   
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
Insert picture description here

Common values ​​of sql_mode

NO_ENGINE_SUBSTITUTION
If the required storage engine is disabled or not compiled, then an error is thrown. When this value is not set, replace with the default storage engine and throw an exception

STRICT_TRANS_TABLES
In this mode, if a value cannot be inserted into a transaction table, the current operation is interrupted, and there is no restriction on non-transactional tables

NO_AUTO_CREATE_USER
prohibits GRANT from creating users with empty passwords

NO_AUTO_VALUE_ON_ZERO
The auto-increment column in mysql can start from 0. By default, the self-increasing column starts from 1, if you insert data with a value of 0, an error will be reported

NO_ZERO_IN_DATE
does not allow the date and month to be zero

NO_ZERO_DATE
mysql database does not allow to insert zero date, inserting zero date will throw an error instead of a warning

ERROR_FOR_DIVISION_BY_ZERO
During INSERT or UPDATE, if the data is divided by zero, an error is generated instead of a warning. By default, MySQL returns NULL when data is divided by zero

PIPES_AS_CONCAT
treats "||" as a string concatenation operator instead of an OR operator, which is the same as the Oracle database and similar to the string concatenation function Concat

ANSI_QUOTES After
enabling ANSI_QUOTES, you cannot use double quotes to quote a string because it is interpreted as an identifier

4.6 Change the owner group of the mysql installation directory and configuration file

chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
Insert picture description here

4.7 Set path environment variables

echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile	
source /etc/profile
Insert picture description here

4.8 Initialize the database

cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \				#生成初始化密码为空
--user=mysql \                      #指定管理用户
--basedir=/usr/local/mysql \        #指定数据库的安装目录
--datadir=/usr/local/mysql/data		#指定数据库文件的存储路径

cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ #For systemctl service management
systemctl daemon-reload
#Refresh recognition systemctl start mysqld.service #Open service
systemctl enable mysqld
#start on boot netstat -anpt | grep 3306

4.9 Configure mysql

mysqladmin -u root -p password "gt123456" 	#给root账号设置密码为gt123456,提示输入的是原始密码(为空)
mysql -u root -p
grant all privileges on *.* to 'root'@'%' identified by 'gt123456';
#授予root用户可以在所有终端远程登录,使用的密码是abc123,并对所有数据库和所有表有操作权限

show databases;			#查看当前已有的数据库
Insert picture description here

Shell script one-click installation

echo "正在安装编译MySQL的前置>"
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake $> /dev/null


cd /opt
echo "正在解压压缩包>"
tar zxvf mysql-5.7.17.tar.gz $> /dev/null
tar zxvf boost_1_59_0.tar.gz $> /dev/null

cd /opt
mv boost_1_59_0 /usr/local/boost


cd /opt/mysql-5.7.17/
echo "正在开始配置模块>"
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=/usr/local/boost \
-DWITH_SYSTEMD=1 $> /dev/null

make && make install

useradd -M -s /sbin/nologin mysql

echo '[client]
port = 3306
socket=/usr/local/mysql/mysql.sock

[mysqld]									
user = mysql       							
basedir=/usr/local/mysql

datadir=/usr/local/mysql/data

port = 3306									
character-set-server=utf8	

pid-file = /usr/local/mysql/mysqld.pid

socket=/usr/local/mysql/mysql.sock

bind-address = 0.0.0.0

skip-name-resolve							
max_connections=2048

default-storage-engine=INNODB				
max_allowed_packet=16M	
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES' > /etc/my.cnf

chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf


cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data


cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
source /etc/profile
systemctl daemon-reload   
systemctl start mysqld.service
systemctl enable mysqld    
netstat -anpt | grep 3306 &> /dev/null
if [ $? -eq 0 ];then
   echo "MySQL部署完毕!"
   httpd -t
fi

yum -y install expect


function inputPassword {
passwd=$1
/usr/bin/expect <<-EOF
spawn mysqladmin -u root -p password $passwd
expect "Enter password:" 
send "\r"

expect eof
EOF
}
read -p "请输入MySQL密码:" secretkey
inputPassword "$secretkey"

read -p "请输入数据库名:" database_name
read -p "请输入数据库用户名" database_user_name

function ChaengeSQL {
/usr/bin/expect <<-EOF
spawn mysql -u root -p
expect "Enter password:" {send "$secretkey\r"}
expect "mysql>" {send "grant all privileges on *.* to 'root'@'%' identified by '$secretkey';\r"}
expect "mysql>" {send "CREATE DATABASE $database_name;\r"}
expect "mysql>" {send "GRANT all ON $database_name.* TO '$database_user_name'@'%' IDENTIFIED BY '$secretkey';\r"}
expect "mysql>" {send "flush privileges;\r"}
expect "mysql>" {send "quit\r"}
expect eof
EOF
}
ChaengeSQL

5. Compile and install PHP from source code

Used to process and generate pictures

yum -y install \
gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel

5.2 Configuration module

cd /opt
tar jxvf php-7.1.10.tar.bz2
cd /opt/php-7.1.10/
./configure \
--prefix=/usr/local/php7 \							#指定将 PHP 程序的安装路径
--with-apxs2=/usr/local/httpd/bin/apxs \			#指定Apache httpd服务提供的apxs 模块支持程序的文件位置
--with-mysql-sock=/usr/local/mysql/mysql.sock \		#指定mysql 数据库连接文件的存储路径
--with-config-file-path=/usr/local/php7				#设置 PHP 的配置文件 php.ini 将要存放的位置
--with-mysqli \										#添加 MySQL 扩展支持 #mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还可以使访问数据库工作变得更加稳定
--with-zlib \										#支持zlib功能,提供数据压缩
--with-curl \										#开启curl扩展功能,实现HTTP的Get下载和Post请求的方法
--with-gd \											#激活gd 库的支持
--with-jpeg-dir \									#激活jpeg 的支持
--with-png-dir \									#激活png 的支持
--with-freetype-dir \
--with-openssl \
--enable-mbstring \									#启用多字节字符串功能,以便支持中文等代码
--enable-xml \										#开启扩展性标记语言模块
--enable-session \									#会话
--enable-ftp \										#文本传输协议
--enable-pdo \										#函数库
--enable-tokenizer \								#令牌解释器
--enable-zip										#ZIP压缩格式

5.3 Compile and install

make && make install

5.4 Modify the main configuration file

cp /opt/php-7.1.10/php.ini-development /usr/local/php7/php.ini	
vim /usr/local/php7/php.ini
mysqli.default_socket = /usr/local/mysql/mysql.sock
date.timezone = Asia/Shanghai
Insert picture description here

5.5 Configure system variables

Easy to call

ln -s /usr/local/php/bin/* /usr/local/bin/

6.6 Hooking in Apache

vim /etc/httpd.conf 
// 在393行插入以下内容
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
// 在255行添加index.php
DirectoryIndex index.html index.php

Search for LoadModule php7_module modules/libphp7.so in the configuration file, if it exists, it means success, if not, you need to go back to the previous step

5.7 Verify PHP test page

rm -rf /usr/local/httpd/htdocs/index.html
vim /usr/local/httpd/htdocs/index.php
<?php
phpinfo();
?>
systemctl restart httpd.service
Insert picture description here