How to Install WordPress On CentOS 7

Step 1) Preflight Check:

This article assumes you have CentOS 7 installed and are logged into your server as the root user. We are also using the most recent version of WordPress for this install. This article also assumes you have already installed LAMP (Linux, Apache, MySQL, PHP) on your server and your PHP is updated to the latest version. You will also need to know your root mysql password so you can log into mysql as root.

Need help to install LAMP? Click this link!

mysql

 

If the above command isn’t working for you, then you may not be logged in as root user. In that case, you can run the following instead.

mysql -u root -p

 

Keep in mind that you will be prompted for the password you set earlier as root when you installed MySQL so you will need to have that handy. If you do not have your root password, you will have to reset it. If you need help with that, check out this article here and scroll down to the method via “Reset using command line“. Once you do gain access, you can safely move to step 2!

 

A successful login should look something like this:

login

 

 

 

 

 

Note:
To exit mysql at any time simply type: exit

exit

 

Step 2) Create a database

Hooray! We successfully logged in. Now we can create a database with the following command. You can call it whatever you would like, but for this article, we are calling it WordPress.

CREATE DATABASE wordpress;

Note:
While it’s easier to copy and paste directly in the command line. You should know that every mysql command will require a ;  (colon) at the end of every statement in case you are getting an error.

Once our database is created, you need to create a user for that database. Once again I am using a very simple username, and password so feel free to make yours more secure. Do remember your username and password as we will need it later in this article. Type the following command.

CREATE USER adminuser@localhost IDENTIFIED BY 'password';

At this point, we have created a database user however, we still need to give that user permissions to access the database. We can add those permissions with the following command:

GRANT ALL PRIVILEGES ON wordpress.* TO adminuser@localhost IDENTIFIED BY 'password';
Replace anything in red with your database name, user, and password. Also, note that the password is contained in single quotes followed by a colon;

Note:
Remember your username and password you use in this step as will be needed later.

Now we want to flush MySQL so that it is made aware of those changes.

FLUSH PRIVILEGES;

 

and finally, exit MySQL

exit

 

Step 3) Install WordPress

 

cd ~
wget http://wordpress.org/latest.tar.gz

 

Note:
if you do not have wget yet you can download it with yum using the following:

 

yum install wget

 

Now, let’s  unzip that tar file

tar -xzvf latest.tar.gz

 

That should create a file named WordPress in our home directory. Next, we want to move that file and its contents to our public_html folder, so it can serve up the content for our website. We want to keep the same file permission, so we use the following rsync command.

sudo rsync -avP ~/wordpress/ /var/www/html/

For WordPress to be able to upload files, we need to create an uploads directory. Go ahead and use the following:

mkdir /var/www/html/wp-content/uploads

 

Lastly, update the Apache permissions for new WordPress files

sudo chown -R apache:apache /var/www/html/*

Step 4) Configuring WordPress

Next, we have to update the wp-config.php file in WordPress for it to connect to the database successfully. So let’s go to the html folder where your WordPress install is located.

cd /var/www/html

Create a wp-config.php file by copying the sample file WordPress has provided.

cp wp-config-sample.php wp-config.php

Now, we need to edit the new wp-config.php file with the correct database information we created in Step 1.

I used vim to make that change, but you can use any editor you are comfortable with.

vim  wp-config.php

Next, we need to add info into the following fields of the databaseuser, and password we created in Step 1.

Old Settings:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );


New Settings:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );
/** MySQL database username */
define( 'DB_USER', 'adminuser' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password' );

Once you have made those changes, go ahead and save the file using the :wq command in Vim.

 

Step 5) Setup through wp-admin and verification

Now, let’s verify that your WordPress install is working. You should see something like the following on your server page. Replace server_domain_name_or_IP with your server name or IP.

http://server_domain_name_or_IP/wp-admin

install

 

If this is what you see then congrats!!! You have successfully installed WordPress on your Centos server, and you can close this article!

 

I have also listed a couple of common issues you might encounter in the sections below. Keep in mind that we cannot list every possible issue but, we have listed what we believe are the most common problems you may run into.

 

 

Database Error Establishing a Connection

If you are getting an Error about establishing a Database connection, verify that your wp-config.php file has the correct userpassword and database name. There should be no spaces between the single quotes! This error usually means you have something going on in the wp-config.php file so be sure to check your syntax.

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );


Troubleshooting and Verifying php

The most common issue with php is, that it may not be fully up to date in order for it to work with the newer versions of WordPress. Usually, you will get the error “WordPress requires at least php version…

In this next step, we will verify the php version is at least PHP 7.1, by creating a phpinfo.php page. However, it is always preferable to have the most up to date version of PHP, which at the time of this article is PHP 7.2, especially for security reasons.

To create a phpinfo page go to your /var/www/html and create a file called phpinfo.php.

touch phpinfo.php
chmod 644 phpinfo.php

 

and then, let’s add the following code:

<?php
// Show all information, defaults to INFO_ALL
phpinfo();
?>

Then, go to http://server_domain-name_or_IP/phpinfo.php

phpinfo page

And you should see something like this if your php is set up successfully.

 

If for some reason your php is out of date, you may have to update yum’s config file to do so.

 

First, double-check you have to correct yum packages with the following command

 

yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

To enable php 7.2, we need to enable the php 7.2 remi repo. At the time of the release of this article, we are using php 7.2. So, in going forward, when they release php 7.4 or php 7.5, you would edit the corresponding file.

 

These should be located in your /etc/yum.repos.d folder. Open the file with vim, and change the enabled field to a 1, and then save the file using vim’s :wq command

[remi-php72]
name=Remi's PHP 7.2 RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/php72/$basearch/
#mirrorlist=https://rpms.remirepo.net/enterprise/7/php72/httpsmirror
mirrorlist=http://cdn.remirepo.net/enterprise/7/php72/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

Update yum

yum update

Now, check for the php version of the php-fpm packages that should be on the server, under the field Version:

 

The output should look similar to this

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.myfahim.com
* epel: mirror.ehost.vn
* extras: centos.myfahim.com
* remi-php72: mirrors.thzhost.com
* remi-safe: mirrors.thzhost.com
* updates: centos.myfahim.com
remi-php72 | 2.9 kB 00:00:00
remi-php72/primary_db | 195 kB 00:00:08
Available Packages
Name : php
Arch : x86_64
Version : 7.2.8
Release : 1.el7.remi
Size : 3.2 M
Repo : remi-php72
Summary : ........

Next, install, enable and then start php fpm with the following commands:

yum install php-fpm php-gd php-pdo php-mbstring php-pear -y
systemctl enable php-fpm
systemctl start php-fpm

Don’t forget to restart apache, and/or php, if changes where made.

 

If using Apache:

service httpd restart

If using Nginx:

service nginx restart

 

Once completed, your phpinfo.php page should show you using php 7.2 or later. If you are using Nginx, you may have to verify that it knows to how to send php requests to php-fpm. If you have an issue, double-check your nginx.conf for errors or misconfiguration problems.

If your php is working and up to date, check your web server settings.

 

Troubleshooting and verifying your web server

You should also check that your Apache or Nginx and ports configured correctly. You should be able to go here

http://server_domain_name_or_IP

and see something similar to this if apache is set up successfully.

testing123

Alternatively, if your using Nginx, something similar to this will appear

 

Lastly, don’t forget to restart your webserver any time you make changes.

 

If you are not getting one of these success pages, double check your configuration files in your Apache or Nginx settings. Also, you can check to see if you are listening to the proper ports.

Resetting Your MYSQL Root Password

  1. The first step to resetting your root MySQL password on a Linux server is to stop MySQL. If you have a monitoring service for MySQL that will restart the service if it is down, make sure that service is also stopped for the time being, such as checkservd and cPanel.
    /etc/init.d/mysql stop
  2. Next, start MySQL in Single User Mode and enter without a password.

    Warning:

    Restarting MySQL this way will allow anyone access to every database. To avoid this, stop eth0 and fuser -k any logged in user and touch /etc/nologin.
    mysqld_safe --skip-grant-tables & mysql

    Note:

    Make sure to add & or the command prompt will not show.
  3. Enter the following commands in the MySQL prompt. The password below is only an example, replace 123456ABCDEF with the password of your choice. Our article Best Practice: Creating a Secure Password provides you with information on secure password best practices.
    UPDATE mysql.user SET password=password("123456ABCDEF") 
    WHERE user='root';
    FLUSH PRIVILEGES;
    exit;
  4. Stop MySQL safe and start MySQL and all other services that kept it from restarting like checkservd and cPanel normally.
    /etc/init.d/mysql stop
    /ect/init.d/mysql start
  5. Test your change by doing a test login, to log into MySQL, type it into the command line.
    mysql
  6. You will be prompted to enter your password. Enter the new password, if you are logged in then you have successfully reset the MySQL root password.

Note:

If the MySQL user has been deleted, run the following query to recreate it:

INSERT INTO `mysql`.`user` ( `Host` , `User` , `Password`
 , `Select_priv` ,`Insert_priv` , `Update_priv` ,`Delete_priv` 
 , `Create_priv` , `Drop_priv` , `Reload_priv` , `Shutdown_priv` 
 , `Process_priv` , `File_priv` , `Grant_priv` , `References_priv` 
 , `Index_priv` , `Alter_priv` , `Show_db_priv` , `Super_priv` 
 ,`Create_tmp_table_priv` , `Lock_tables_priv` , `Execute_priv` 
 , `Repl_slave_priv`, `Repl_client_priv` , `Create_view_priv` 
 , `Show_view_priv` , `Create_routine_priv` , `Alter_routine_priv` 
 , `Create_user_priv` , `max_questions` , `max_updates`
 , `max_connections` , `max_user_connections` ) VALUES ( 'localhost'
 , 'root',PASSWORD('password1234'), 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'
 , 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y','Y', 'Y', 'Y', 'Y'
 , 'Y', 'Y', 'Y', 'Y', 'Y', '0', '0', '0', '0' );

Troubleshooting

You may encounter an error when trying to change the root password that looks like the example below:

mysql> show warnings; +---------+------+-----------------------------------------------+ 
| Level | Code | Message | +---------+------+-----------------------------------------------+ 
| Warning | 1265 | Data truncated for column 'Password' at row 1 | 
| Warning | 1265 | Data truncated for column 'Password' at row 2 | 
+---------+------+-----------------------------------------------+

 

If MySQL is giving you warnings when changing the password, type the following:
mysql>prompt

 

Then leave MySQL and run:

/usr/bin/mysql_fix_privilege_tables

Once this is complete, try to set the password again. If you have further issues, please contact our Support team and we will be happy to assist you!

How To Install the LAMP Stack on CentOS 7

Pre-flight Checks

To find out which Linux distribution you are running, use this command:
cat /etc/redhat-release
It’s now time to verify that our yum environment is clean and up to date, we’ll do this by cleaning all of the yum cache, and update yum using:
yum clean all
yum update

Installing LAMP

Now that we know what environment we’re working in let’s get started on installing the LAMP stack on CentOS 7:

L – Linux

The first part of the stack is Linux. This is your operating system and since it is already installed there no need to worry about installing it or make any modifications. Installing CentOS 7 is easy to download and install using the image files that are provided from centos.org. CentOS has a helpful installation guide if you need to reference it for additional installation instructions.

A – Apache

Apache is the next piece of the LAMP stack. Apache is the webserver software that is responsible for serving the content to your web browser from the server. It takes the requests that it receives and sends back the HTML code for your browser to interpret.
Install Apache using Yum:
yum -y install httpd

Open ports in the FW:
firewall-cmd --permanent --add-service=http -add-service=https
firewall-cmd --reload

Start and enable apache to run when the server starts:
systemctl start httpd
systemctl enable httpd

Default Apache installation locations:

Some important server locations to remember for Apache are listed below. These are out-of-the-box defaults and can be changed as you see fit:
httpd binary: /sbin/httpd
Apache configuration file: /etc/httpd/conf/httpd.conf
Website files: /var/www/html/
Apache logs: /var/log/httpd/

M – MySQL/MariaDB

MySQL and MariaDB are what handle your website’s database. In most of today’s websites, data is not stored in flat or static files. Instead, the base of the site is coded in PHP which can pull information from your website’s database to deliver more dynamic content. MySQL and MariaDB are popular database servers that help house that information. MariaDB is becoming more widely used, so we’ll use for installation. Both are very similar in setting up and configuring.

Install MariaDB:
yum -y install mariadb-server
systemctl start mariadb

Although securing mysql is optional, it is strongly recommended:
mysql_secure_installation
**Run through the steps on screen to secure your Mysql/MariaDB environment

Enable MariaDB to start when the server starts:
systemctl enable mariadb

Default installation locations:

Some important server locations to remember for MySQL/MariaDB are listed below. These are out-of-the-box defaults and can be changed as you see fit:
MariaDB binary: /bin/mysql
MariaDB Configuration file: /etc/my.cnf
Database location: /var/lib/mysql
MariaDB logs: /var/log/mariadb/mariadb.log

P – PHP

Most websites that exist today are built using PHP coding. PHP provides the programmer with more options for dynamic content compared to flat html code. Several PHP versions are available for use depending on what PHP version the website was built in. We’ll install the latest version of PHP.

In order to install the latest PHP version, we first need to install CentOS’s Software Collection repository (SCL):
yum -y install centos-release-scl.noarch

We’ll now have access to install PHP 7.2 :
yum -y install rh-php72

Now we’ll fix the symbolic link for the binary:
ln -s /opt/rh/rh-php72/root/usr/bin/php /usr/bin/php

Install the updated PHP Module for Mysql/MariaDB:
yum -y install rh-php72-php-mysqlnd

Restart apache to work with the newly installed PHP:
systemctl restart httpd

How To Install PHP 7, 7.2 & 7.3 On CentOS 7

Prerequisites

How to Install PHP 7.2 with Apache on CentOS

Step 1: Choose PHP Version to Install

The newest stable release version of PHP is PHP 7.3.1. However, some software repositories default to older versions of the software.

One advantage of using an older release is its high stability and reliability. Newer releases may include more features, but are often more experimental and could cause system instability. If you cannot decide which version is right for you, version 7.2 is a great place to start.

Step 2: Enable Additional Software Repositories

By default, the yum package manager does not include access to the repositories that store the PHP packages. Therefore, you need to enable access to these software packages.

1. First, start by installing the yum-utils package by entering the following command in a terminal window:

sudo yum install yum-utils –y

2. Then, enable the epel-release repository by entering the following:

sudo yum install epel-release –y

3. Finally, add the following software repositories that contain the PHP packages:

sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Step 3: Enable Repository For PHP Version

Next, use the yum-config-manager to point your installer to the repository of the PHP version you want to install:

sudo yum-config-manager ––enable remi–php70

This command configures your system to download and install PHP 7.0.
To install PHP 7.1, change the entry as follows:

sudo yum-config-manager ––enable remi–php71

Likewise, replace the last two characters with 72 to install PHP 7.2.

 

Step 4: Install PHP and its Dependencies

At this point, you can install PHP. Enter the following command in the terminal:

sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql –y

As you see, the line includes many of the modules and add-ons that help PHP integrate with your local server configuration.

The output will also indicate which dependencies have been installed.

 

Step 5: PHP Modules

You may have noticed that the installation script included more than just the base PHP package. Many of these are modules that provide basic functionality to PHP. Installing this set helps ensure that your PHP installation meets your expectations for usage.

Like many other Linux applications, you can enhance your system’s PHP functionality using modules.

To search for available modules and generate a list of modules, type in the following command:

sudo yum search php | more

Step 6: Verify PHP Version

Finally, verify your installation was successful. Check which version of PHP you are running with the command:

php –v

Optional: Install PHP 7.3

You can enable Remi’s PHP 7.3 repository the same way you enabled other repositories in Step 3. However, this installation may fail if you have extensions that are not compatible with PHP 7.3.

If this is a first-time installation and you are confident that your system is compatible, you can enable PHP 7.3. Install the release with the following commands:

sudo yum-config-manager ––enable remi–php73
sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql –y