Compile and install Apache, mysqld and PHP

Overview of LAMP architecture

Introduction to LAMP

LAMP is the acronym of Linux operating system + Apache Web server + MySQL database server + PHP (or Perl, Python) web programming language.
They are all independent programs, but together they can be used to build open source software for dynamic websites or servers.

Function of each component

Linux - platform

Linux is the basis of LAMP architecture. It provides the operating system used to support the Web site, and can provide better stability and compatibility with the other three components (AMP components also support Windows, UNIX and other platforms).

Apache - front desk

As the front end of LAMP architecture, Apache is a powerful and stable Web server program. The server directly provides users with website access, sending Web pages, pictures and other file contents.

MySQL - background

MySQL, as the back end of LAMP architecture, is a popular open source relational database system.
In enterprise websites, business systems and other applications, various account information, product information, customer information and business data can be stored in MySQL database. Other programs can query and change these information through SQL statements.

PHP/Perl/Python - intermediate connection

PHP/Perl/Python: as three programming languages for developing dynamic Web pages, it is responsible for interpreting dynamic Web page files, communicating the Web server and database system to work together, and providing the development and running environment of Web applications.
PHP is a widely used open source multi-purpose scripting language, which can be embedded in HTML, especially suitable for Web application development. (this chapter is written in PHP language)

Sequence of building LAMP platform

When building the LAMP platform, the installation sequence of each component is Linux → Apache → MySQL → PHP. Apache and MySQL are not installed in strict order. The installation of PHP environment is generally put at the end, which is responsible for communicating the Web server and database system to work together.

Compile and install Apache httpd

1.Turn off the firewall and install Apache Transfer the required software package to/opt Under the directory
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
#apr component package is used to support Apache upper application cross platform and provide lower interface library, which can effectively reduce the number of concurrent connections, processes and access congestion.

2.Install environment dependent packages
yum -y install \
gcc \							#C language compiler
gcc-c++ \						#C + + compiler
make \							#Source code compiler (source code to binary file)
pcre \							#pcre is a perl function library, including perl compatible regular expression library
pcre-devel \                    #perl interface development package
expat-devel \                   #It is used to support the website to parse HTML and XML files
perl                            #perl compiler
----------------------------------------------------------------------------------------------------------
yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel perl            

3.Configuration 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 path where the httpd service program will be installed
--enable-so \					#Enable dynamic loading module support to enable httpd to further expand its functions
--enable-rewrite \				#Enable the web address rewriting function for website optimization, anti-theft chain and directory migration maintenance
--enable-charset-lite \			#Start character set support to support pages encoded with various character sets
--enable-cgi					#Enable CGI (general Gateway Interface) script program support to facilitate the external expansion of application access capability of the website
----------------------------------------------------------------------------------------------------------
./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi

4.Compilation and installation
make							#make -j 2 means to open 2 cores and compile at the same time
make install

5.Optimize the configuration file path and httpd The executable program file of the service is put into the directory of the path environment variable for system identification
ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/

6.add to httpd system service 
Method 1:
cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd		#Used for service management
chmod +x /etc/init.d/httpd
vi /etc/init.d/httpd
#!/bin/bash												#Insert a new line before the first line and add these three lines
# chkconfig: 35 85 21									#Level 35 automatic operation, 85th start, 21st close
# description: Apache is a World Wide Web server

chkconfig --add httpd     		#Add httpd service to service manager

systemctl start httpd.service
 or
service httpd start

Method 2:
vim /lib/systemd/system/httpd.service
[Unit]
Description=The Apache HTTP Server						#describe
After=network.target									#Describe service category
[Service]
Type=forking											#Background operation mode
PIDFile=/usr/local/httpd/logs/httpd.pid					#PID file location
ExecStart=/usr/local/bin/apachectl $OPTIONS				#Start service
ExecReload=/bin/kill -HUP $MAINPID						#According to PID overload configuration
[Install]
WantedBy=multi-user.target

systemctl start httpd.service
systemctl enable httpd.service


7.modify httpd Service profile
vim /etc/httpd.conf
--52 that 's ok--modify
Listen 192.198.19.88:80
--197 that 's ok--Uncomment, modify
ServerName www.feng.com:80

--221 that 's ok--Default home page storage path
DocumentRoot "/usr/local/httpd/htdocs"
--255 that 's ok--Default home page file name setting
DirectoryIndex index.html

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

8.Browser access verification
netstat -anpt | grep 80
echo "192.168.19.88 www.feng.com" >> /etc/hosts

http://192.168.19.88
http://www.feng.com

Compile and install mysqld

1.Will install mysql Transfer the required software package to/opt Under the directory
mysql-5.7.17.tar.gz
boost_1_59_0.tar.gz		#Runtime supporting c + +

2.Install environment dependent packages
yum -y install \
gcc \
gcc-c++ \
ncurses \				#Dynamic library of graphic interactive function under character terminal
ncurses-devel \			#ncurses development kit
bison \					#Parser
cmake					#mysql needs to be compiled and installed with cmake
----------------------------------------------------------------------------------------------------------
yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake

3.Configuration 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 storage path of mysql process listening socket file (database connection file)
-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  \                       #Specifies the character set encoding used by default, such as utf8
-DDEFAULT_COLLATION=utf8_general_ci \			#Specifies the default character set collation rule to use
-DWITH_EXTRA_CHARSETS=all \						#Specifies that other character set encodings are supported
-DWITH_INNOBASE_STORAGE_ENGINE=1 \              #Install INNOBASE storage engine
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \               #Installing the ARCHIVE storage engine 
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \             #Installing the BLACKHOLE storage engine 
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \            #Install 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 MySQL boost integration package is used for installation, - DWITH_BOOST=boost
-DWITH_SYSTEMD=1								#Generate files for systemctl management

Storage engine options:
MYISAM,MERGE,MEMORY,and CSV The engine is compiled into the server by default and does not need to be explicitly installed.
Statically compile a storage engine to the server, using-DWITH_engine_STORAGE_ENGINE= 1
 Available storage engine values are: ARCHIVE, BLACKHOLE, EXAMPLE, FEDERATED, INNOBASE (InnoDB), PARTITION (partitioning support), and PERFSCHEMA (Performance Schema)
----------------------------------------------------------------------------------------------------------
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

Note: if CMAKE There are errors in the process of. When the errors are solved, you need to put the errors in the source directory CMakeCache.txt Delete the file and then try again CMAKE,Otherwise, the error remains

4.Compilation and installation
make && make install

5.establish mysql user
useradd -M -s /sbin/nologin  mysql

6.modify mysql configuration file
vim /etc/my.cnf								#Delete the original configuration item and add the following content again
[client]									#Client settings
port = 3306
socket = /usr/local/mysql/mysql.sock			

[mysql]										#Server settings
port = 3306
socket = /usr/local/mysql/mysql.sock
auto-rehash									#Turn on the automatic completion function

[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 encoding format of the server character set to utf8
pid-file = /usr/local/mysql/mysqld.pid		#Specify pid process file path
socket=/usr/local/mysql/mysql.sock			#Specify database connection file
bind-address = 0.0.0.0						#Set the listening address. 0.0.0.0 means that all IP addresses are allowed. If multiple IP addresses are allowed, they should be separated by spaces
skip-name-resolve							#Disable DNS resolution
max_connections=2048						#Set the maximum number of mysql connections
default-storage-engine=INNODB				#Specify the default storage engine
max_allowed_packet=16M						#Set the maximum packet size received by the database
server-id = 1								#Specify 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

----------------------------------------------------------------------------------------------------------
sql_mode Common values are as follows:
NO_ENGINE_SUBSTITUTION
 If the required storage engine is disabled or not compiled,Then throw an error. When this value is not set,Replace with default storage engine,And throw an exception

STRICT_TRANS_TABLES
 In this mode,If a value cannot be inserted into a transaction table,Then interrupt the current operation,No restrictions on non transaction tables

NO_AUTO_CREATE_USER
 prohibit GRANT Create a user with a blank password

NO_AUTO_VALUE_ON_ZERO
mysql The self growing column in can start with 0. By default, the self growth column starts from 1. If you insert data with a value of 0, an error will be reported

NO_ZERO_IN_DATE
 Zero date and month are not allowed

NO_ZERO_DATE
mysql Zero date insertion is not allowed in the database,Inserting a zero date throws an error instead of a warning

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

PIPES_AS_CONCAT
 take"||"Treat as a concatenation operator of a string rather than an or operator, which is similar to Oracle The database is the same as the string splicing function Concat Similar

ANSI_QUOTES
 Enable ANSI_QUOTES You cannot use double quotation marks to refer to a string after, because it is interpreted as an identifier
----------------------------------------------------------------------------------------------------------
[client]
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
auto-rehash

[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

7.change mysql Primary group of installation directories and configuration files
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf

8.Set path environment variable
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile	
source /etc/profile

9.Initialize database
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \				#Generation initialization password is null
--user=mysql \                      #Specify administrative users
--basedir=/usr/local/mysql \        #Specify the installation directory of the database
--datadir=/usr/local/mysql/data		#Specify the storage path of the database file
----------------------------------------------------------------------------------------------------------
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

10.add to mysqld system service 
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         #Power on self start
netstat -anpt | grep 3306       #View port

11.modify mysql Login password for
mysqladmin -u root -p password "abc123" 	#Set the password to abc123 for the root account, and the prompt is the original password (empty)

12.Authorize remote login
mysql -u root -p
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
#The root user is authorized to log in remotely at all terminals with the password abc123, and has operation authority on all databases and tables

show databases;			#View existing databases

Compile and install PHP

1.Will install PHP Transfer the required software package to/opt Under the directory
php-7.1.10.tar.bz2

2.install GD Library and GD Library Association program, which is 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

3.Configuration software module
cd /opt
tar jxvf php-7.1.10.tar.bz2
cd /opt/php-7.1.10/
./configure \
--prefix=/usr/local/php7 \							#Specify the path where the PHP program will be installed
--with-apxs2=/usr/local/httpd/bin/apxs \			#Specifies the file location of the apxs module support program provided by the Apache httpd service
--with-mysql-sock=/usr/local/mysql/mysql.sock \		#Specify the storage path of mysql database connection file
--with-config-file-path=/usr/local/php7				#Set the configuration file for PHP Where ini will be stored
--with-mysqli \										#add to MySQL Extended support #mysqli extension technology can not only call MySQL stored procedures and handle MySQL transactions, but also make accessing the database more stable
--with-zlib \										#Support zlib function and provide data compression
--with-curl \										#Enable curl extension function to realize HTTP Get download and Post request
--with-gd \											#Activate gd library support
--with-jpeg-dir \									#Activate jpeg support
--with-png-dir \									#Activate png support
--with-freetype-dir \
--with-openssl \
--enable-mbstring \									#Enable multi byte string function to support Chinese and other codes
--enable-xml \										#Open extensible markup language module
--enable-session \									#conversation
--enable-ftp \										#Text transfer protocol
--enable-pdo \										#function library
--enable-tokenizer \								#Token interpreter
--enable-zip										#ZIP compression format
----------------------------------------------------------------------------------------------------------
./configure \
--prefix=/usr/local/php7 \
--with-apxs2=/usr/local/httpd/bin/apxs \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-config-file-path=/usr/local/php7 \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip

4.Compilation and installation
make && make install

5.Copy template file as PHP And modify it
cp /opt/php-7.1.10/php.ini-development /usr/local/php7/php.ini	
#Use PHP. PHP when testing the environment Ini development file, while using PHP in the production environment Ini production file
vim /usr/local/php7/php.ini
--1170 that 's ok--modify
mysqli.default_socket = /usr/local/mysql/mysql.sock
--939 that 's ok--Uncomment, modify
date.timezone = Asia/Shanghai

6.Optimize handle PHP The executable program file is put into the directory of path environment variable for system identification
ln -s /usr/local/php7/bin/* /usr/local/bin/
php -m 			#See which modules PHP loads

7.modify httpd Service profile, let apache support PHP
vim /etc/httpd.conf 
--393 that 's ok--Insert the following
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
--255 that 's ok--Modify home page file name settings
DirectoryIndex index.html index.php

---Check support php7 Does the module exist------
LoadModule php7_module        modules/libphp7.so


8.verification 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 access
http://192.168.19.88

Practical operation ---- installation Forum

1.Create database and authorize
mysql -u root -p

CREATE DATABASE bbs;
#Create a database

GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';
#Grant the authority of all tables in the bbs database to bbsuser and set the password admin123

flush privileges;
#Refresh database

show databases;

2.Unzip the forum zip
unzip /opt/Discuz_X3.4_SC_UTF8.zip -d /opt/dis
cd /opt/dis/dir_SC_UTF8/
cp -r upload/ /usr/local/httpd/htdocs/bbs		#Upload site update package

3.Change the owner of the forum directory
ps aux							#The user name of the forum process is daemon
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

4.Browser access verification
 Forum page access
http://192.168.19.88/bbs
----------------------------------------------------------------------------------------------------------
Database server: localhost     ###localhost is used for local erection. How to fill in the IP address and port number instead of on the local machine
 Database name: bbs
 Database user name: bbsuser
 Database password: admin123
 administrator account :admin
 Administrator password:admin123
----------------------------------------------------------------------------------------------------------
Forum background administrator page
http://192.168.19.88/bbs/admin.php

Keywords: Web Development Linux MySQL

Added by Slashscape on Wed, 02 Feb 2022 20:41:41 +0200