LAMP yum installation - Centos 7

Keywords: MySQL yum vim RPM

Centos 7 yum install LAMP

Author: Yan Tao


Note: the experimental environment is virtual machine, selinux and firewalld have been shut down

Official Apache 2.4 documentation

#View port
[root@localhost ~]# firewall-cmd --query-port=80/tcp

#Open port
[root@localhost ~]# firewall-cmd --permanent --add-port=80/tcp

#Reload rule
[root@localhost ~]# firewall-cmd --reload 

#View again
[root@localhost ~]# firewall-cmd --query-port=80/tcp

1. Apache

1.1 delete the original yum source and replace the Alibaba source

[root@localhost ~]# rm -rf /etc/yum.repos.d/*
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo
[root@localhost ~]# yum clean all 
[root@localhost ~]# yum makecache

1.2 installing httpd

[root@localhost ~]# yum install httpd

1.3 start httpd

[root@localhost ~]# systemctl start httpd.service 

1.4 setting httpd startup

[root@localhost ~]# systemctl enable httpd.service 

1.5 backup and edit httpd configuration file

[root@localhost ~]# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak 
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
#You can change the server name without changing it, but always give an AH00558 prompt
//Change to
ServerName localhost:80
#Preferentially parsing php files
<IfModule dir_module>
    DirectoryIndex index.html
#Change to
<IfModule dir_module>
    DirectoryIndex index.php  index.html

1.6 check configuration file syntax

[root@localhost ~]# httpd -t
Syntax OK

1.7 restart httpd

[root@localhost ~]# systemctl restart httpd.service

2. PHP

2.1 install epel source and ius source

ius source depends on epel source

[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo
[root@localhost ~]# wget
[root@localhost ~]# ls
anaconda-ks.cfg  ius-release.rpm
[root@localhost ~]# yum install ius-release.rpm 
[root@localhost ~]# yum clean all 
[root@localhost ~]# yum makecache
#Check whether all yum sources are installed
[root@localhost ~]# ls /etc/yum.repos.d/
CentOS-Base.repo  epel.repo  ius-archive.repo  ius.repo  ius-testing.repo

2.2 installing php7.2

[root@localhost ~]# yum install php72u*

2.3 creating php files

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim index.php

Enter the following


Restart apache, the browser input ip can see the php information.

3. MySQL


3.1 download and install the yum source of mysql

[root@localhost ~]# wget
[root@localhost ~]# yum install mysql80-community-release-el7-3.noarch.rpm 
#/etc/ yum.repos MySQL will be generated in the. D / directory- community.repo

3.2 yum file of MySQL

mysql8 is enabled by default for this yum source. Here is how to open other versions

[root@localhost ~]# yum list |grep mysql-community-server
mysql-community-server.x86_64             8.0.19-1.el7                 mysql80-community
[root@localhost ~]# vim /etc/yum.repos.d/mysql-community.repo 

To download that version, just enable = 1

# Enable to use MySQL 5.5
name=MySQL 5.5 Community Server

# Enable to use MySQL 5.6
name=MySQL 5.6 Community Server

# Enable to use MySQL 5.7
name=MySQL 5.7 Community Server

name=MySQL 8.0 Community Server

I installed version 5.6

[root@localhost ~]# yum list |grep mysql-community-server
mysql-community-server.x86_64             5.6.47-2.el7                 mysql56-community

3.3 install mysql

[root@localhost ~]# yum install mysql-community-server.x86_64 

3.4 starting mysql

[root@localhost ~]# systemctl start mysqld.service 

3.5 startup

[root@localhost ~]# systemctl enable mysqld.service

3.6 initialization

Use this under version 5.7

[root@localhost ~]# mysql_secure_installation 

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Cleaning up...

3.7 creating a telnet user

mysql>  create user 'yantao'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql>  grant all on *.* to 'yantao'@'%';
Query OK, 0 rows affected (0.00 sec)

4. Apache experiment

1. Virtual host


Apache does not support multiple https virtual hosts

1.1 prepare two domain names

1.2 generate Host Directory

Create l and t directories and index.html

[root@localhost ~]# mkdir /var/www/html/web1
[root@localhost ~]# vim /var/www/html/web1/index.html 
#E.g. down entry

[root@localhost ~]# mkdir /var/www/html/web2
[root@localhost ~]# vim /var/www/html/web2/index.html 
#E.g. down entry

1.3 modify configuration file

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf 
#The bottom line is to open and load the sub configuration file under conf.d/
IncludeOptional conf.d/*.conf
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim web1.conf
#Write the following
<VirtualHost *:80>
    DocumentRoot "/var/www/html/web1"

[root@localhost conf.d]# vim web2.conf 
<VirtualHost *:80>
    DocumentRoot "/var/www/html/web2"

1.4 check syntax

[root@localhost ~]# httpd -t
Syntax OK

1.5 restart apache

[root@localhost ~]# systemctl restart httpd.service 

1.6 modify the hosts file of the computer

#Add the following

This is to use a browser to access these two domain names

2. Hide version number

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf 
#Add the following at the bottom to hide the Apache version
ServerTokens Prod
ServerSignature Off

#Hide php version
[root@localhost ~]# vim /etc/php.ini 
expose_php = On
#Amend to read
expose_php = Off

3. User authentication

reference resources

Create a new first

3.1 generate account file

Create a new directory to store account files
[root@localhost ~]# mkdir /etc/httpd/passwd
#Create account file
[root@localhost ~]# htpasswd -c /etc/httpd/passwd/web3 yantao
New password: 
Re-type new password: 
Adding password for user yantao

3.2 modify virtual host file

[root@localhost ~]# vim /etc/httpd/conf.d/web3.conf 
<Directory /var/www/html/web3>
    AuthType Basic
    AuthName "Restricted Files"
    AuthUserFile /etc/httpd/passwd/web3
    Require user yantao
<VirtualHost *:80>
    DocumentRoot "/var/www/html/web3"

Check syntax, restart

4. Domain name jump

Let web2 jump to web1

4.1 modify web2 virtual host file

[root@localhost ~]# vim /etc/httpd/conf.d/web2.conf
#as follows
<Directory /var/www/html/web2>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted

<VirtualHost *:80>
    DocumentRoot "/var/www/html/web2"

4.2 create. htaccess in the web2 directory

[root@localhost ~]# vim /var/www/html/web2/.htaccess
#as follows
#Turn on rewrite function
RewriteEngine on

#hold  The beginning content is assigned to HTTP_HOST variable
RewriteCond %{HTTP_HOST} ^

RewriteRule ^(.*)$1 [R=permanent,L]
# ^(. *) $refers to the resource to be accessed by the client
# $1 assigns the * reference to the $1 variable
# Rpermanent permanent redirection = 301
# L specifies that this rule is the last one to take effect, and the following one will no longer take effect

Check syntax, restart


a: Https ensures that all packets transmitted through the server are encrypted.

b: An official digital certificate certificate has been established, which makes the fake server unable to impersonate the real server.

Certificate source

1. Self signed certificates are recommended for testing purposes and personal projects. Self signed certificates can also be used by service providers, but they are generally used when users trust each other. In addition, self signed certificates do not cost money to purchase.

2. Certificates can be provided by community-based authentication providers such as StartSSL and CACERT methods. These certificates also do not cost money, but are recommended for personal projects.

3. For global business websites, it is recommended to purchase certificates from reputable certification authorities. These certificates cost money, but they add to the credibility of network service providers.

We use self signed certificate, openssl to generate Certificate (installed), mod_ssl implementation HTTPS (not installed)

Install mod_ssl

[root@localhost ~]# yum install mod_ssl
#After installation, mod will be generated in / etc/httpd/modules_

5.1 generate private key

#Generate 2048 bit encrypted private key
[root@localhost ~]# openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
e is 65537 (0x10001)

5.2 generate certificate signing request

[root@localhost ~]# openssl req -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:LN
Locality Name (eg, city) [Default City]:YS
Organization Name (eg, company) [Default Company Ltd]:CompanyName
Organizational Unit Name (eg, section) []:PHP
#domain name
Common Name (eg, your name or your server's hostname) []
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

5.3 generate self signed certificate

#Generate a self signed certificate of type X509. The validity period is set to 3650 days, i.e. 10 years
[root@localhost ~]# openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
Signature ok
Getting Private key

5.4 put three files in corresponding directory

[root@localhost ~]# cp server.crt /etc/pki/tls/certs/
[root@localhost ~]# cp server.key /etc/pki/tls/private/
[root@localhost ~]# cp server.csr /etc/pki/tls/private/

5.5 modification ssl.conf

#SSLCertificateFile /etc/pki/tls/certs/localhost.crt
#SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
//Amend to read
SSLCertificateFile /etc/pki/tls/certs/server.crt
SSLCertificateKeyFile /etc/pki/tls/private/server.key

5.6 virtual host profile

[root@localhost ~]# vim /etc/httpd/conf.d/web1.conf 
<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile "/etc/pki/tls/certs/server.crt"
    SSLCertificateKeyFile "/etc/pki/tls/private/server.key"

    DocumentRoot /var/www/html/web1

Check the syntax after configuration and restart apache

5.7 open port 443

Method as above

visit Select the trust certificate to browse the website

5.8 force https jump

[root@localhost ~]# vim /etc/httpd/conf.d/web1.conf 
<VirtualHost *:80>
    Redirect permanent /

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile "/etc/pki/tls/certs/server.crt"
    SSLCertificateKeyFile "/etc/pki/tls/private/server.key"

    DocumentRoot /var/www/html/web1

6. Static cache

[root@localhost ~]# vim /etc/httpd/conf.d/web1.conf 
<VirtualHost *:80>
    Redirect permanent /

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile "/etc/pki/tls/certs/server.crt"
    SSLCertificateKeyFile "/etc/pki/tls/private/server.key"

    DocumentRoot /var/www/html/web1

<IfModule mod_expires.c>
        ExpiresActive on
        ExpiresByType image/jpeg "access plus 24 hours"
        ExpiresByType image/png "access plus 24 hours"
        ExpiresByType text/css  "now plus 2 hours"
        ExpiresByType application/javascript "now plus 2 hours"

Posted by Linjon on Sun, 21 Jun 2020 03:01:40 -0700