Compile and install LAMP from source code

Compile and install LAMP from source code

1. LAMP architecture

1.1 What is LAMP

The 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 to provide dynamic website services and application development environments. LAMP is an acronym that specifically includes Linux operating system, Apache web server, MySQL database server, PHP (or Perl, Python) web programming language.

1.2 Components of the LAMP architecture

ComponentFeatures
(Platform)LinuxAs the basis of the LAMP architecture, it provides an operating system to support the Web site, which can provide better stability and compatibility with the other three components (AMP components also support platforms such as Windows and UNIX).
(Foreground) ApacheAs the front end of the LAMP architecture, it is a powerful and stable web server program that directly provides users with website access, sending web pages, pictures and other file content.
(Background) MySQLAs the back end of the LAMP architecture, it is a popular open source relational database system. In applications such as corporate websites and business systems, various account information, product information, customer information, business data, etc. can be stored in the MySQL database, and other programs can query and change this information through SQL statements.
(Intermediate connection) PHP/Perl/PythonAs three programming languages ​​for developing dynamic web pages, it is responsible for interpreting dynamic webpage files, communicating with web servers and database systems to work together, and providing a development and operating environment for web applications. Among them, PHP is a widely used open source multi-purpose scripting language, which can be embedded in HTML, and is especially suitable for Web application development.

1.3 The sequence of building the LAMP platform

When building the LAMP platform, the order of installation of each component is Linux→Apache→MySQL→PHP. Among them, there is no strict order for the installation of Apache and MySQL. The installation of the PHP environment is generally placed at the end of the installation, responsible for communicating with the Web server and the database system to work together.

Two, compile and install the Apache service

1. Turn off the firewall, and upload the software packages required to install Apache to the /opt directory

systemctl stop firewalld
systemctl disable firewalld
setenforce 0
Insert picture description here


2. Installation environment dependent packages

yum -y install \
gcc \              #C语言的编译器
gcc-c++ \          #C++的编译器
make \             #源代码编译器(源代码转换成二进制文件)
pcre \             #pcre是一个Perl函数库,包括perl 兼容的正则表达式库
pcre-devel \       #perl的接口开发包
expat-devel \      #用于支持网站解析HTML、XML文件
perl               #perl语言编译器      

Note: You need to create a yum warehouse before this step

Insert picture description here


. 3. Configure the software module

cd /opt/
tar zxvf apr-1.6.2.tar.gz
tar zxvf apr-util-1.6.0.tar.gz
tar jxvf httpd-2.4.29.tar.bz2

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 \ #Specify the installation path of the httpd service program
–enable-so \ #Enable dynamic loading module support, so that httpd has further extended functions The ability of
–enable-rewrite \ #Enable web page address rewriting function for website optimization, anti-leech and catalog migration maintenance
–enable-charset-lite \ #Enable character set support to support pages that use various character set encodings
– enable-cgi #Enable CGI (Common Gateway Interface) script program support to facilitate the external extension of the website's application access capabilities

Insert picture description here


Insert picture description here


Insert picture description here


Insert picture description here


Insert picture description here


4. Compile and install

make       #make -j 2  表示开2核同时进行编译
make install
Insert picture description here


5. Optimize the configuration file path, and put the executable program file of the httpd service into the directory of the path environment variable to facilitate system identification

ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/
Insert picture description here


6. Add httpd system service
Method one:

cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd		#用于service服务管理
chmod +x /etc/init.d/httpd
vim /etc/init.d/httpd
#!/bin/bash								#在第一行前插入新行,添加此三行内容
# chkconfig: 35 85 21					#35级别自动运行  第85个启动 第21个关闭
# description: Apache is a World Wide Web server

chkconfig --add httpd
#Add the httpd service to the service manager systemctl start httpd.service
or
service httpd start

Method Two:

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

Insert picture description here


Insert picture description here


7. Modify the httpd service configuration file

vim /etc/httpd.conf
--52行--修改
Listen 192.198.229.50:80
--197行--取消注释,修改
ServerName www.wyw.com:80

–Line 221 – The default home page storage path
DocumentRoot "/usr/local/httpd/htdocs"
– Line 255 – The default home page file name setting
DirectoryIndex index.html

httpd -t or apachectl -t #Check whether the configuration items of the configuration file are wrong
cat /usr/local/httpd/htdocs/index.html
systemctl restart httpd.service

Insert picture description here


Insert picture description here


Insert picture description here


Insert picture description here


Insert picture description here


Insert picture description here


8. Browser access verification

netstat -anpt | grep 80
echo "192.168.229.50 www.wyw.com" >> /etc/hosts

http://192.168.229.50
http://www.wyw.com

Insert picture description here


Insert picture description here


apache installation is complete

Three, compile and install mysqld service

1. Transfer the software packages required to install mysql to the /opt directory

Insert picture description here


2. Installation environment dependent packages

yum -y install \
gcc \
gcc-c++ \
ncurses \    #字符终端下图形互动功能的动态库
ncurses-devel \   #ncurses开发包
bison \     #语法分析器
cmake     #mysql需要用cmake编译安装
Insert picture description here


Insert picture description here


3. Configure the software module

tar zxvf mysql-5.7.17.tar.gz
tar zxvf boost_1_59_0.tar.gz

cd /opt
mv boost_1_59_0 /usr/local/boost #Rename

cd /opt/mysql-5.7.17/
cmake
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
#Specify the installation path of mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ #Specify the mysql process to monitor the socket file (Database connection file) storage path
-DSYSCONFDIR=/etc \ #Specify the storage path of the configuration file
-DSYSTEMD_PID_DIR=/usr/local/mysql \
#Specify the storage path of the process file -DDEFAULT_CHARSET=utf8 \ #Specify the default character set used Encoding, such as utf8
-DDEFAULT_COLLATION=utf8_general_ci \ #Specify the default character set collation rules
-DWITH_EXTRA_CHARSETS=all \ #Specify to support other character set encodings
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ #Install INNOBASE storage engine-
DWITH_ARCHIVE_S #TORAGE_ARCHIVE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ #Install the BLACKHOLE storage engine-
DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ #Install the FEDERATED storage engine-
DMYSQL_DATADIR=/usr/local/mysql/data \ #Specify the storage path of the database file
-DWITH_BOOST=/usr/local/boost \ #Specify the path of boost, if you use the mysql-boost integration package to install, -DWITH_BOOST=boost
-DWITH_SYSTEMD=1 #Generate files that are easy to manage by systemctl

Storage engine options:
MYISAM, MERGE, MEMORY, and CSV engines are compiled to 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)
Note: If in CMAKE An error was reported during the process. When the error is resolved, you need to delete the CMakeCache.txt file in the source directory, and then re-CMAKE, otherwise the error remains

Insert picture description here


Insert picture description here


Insert picture description here


4. Compile and install

make && make install
Insert picture description here


Insert picture description here


Insert picture description here


5. Create a mysql user

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


6. Modify the mysql configuration file

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

[mysqld] #Service global settings
user = mysql
#Set management user basedir=/usr/local/mysql
#Specify the installation directory of the database datadir=/usr/local/mysql/data
#Specify the storage path of the database file port = 3306 #Specify Port
character-set-server=utf8 #Set the server character set encoding format to utf8
pid-file = /usr/local/mysql/mysqld.pid #Specify the path of the pid process file
socket=/usr/local/mysql/mysql.sock # Specify the database connection file
bind-address = 0.0.0.0 #Set the listening address, 0.0.0.0 means all are allowed, if multiple IPs are allowed, a space must be separated by
skip-name-resolve #Disable DNS resolution
max_connections=2048 #Set the maximum connection of mysql Number
default-storage-engine=INNODB #Specify the default storage engine
max_allowed_packet=16M #Set the maximum size of data packets received by the database
server-id = 1 #Specify the service ID number

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

Common values ​​of sql_mode are as follows:
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 ANSI_QUOTES is enabled, double quotes cannot be used to quote a string, because it is interpreted as an identifier.

Insert picture description here


7. 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


8. Set the path environment variable

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


9. Initialize the database

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


Insert picture description here


10. Add mysqld system service

cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/  #用于systemctl服务管理
systemctl daemon-reload         #刷新识别     
systemctl start mysqld.service  #开启服务
systemctl enable mysqld         #开机自启动
netstat -anpt | grep 3306       #查看端口
Insert picture description here


11. Modify the mysql login password

mysqladmin -u root -p password "abc123"  
#给root账号设置密码为abc123,提示输入的是原始密码(为空)
Insert picture description here


12. Authorize remote login

mysql -u root -p
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
#授予root用户可以在所有终端远程登录,使用的密码是abc123,并对所有数据库和所有表有操作权限

show databases; #View current existing databases

Insert picture description here

Four, compile and install the PHP parsing environment

1. Transfer the software packages required for installing PHP to the /opt directory

Insert picture description here


2. Install GD library and GD library related programs 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
Insert picture description here


3. Configure the software 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压缩格式
Insert picture description here


Insert picture description here


4. Compile and install

make && make install
Insert picture description here


Insert picture description here


Insert picture description here


5. Copy the template file as the main configuration file of PHP and modify it

cp /opt/php-7.1.10/php.ini-development /usr/local/php7/php.ini 
#在测试环境时使用php.ini-development文件,而在生产环境时使用php.ini-production文件
vim /usr/local/php7/php.ini
--1170行--修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
--939行--取消注释,修改
date.timezone = Asia/Shanghai
Insert picture description here


Insert picture description here


Insert picture description here


6. Optimize the PHP executable program file to be placed in the directory of the path environment variable for easy system identification

ln -s /usr/local/php7/bin/* /usr/local/bin/
php -m    #查看PHP 加载了哪些模块
Insert picture description here


7. Modify the configuration file of the httpd service to allow Apache to support PHP

vim /etc/httpd.conf 
--393行--插入以下内容
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
--255行--修改首页文件名设置
DirectoryIndex index.html index.php
---检查支持php7的模块是否存在------
LoadModule php7_module        modules/libphp7.so
Insert picture description here


Insert picture description here


Insert picture description here


8. Verify the PHP test page

rm -rf /usr/local/httpd/htdocs/index.html
vim /usr/local/httpd/htdocs/index.php
<?php
phpinfo();
?>

systemctl restart httpd.service

Browser visit
http://192.168.229.50

Insert picture description here


Insert picture description here


Insert picture description here

Five, install the forum

1. Create a database and authorize

mysql -u root -p
CREATE DATABASE bbs;
#创建一个数据库
GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';
#把bbs数据库里面所有表的权限授予给bbsuser,并设置密码admin123
flush privileges;
#刷新数据库
show databases; 
Insert picture description here


Insert picture description here


3. Change the owner of the forum directory

cd /usr/local/httpd/htdocs/bbs
chown -R daemon ./config
chown -R daemon ./data
chown -R daemon ./uc_client
chown -R daemon ./uc_server/data
Insert picture description here


4. Browser access verification Visit the
forum page at
http://192.168.229.50/bbs
database server: localhost ###Use localhost for local setup, how to fill in the IP address and port number instead of on this machine
Database name: bbs
Database user name: bbsuser
Database password: admin123
Administrator account: admin
Administrator password: admin123
Forum background administrator page
http://192.168.80.10/bbs/admin.php

Insert picture description here


Insert picture description here


Insert picture description here


Insert picture description here


Insert picture description here


Insert picture description here