Post

Installing LAMP Stack and WordPress on CentOS Stream 9

Last Updated: March 20, 2024

Introduction –

In this tutorial, we’ll walk you through the process of setting up a LAMP (Linux, Apache, MySQL, PHP) stack on CentOS Stream 9 and installing WordPress. We’ll also include all the necessary commands in a single code block to automate the installation process.

For a solid foundation for your LAMP stack and WordPress, selecting a robust server is crucial. Models like the Dell PowerEdge R620 Server or the Dell PowerEdge T320 are top choices for their affordability, reliability and performance.

Step 1: Update the System

Start by ensuring your CentOS system is up to date:

1
sudo yum update -y

Step 2: Install Apache Web Server

Install the Apache web server:

1
sudo yum install httpd -y

Start and enable Apache to run on boot:

1
2
sudo systemctl start httpd
sudo systemctl enable httpd

Step 3: Install MySQL Database

Install MySQL server:

1
sudo yum install mysql-server -y

Start and enable MySQL:

1
2
sudo systemctl start mysqld
sudo systemctl enable mysqld

Run the MySQL secure installation script to set the root password and secure your MySQL installation:

1
sudo mysql_secure_installation

Step 4: Install PHP

Install PHP and necessary extensions:

1
sudo yum install php php-mysqlnd -y

Step 5: Create a MySQL Database and User for WordPress

Log in to the MySQL shell:

1
sudo mysql -p

Create a new database for WordPress:

1
CREATE DATABASE wordpress;

Create a new user and grant privileges to the WordPress database:

1
2
3
4
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Replace 'your_password' with a strong password.

Step 6: Install WordPress

Download and extract WordPress to your web directory:

1
2
3
4
sudo yum install wget -y
sudo yum install tar -y
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz -C /var/www/html/

Copy the sample configuration file:

1
sudo cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php

Edit the wp-config.php file to include your MySQL database details:

1
sudo vi /var/www/html/wordpress/wp-config.php

Step 7: Configure Apache for WordPress

Create an Apache configuration file for your WordPress site:

1
sudo vi /etc/httpd/conf.d/wordpress.conf

Add the following content, and save the file:

1
2
3
4
5
6
7
8
9
<VirtualHost *:80>
    ServerAdmin webmaster@your_domain.com
    DocumentRoot /var/www/html/wordpress
    ServerName your_domain.com
    ServerAlias www.your_domain.com

    ErrorLog /var/log/httpd/wordpress_error.log
    CustomLog /var/log/httpd/wordpress_access.log combined
</VirtualHost>

Replace your_domain.com with your actual domain name. Restart Apache:

1
sudo systemctl restart httpd

Step 8: Set File Permissions and Ownership

Set the correct file permissions and ownership for your WordPress installation:

1
2
3
4
sudo chown -R apache:apache /var/www/html/wordpress
sudo chmod 644 /var/www/html/wordpress/*
sudo chmod 755 /var/www/html/wordpress
sudo systemctl restart httpd

Step 9: Enable Firewall Rules

If you’re using the firewalld service, enable HTTP and HTTPS rules:

1
2
3
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload

Step 10: Access WordPress

Open a web browser and navigate to your server’s IP address or domain name. Follow the WordPress setup wizard to complete the installation.

Conclusion

That’s it! You’ve successfully installed the LAMP stack and WordPress on CentOS Stream 9.

Here’s a single code block with all the commands you need:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
sudo yum update -y
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
sudo yum install mysql-server -y
sudo systemctl start mysqld
sudo systemctl enable mysqld
sudo mysql_secure_installation
sudo yum install php php-mysqlnd -y
# Note: You should run the MySQL commands within the MySQL shell, not in the bash shell.
# Login to MySQL:
sudo mysql -p
# Then within the MySQL shell, run the following:
# CREATE DATABASE wordpress;
# CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'your_password';
# GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost';
# FLUSH PRIVILEGES;
# EXIT;
sudo yum install wget -y
sudo yum install tar -y
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz -C /var/www/html/
sudo cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php
# Before running this, replace database_name_here, username_here, and password_here with your details.
sudo sed -i 's/database_name_here/wordpress/g' /var/www/html/wordpress/wp-config.php
sudo sed -i 's/username_here/wordpressuser/g' /var/www/html/wordpress/wp-config.php
sudo sed -i 's/password_here/your_password/g' /var/www/html/wordpress/wp-config.php
# Insert proper DB details in wp-config.php before proceeding.
# After configuring wp-config.php, create the Apache config file with your domain information.
# Add the VirtualHost configuration to the wordpress.conf file.
echo '<VirtualHost *:80>
    ServerAdmin webmaster@your_domain.com
    DocumentRoot /var/www/html/wordpress
    ServerName your_domain.com
    ServerAlias www.your_domain.com

    ErrorLog /var/log/httpd/wordpress_error.log
    CustomLog /var/log/httpd/wordpress_access.log combined
</VirtualHost>' | sudo tee /etc/httpd/conf.d/wordpress.conf
sudo systemctl restart httpd
sudo chown -R apache:apache /var/www/html/wordpress
sudo find /var/www/html/wordpress/ -type d -exec chmod 755 {} \;
sudo find /var/www/html/wordpress/ -type f -exec chmod 644 {} \;
sudo systemctl restart httpd
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload

Remember to replace 'your_password' and your_domain.com with your own values.



Featured Tweet

This post is licensed under Apache License 2.0 by the author.