Apache is a popular Linux-based web server application. It is part of the LAMP stack (Linux, Apache, MySQL, PHP) that powers much of the internet. This guide will show you how to install Apache, MySql, PHP and phpMyAdmin on your latest CentOS 8 webServer.
Prerequisites
A system running CentOS 8 LinuxAccess to a terminal window / command line (Ctrl–Alt–F2)
A user account with sudo or root privileges
Update Software Repository
Open a terminal window, and update the repository package lists by entering the following:
# sudo yum update
Install Apache
Step 1: Install Apache
Now you can install Apache with the command:
# sudo yum -y install httpd
Step 2: Start and Manage Apache Web Server
Apache is a service that runs in the background.
Start the Apache service by entering the following:
# sudo systemctl start httpd
To configure Apache to run on startup:
# sudo systemctl enable httpd
To check the status of the Apache service:
# sudo systemctl status httpd
To reload Apache (reloads configuration files to apply changes):
# sudo systemctl reload httpd
To restart the entire Apache service:
# sudo systemctl restart httpd
To stop Apache:
# sudo systemctl stop httpd
To disable Apache at system startup:
# sudo systemctl disable httpd
Step 3: Test Apache Web Server
Your Apache software’s job is to serve web pages over a network. Your new Apache installation has a default test page, but you can also create a custom test page.
Check the Default Apache Test Page
In a terminal window, find your system’s IP address with the following:
# hostname -I | awk '{print $1}'
If you’re familiar with the ip address show or ifconfig commands, you can use those instead.
Open a web browser and type in the IP address displayed in the output. The system should show the Apache HTTP Server Test Page.
Create an HTML File to Test
If, for some reason, you need or have a custom HTML page you want to use as a test page, do the following:
In a terminal window, create a new HTML index file in /var/www/html/ folder named as index.html , type html content and save it. Your Apache server is working correctly if it displays the specified custom page.
Step 4: Adjust Firewall for Apache
The firewall on your system blocks traffic on different ports. Each port has a number, and different kinds of traffic use different ports. For your web server, you’ll need to allow HTTP and HTTPS traffic on ports 80 and 443 (respectively).
In a terminal, enter the following:
# sudo firewall-cmd --permanent --zone=public --add-service=http
# sudo firewall-cmd --permanent --zone=public --add-service=https
# sudo firewall-cmd --permanent --add-port=80/tcp
# sudo firewall-cmd --permanent --add-port=443/tcp
Reload the firewall
# sudo firewall-cmd --reload
Double-check to make sure your firewall is correctly configured:
# sudo firewall-cmd --list-all | grep services
You should see http and https in the list of allowed services.
Apache Files and Directories
Apache is controlled by applying directives in configuration files:
/etc/httpd/conf/httpd.conf – Main Apache config file
/etc/httpd/ – Location for all config files
/etc/httpd/conf.d/ – All config files in this directory are included in the main confog file
/etc/httpd/conf.modules.d/ – Location for Apache module config files
Diligently check Apache log files to monitor your web server:
/var/log/httpd/ – Location of Apache log files
/var/log/httpd/access_log – Shows a log of systems that accessed the server
/var/log/httpd/error_log – Shows a list of any errors Apache encounters
Designate a directory to store the files for your website. Use the configuration files to point to the directory you choose. Some typical locations include:
/home/username/my_website
/var/www/my_website
/var/www/html/my_website
/opt/my_website
Please note that when making changes to configuration files, remember to always restart the Apache service to apply the new configuration.
Finally modify permission to ensure that read access is permitted to the general web directory, and all of the files and folders inside so that pages can be server correctly.
Installing MySQL
Step 1: Download and install MySQL
On CentOS 8, MySQL version 8 is available from the default repositories.
Run the following command to install the mysql-server package and a number of its dependencies:
# sudo dnf install mysql-server
When prompted, press y and then ENTER to confirm that you want to proceed:
With that, MySQL is installed on your server but it isn’t yet operational. The package you just installed configures MySQL to run as a systemd service named mysqld.service. In order to use MySQL, you will need to start it with the systemctl command:
# sudo systemctl start mysqld.service
To check that the service is running correctly, run the following command.
# sudo systemctl status mysqld
If MySQL was successfully started, the output will show that the MySQL service is active:
Next, set MySQL to start whenever the server boots up with the following command:
# sudo systemctl enable mysqld
If you ever want to change this behavior and disable MySQL from starting up at boot, you can do so by running:
# sudo systemctl disable mysqld
MySQL is now installed, running, and enabled on your server. Next, we’ll go over how to harden your database’s security using a shell script that came preinstalled with your MySQL instance.
Step 2: Get the temporary MySQL root password
When installing MySQL on CentOS, a temporary root password is generated. Issue the command below to see it:
# sudo grep 'password' /var/log/mysqld.log
Step 3: Securing MySQL
MySQL includes a security script that allows you to change some default configuration options in order to improve MySQL’s security.
To use the security script, run the following command:
# sudo mysql_secure_installation
This will take you through a series of prompts asking if you want to make certain changes to your MySQL installation’s security options. The first prompt will ask whether you’d like to set up the Validate Password Plugin, which you can use to test the strength of your MySQL password.
If you elect to set up the Validate Password Plugin, the script will ask you to choose a password validation level. The strongest level — which you select by entering 2 — will require your password to be at least eight characters long and include a mix of uppercase, lowercase, numeric, and special characters.
Regardless of whether you choose to set up the Validate Password Plugin, the next prompt will be to set a password for the MySQL root user. Enter and then confirm a secure password of your choice.
If you used the Validate Password Plugin, you’ll receive feedback on the strength of your new password. Then the script will ask if you want to continue with the password you just entered or if you want to enter a new one. Assuming you’re satisfied with the strength of the password you just entered, enter Y to continue the script.
Following that, you can press Y and then ENTER to accept the defaults for all the subsequent questions. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes you have made.
With that, you’ve installed and secured MySQL on your CentOS 8 server. As a final step, we will test that the database is accessible and working as expected.
Step 4: Testing MySQL
You can verify your installation and get information about it by connecting with the mysqladmin tool, a client that lets you run administrative commands. Use the following command to connect to MySQL as root (-u root), prompt for a password (-p), and return the installation’s version.
# mysqladmin -u root -p version
If you’d like to connect to MySQL and begin adding data to it, run the following:
# mysql -u root -p
Like the previous mysqladmin command, this command includes the -u option, which allows you to specify the user you’d like to connect as (root in this case), and the -p option, which tells the command to prompt you for the user password you set in the previous step.
After you enter your root MySQL user’s password, you will see the MySQL prompt:
mysql>
From there, you can begin using your MySQL installation to create and load databases and start running queries.
Type quit to exit mysql prompt.
Installing phpMyAdmin
Step 1: Install PHP
phpMyAdmin requires php to execute. You must install php first on your centos 8 installation. Type the following command io install php on your machine.
# sudo dnf install php php-pdo php-pecl-zip php-json php-mbstring php-mysqlnd
Step 2: Install phpMyAdmin
Your system is ready for the phpMyAdmin installation. Download the latest phpMyAdmin archive from the official download page, or use the below commands to download phpMyAdmin 5.0.1 on your system.
After downloading extract archive and move to the proper location.
# wget https://files.phpmyadmin.net/phpMyAdmin/5.0.1/phpMyAdmin-5.0.1-all-languages.zip
# unzip phpMyAdmin-5.0.1-all-languages.zip
# mv phpMyAdmin-5.0.1-all-languages /usr/share/phpmyadmin
Then create tmp directory and set the proper permissions.
# mkdir /usr/share/phpmyadmin/tmp
# chown -R apache:apache /usr/share/phpmyadmin
# chmod 777 /usr/share/phpmyadmin/tmp
Step 3: Configure phpMyAdmin
Now, you need to configure web server to serve phpMyAdmin on network. Create Apache configuration file for phpMyAdmin and edit in text editor:
# vi /etc/httpd/conf.d/phpmyadmin.conf
add the below content to file.
Alias /phpmyadmin /usr/share/phpmyadmin
<Directory /usr/share/phpmyadmin/>
AddDefaultCharset UTF-8
<IfModule mod_authz_core.c>
# Apache 2.4
<RequireAny>
Require ip 127.0.0.1
Require ip ::1
#Require all granted
</RequireAny>
</IfModule>
</Directory>
<Directory /usr/share/phpmyadmin/setup/>
<IfModule mod_authz_core.c>
# Apache 2.4
<RequireAny>
Require ip 127.0.0.1
Require ip ::1
#Require all granted
</RequireAny>
</IfModule>
</Directory>
Save your file and close it using :wq command. The systems with SELinux enabled needs to set proper permissions to allow SELinux policies
# chcon -Rv --type=httpd_sys_content_t /usr/share/phpmyadmin/*
After making all the changes, make sure to start the Apache service to reload all settings.
# systemctl restart httpd.service
Step 4: Adjust Firewall
The systems with enabled firewalls need to allow HTTP service from the firewall. Run the below commands to open a port for the webserver in the firewall.
# sudo firewall-cmd --permanent --add-service=http
# sudo firewall-cmd --reload
Step 5: Access phpMyAdmin
All done. Open Forefox browser from applications and type 127.0.0.1/phpmyadmin or localhost/phpmyadmin in the address bar.
If you use specific IP in /etc/httpd/conf.d/phpmyadmin.conf file , type that IP instead of 127.0.0.1 or localhost.
If you want to use specific IP in /etc/httpd/conf.d/phpmyadmin.conf file and don't know your machine's IP, find your system’s IP address with the following command and replace those lines having Require ip 127.0.0.1 with that. Say Require ip 192.168.122.1.
# hostname -I | awk '{print $1}'
Now type the IP followed by /phpmyadmin . E.g. in your browser's address bar.
192.168.122.1/phpmyadmin
Finally:
If you unable to log in with error message 'Cannot log in to the MySQL server', and able to login in with this command:
# mysql -u root -p
form terminal, run this command in mysql terminal:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
0 comments:
Post a Comment