Top level distribution analysis of LAMP architecture in Linux system

Posted by m5638829 on Sat, 05 Mar 2022 03:04:30 +0100

Top level distribution analysis of LAMP architecture in Linux system

theory

1, LAMP (Linux Apache MySQL PHP)

LAMP It has the advantages of universal, cross platform, high performance and low price LAMP Whether it is performance, quality or price, it is the preferred platform for enterprises to build websites

(1) Distribution interpretation

  • L stands for the server operating system using Linux
  • A represents that the website service uses httpd software in the Apache Software Foundation
  • M represents the MySQL database used in the background of the website
  • P stands for the website is developed in PHP/Perl/Python and other languages

The products are open source software, which is a mature architecture framework in the world. Many popular commercial applications adopt this architecture.

(2) In depth explanation

  • The client sends a request to connect to port 80 of the web server, and Apache processes the user's static request accordingly.
  • If the client requests dynamic resources, Apache loads and calls libphpx So module (Brought by installing php program) for parsing.
  • If the processing needs to communicate with the background database, it is completed by the php program.
  • The Php program returns the processed results to Apache and Apache returns them to the client.

operation

1, Apache build

(1) First, turn off the firewall

systemctl stop firewalld
setenforce 0

(2) Install environment dependent packages

be careful: yum The source needs to be configured
yum -y  install  gcc
yum -y  install  gcc-c++ make 
yum -y  install  pcre 
yum -y  install  pcre-devel 
yum -y  install  expat-devel 
yum -y  install  perl 

Concise approach:
yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel perl

(3) Software configuration parameters

Put the software package in the / opt directory

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
#Unzip package
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
#Change location and rename
cd /opt/httpd-2.4.29
./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi
##Software module pointing

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

Complete display:

(4) Compile

make
#If multiple cores can be compiled using make -j, the number of cores can be specified using geometry
make install						
#compile

The compilation is completed as shown in the figure:

(5) Path environment

ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/

(6) Add httpd service

vim /lib/systemd/system/httpd.service                   #Enter configuration file

[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
##Write the above contents into the configuration file

systemctl start httpd.service                           #Open service

(7) Modify httpd service configuration file

vim /etc/httpd.conf                                     #Enter configuration file

Line 52 change
Listen Local machine IP:80

Line 197 change and uncomment
ServerName domain name:80

Line 221 sets the default home page storage path
DocumentRoot "/usr/local/httpd/htdocs"

Line 255 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
systemctl restart httpd.service                         #Confirm that there is no error and restart the service

2, Mysql database construction

(1) Installation environment dependency

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

(2) Configuration software module

cd /opt
tar zxvf mysql-5.7.17.tar.gz
tar zxvf boost_1_59_0.tar.gz
mv boost_1_59_0 /usr/local/boost         #Move and 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

Completion diagram:

(3) Compilation and installation

Note: if the number of cores used for compilation does not match the running memory of the virtual machine, it is easy to report an error
make 
#If multiple cores can be compiled using make -j, the number of cores can be specified using geometry
make install

Completion diagram:

(4) Create mysql user

useradd -M -s /sbin/nologin  mysql

(5) mysql configuration file

vim /etc/my.cnf

[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
Command panel input after saving:
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, an error is thrown. When this value is not set, the default storage engine is used instead and an exception is thrown
STRICT_TRANS_TABLES
##In this mode, if a value cannot be inserted into a transaction table, the current operation will be interrupted and the non transaction table will not be restricted
NO_AUTO_CREATE_USER
##GRANT is prohibited from creating users with blank passwords
NO_AUTO_VALUE_ON_ZERO
##The self growing column in mysql can start from 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 database is not allowed 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 the data is divided by zero
PIPES_AS_CONCAT
##Treating "|" as a concatenation operator of a string rather than an or operator is the same as in Oracle database and is similar to the Concat function of a string
ANSI_QUOTES
 Enable ANSI_QUOTES You cannot use double quotation marks to refer to a string after, because it is interpreted as an identifier

(6) Change the primary group of mysql installation directory and configuration file

Note: if there is no such directory or file, the make install command may not run after compiling above

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

(7) Set path environment variable

echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
##Path variable	
source /etc/profile
##Update file

(8) Initialize database

cd /usr/local/mysql/bin/

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

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

(9) Add 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

(10) Modify the login password of mysql

mysqladmin -u root -p password "password"
#Set password for root account

(11) Authorize remote login

mysql -u root -p
mysql
grant all privileges on *.* to 'root'@'%' identified by 'xxxxxx';
#The root user is authorized to log in remotely at all terminals with the password of xxxxxx and have operation authority on all databases and tables
show databases;			#View existing databases

Completion diagram:

3, PHP build

(1) Installation environment dependency

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

(2) 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 #MySQL technology can not only call mysql, but also make the stored procedure of MySQL 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

Completion diagram:

(3) Compilation and installation

make 
#If multiple cores can be compiled using make -j, the number of cores can be specified using geometry
make install

(4) 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	
#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

Line 1170 change
mysqli.default_socket = /usr/local/mysql/mysql.sock

Line 939 and cancel comment
date.timezone = Asia/Shanghai

(5) Put the PHP executable program file 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

(6) Modify the configuration file of httpd service to make apache support PHP

vim /etc/httpd.conf 

Line 393 change and insert the following
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

Line 255: change and modify the file name setting of the first page
DirectoryIndex index.html index.php

So far, the LAMP architecture has been basically built

Topics: PHP Linux Operation & Maintenance MySQL Apache