Table of Contents
Introduction
prerequisites
Update system
Install Apache
Install MySQL 8 or MariaDB 10
Install PHP 7
Configure PHP and test the installation
Verify MySQL or MariaDB installation
Connect to MySQL or MariaDB
in conclusion
Home Database Mysql Tutorial Install Apache, MySQL 8 or MariaDB 10 and PHP 7 on CentOS 7

Install Apache, MySQL 8 or MariaDB 10 and PHP 7 on CentOS 7

Sep 13, 2023 pm 10:49 PM

在 CentOS 7 上安装 Apache、MySQL 8 或 MariaDB 10 和 PHP 7

Introduction

CentOS 7 is a popular Linux distribution known for its stability and security. If you want to set up a web server on CentOS 7, you may need to install Apache, MySQL or MariaDB and PHP. In this comprehensive guide, we'll walk you through the installation process of these components, along with examples and their output to ensure a successful installation.

prerequisites

Before we begin, please make sure you have the following prerequisites -

CentOS 7 installed on your server or virtual machine.

Root access or sudo permissions to execute commands with administrative privileges.

Update system

First, let's update the system to make sure we have the latest packages and dependencies.

Open the terminal and execute the following command -

sudo yum update -y

Install Apache

Apache is a widely used web server software. To install Apache on CentOS 7, follow these steps -

Enter the following command to install Apache -

sudo yum install httpd -y

After the installation is complete, start the Apache service -

sudo systemctl start httpd

To make Apache start automatically on boot, run the following command -

sudo systemctl enable httpd

Verify that Apache is running by accessing the server's IP address or domain name in a web browser. You should see the Apache default page.

Install MySQL 8 or MariaDB 10

Next, we need to install a relational database management system. You can choose MySQL 8 or MariaDB 10 according to your preference. Here we will cover both installations.

For MySQL 8 -

Execute the following command to install the MySQL repository -

sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm -y

Install MySQL 8 by running the following command -

sudo yum install mysql-server -y

After the installation is complete, start the MySQL service -

sudo systemctl start mysqld

To protect your MySQL installation, run the security script -

sudo mysql_secure_installation

For MariaDB 10 -

Enter the following commands to install MariaDB -

sudo yum install mariadb-server -y

Start MariaDB service -

sudo systemctl start mariadb

Protect MariaDB installation using secure scripts -

sudo mysql_secure_installation

Install PHP 7

PHP is a server-side scripting language required for dynamic web development. To install PHP 7 on CentOS 7, follow these steps -

Execute the following command to install PHP and its extensions -

sudo yum install php php-mysqlnd -y

After the installation is complete, restart the Apache service to enable PHP -

sudo systemctl restart httpd

To test whether PHP is working properly, create a PHP information file. Create a file named info.php in the default web server directory using the following command -

sudo nano /var/www/html/info.php

In the editor, add the following PHP code -

<?php
   phpinfo();
?>

Save the file and exit the editor. Now, visit http://your_server_IP_address/info.php in your web browser. You should see a PHP Information page showing details about your PHP installation.

Configure PHP and test the installation

In order to further configure PHP, you can modify the PHP configuration file as needed. The configuration file for PHP on CentOS 7 is located in /etc/php.ini. You can open and edit the file using a text editor such as nano or vi.

For example, to increase the maximum file upload size, search for the upload_max_filesize directive in the php.ini file and modify it to your desired value. Save changes and restart Apache for modifications to take effect -

sudo systemctl restart httpd

Now, let's test the PHP installation by creating a simple PHP script. Create a new file called test.php in the default web server directory -

sudo nano /var/www/html/test.php

Add the following PHP code to the file -

<?php
   echo "PHP is working correctly!";
?>

Save the file and exit the editor. Now, visit http://your_server_IP_address/test.php in your web browser. You should see the message "PHP is working fine!" displayed on the page, confirming that PHP is installed and configured correctly.

Verify MySQL or MariaDB installation

To ensure that MySQL 8 or MariaDB 10 was installed successfully, you can perform a simple check.

For MySQL, execute the following command -

sudo systemctl status mysqld

If MySQL is running normally, you should see an output indicating that the service is active and running.

For MariaDB, run the following command -

sudo systemctl status mariadb

If MariaDB is running normally, you will see the service status as Active.

Connect to MySQL or MariaDB

To connect to MySQL or MariaDB from the command line, you can use the MySQL client utility. Open a terminal and enter the following command -

mysql -u root -p

You will be prompted to enter your MySQL or MariaDB root password. Once authenticated, you will enter the MySQL or MariaDB command line interface.

To verify the connection, you can execute a simple query. For example, let's show all databases -

SHOW DATABASES;

This command will display the list of databases available in the MySQL or MariaDB server.

in conclusion

In this article, we cover the step-by-step installation process of Apache, MySQL 8 or MariaDB 10 and PHP 7 on CentOS 7. We also provide examples and output to help you understand the installation and configuration steps.

The above is the detailed content of Install Apache, MySQL 8 or MariaDB 10 and PHP 7 on CentOS 7. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to audit database activity in MySQL? How to audit database activity in MySQL? Aug 05, 2025 pm 01:34 PM

UseMySQLEnterpriseAuditPluginifonEnterpriseEditionbyenablingitinconfigurationwithserver-audit=FORCE_PLUS_PERMANENTandcustomizeeventsviaserver_audit_events;2.Forfreealternatives,usePerconaServerorMariaDBwiththeiropen-sourceauditpluginslikeaudit_log;3.

How to use check constraints to enforce data rules in MySQL? How to use check constraints to enforce data rules in MySQL? Aug 06, 2025 pm 04:49 PM

MySQL supports CHECK constraints to force domain integrity, effective from version 8.0.16; 1. Add constraints when creating a table: Use CREATETABLE to define CHECK conditions, such as age ≥18, salary > 0, department limit values; 2. Modify the table to add constraints: Use ALTERTABLEADDCONSTRAINT to limit field values, such as name non-empty; 3. Use complex conditions: support multi-column logic and expressions, such as end date ≥start date and completion status must have an end date; 4. Delete constraints: use ALTERTABLEDROPCONSTRAINT to specify the name to delete; 5. Notes: MySQL8.0.16, InnoDB or MyISAM needs to be quoted

How to implement a tagging system in a MySQL database? How to implement a tagging system in a MySQL database? Aug 05, 2025 am 05:41 AM

Useamany-to-manyrelationshipwithajunctiontabletolinkitemsandtagsviathreetables:items,tags,anditem_tags.2.Whenaddingtags,checkforexistingtagsinthetagstable,insertifnecessary,thencreatemappingsinitem_tagsusingtransactionsforconsistency.3.Queryitemsbyta

Best Practices for Managing Large MySQL Tables Best Practices for Managing Large MySQL Tables Aug 05, 2025 am 03:55 AM

When dealing with large tables, MySQL performance and maintainability face challenges, and it is necessary to start from structural design, index optimization, table sub-table strategy, etc. 1. Reasonably design primary keys and indexes: It is recommended to use self-increment integers as primary keys to reduce page splits; use overlay indexes to improve query efficiency; regularly analyze slow query logs and delete invalid indexes. 2. Rational use of partition tables: partition according to time range and other strategies to improve query and maintenance efficiency, but attention should be paid to partitioning and cutting issues. 3. Consider reading and writing separation and library separation: Read and writing separation alleviates the pressure on the main library. The library separation and table separation are suitable for scenarios with a large amount of data. It is recommended to use middleware and evaluate transaction and cross-store query problems. Early planning and continuous optimization are the key.

How to use the COALESCE() function in MySQL? How to use the COALESCE() function in MySQL? Aug 14, 2025 pm 06:15 PM

COALESCE()returnsthefirstnon-NULLvaluefromalistofexpressions,enablinggracefulhandlingofmissingdatabysubstitutingdefaults,mergingcolumnvalues,supportingcalculationswithoptionalfields,andprovidingfallbacksinjoinsandaggregations,ensuringpredictableresul

How to add a primary key to an existing table in MySQL? How to add a primary key to an existing table in MySQL? Aug 12, 2025 am 04:11 AM

To add a primary key to an existing table, use the ALTERTABLE statement with the ADDPRIMARYKEY clause. 1. Ensure that the target column has no NULL value, no duplication and is defined as NOTNULL; 2. The single-column primary key syntax is ALTERTABLE table name ADDPRIMARYKEY (column name); 3. The multi-column combination primary key syntax is ALTERTABLE table name ADDPRIMARYKEY (column 1, column 2); 4. If the column allows NULL, you must first execute MODIFY to set NOTNULL; 5. Each table can only have one primary key, and the old primary key must be deleted before adding; 6. If you need to increase it yourself, you can use MODIFY to set AUTO_INCREMENT. Ensure data before operation

How to show all databases in MySQL How to show all databases in MySQL Aug 08, 2025 am 09:50 AM

To display all databases in MySQL, you need to use the SHOWDATABASES command; 1. After logging into the MySQL server, you can execute the SHOWDATABASES; command to list all databases that the current user has permission to access; 2. System databases such as information_schema, mysql, performance_schema and sys exist by default, but users with insufficient permissions may not be able to see it; 3. You can also query and filter the database through SELECTSCHEMA_NAMEFROMinformation_schema.SCHEMATA; for example, excluding the system database to only display the database created by users; make sure to use

How to Troubleshoot Common MySQL Connection Errors? How to Troubleshoot Common MySQL Connection Errors? Aug 08, 2025 am 06:44 AM

Check whether the MySQL service is running, use sudosystemctlstatusmysql to confirm and start; 2. Make sure that bind-address is set to 0.0.0.0 to allow remote connections and restart the service; 3. Verify whether the 3306 port is open, check and configure the firewall rules to allow the port; 4. For the "Accessdenied" error, you need to check the user name, password and host name, and then log in to MySQL and query the mysql.user table to confirm permissions. If necessary, create or update the user and authorize it, such as using 'your_user'@'%'; 5. If authentication is lost due to caching_sha2_password

See all articles