Mastering mysqldump: Essential Techniques for Backing Up and Restoring MySQL Databases

In this guide, I will show you how to back up and restore MySQL or MariaDB databases using the mysqldump command line from the terminal.

MySQL Backup Strategies

  • Physical Backups: Directly copying the database files. Fast recovery but requires downtime.
  • Logical Backups: Extracting data as SQL statements, allowing for portability but slower to restore.
  • Incremental Backups: Only saving changes since the last backup, reducing time and storage needs.
  • Automation: Using tools like cron for regular and consistent backups without manual intervention.
  • Verification: Regularly checking backups for integrity to ensure they are usable.
  • Recovery Testing: Regularly testing the recovery process to ensure minimal downtime during actual data loss.

Here’s an illustrated image representing MySQL backup strategies.

Backup a Single MySQL Database

To back up a single MySQL database, utilize the command line. Here’s how you can perform the backup using the root user and save it to a specific folder:

mysqldump -u root -p LINUXOPERATINGSYSTEM > /tmp/LINUXOPERATINGSYSTEM-$(date +%Y%m%d).sql

Replace DATABASE_NAME with your actual database name and /path/to/backup/ with the actual path where you want to save the backup.

Restore a Single MySQL Database

To restore a single MySQL database to a new host, follow these steps. Here’s an example:

Example my Database

  • New Database: LINUXOPERATINGSYSTEM
  • File dump: /tmp/LINUXOPERATINGSYSTEM-$(date +%Y%m%d).sql
mysql -u root -p -e "create database LINUXOPERATINGSYSTEM";
mysql -u root -p LINUXOPERATINGSYSTEM < /tmp/LINUXOPERATINGSYSTEM-$(date +%Y%m%d).sql

mysqldump syntax

mysqldump <OPTIONS> > dumpfile.sql

Where OPTIONS include backing up restoring, and dumpfile.sql is the name of the file to store the database backup.

Conclusion

Following these steps, you can effectively back up and restore MySQL databases using the mysqldump command line. This knowledge is crucial for database management and ensuring that your data is securely backed up. Thank you for following this tutorial from DevopsRoles page!

Change WordPress URLs in MySQL Database

Introduction

In this tutorial, How to Change WordPress URLs in MySQL Database using the command line. WordPress uses the MySQL database to store all its data, including site URLs.

Determining the Name of WordPress MySQL Database

WordPress stores the MySQL database name and its credentials in the wp-config.php file. Example below

[devopsroles@server1 ~]$ egrep -A 10 DB_NAME /Wordpress/web/wp-config.php 
define('DB_NAME', 'wordpressdb');

/** MySQL database username */
define('DB_USER', 'HuuPV');

/** MySQL database password */
define('DB_PASSWORD', 'devopsroles.com');

/** MySQL hostname */
define('DB_HOST', 'localhost');

Change WordPress URLs

I will change from the Old site: https://linuxoperatingsystem.net to the New site: http://192.168.122.229

Determining the site URL currently with the command below

mysql> SELECT * FROM wp_options WHERE option_name='siteurl';
+-----------+-------------+----------------------------------+----------+
| option_id | option_name | option_value                     | autoload |
+-----------+-------------+----------------------------------+----------+
|         1 | siteurl     | https://linuxoperatingsystem.net | yes      |
+-----------+-------------+----------------------------------+----------+
1 row in set (0.00 sec)

mysql> 

Command-line change WordPress URLs in MySQL Database

mysql -u root -p Database_Name
mysql> UPDATE wp_options SET option_value = replace(option_value, 'https://linuxoperatingsystem.net', 'http://192.168.122.229') WHERE option_name = 'home' OR option_name = 'siteurl';
mysql> UPDATE wp_posts SET guid = replace(guid, 'https://linuxoperatingsystem.net','http://192.168.122.229');
mysql> UPDATE wp_posts SET post_content = replace(post_content, 'https://linuxoperatingsystem.net', 'http://192.168.122.229');
mysql> UPDATE wp_postmeta SET meta_value = replace(meta_value,'https://linuxoperatingsystem.net','http://192.168.122.229');

The output is as below:

mysql> show databases;
+-------------------------+
| Database                |
+-------------------------+
| information_schema      |
| mysql                   |
| performance_schema      |
| sys                     |
| wordpressdb             |
+-------------------------+
6 rows in set (0.00 sec)

mysql> use wordpressdb;
Database changed
mysql> 


mysql> UPDATE wp_options SET option_value = replace(option_value, 'https://linuxoperatingsystem.net', 'http://192.168.122.229') WHERE option_name = 'home' OR option_name = 'siteurl';
Query OK, 2 rows affected (0.02 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> UPDATE wp_posts SET guid = replace(guid, 'https://linuxoperatingsystem.net','http://192.168.122.229');
Query OK, 43 rows affected (0.03 sec)
Rows matched: 43  Changed: 43  Warnings: 0

mysql> UPDATE wp_posts SET post_content = replace(post_content, 'https://linuxoperatingsystem.net', 'http://192.168.122.229');
Query OK, 29 rows affected (0.01 sec)
Rows matched: 43  Changed: 29  Warnings: 0

mysql> UPDATE wp_postmeta SET meta_value = replace(meta_value,'https://linuxoperatingsystem.net','http://192.168.122.229');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 73  Changed: 0  Warnings: 0

Conclusion

If you’re managing a WordPress site, you may find yourself needing to change the URLs directly in the MySQL database, especially after a migration or domain change. This process involves accessing your MySQL database through a command line interface and using SQL commands to update the site URL across different tables such as wp_options, wp_posts, and wp_postmeta. It’s a crucial skill for maintaining the integrity of your WordPress site links. Always back up your database before making such changes to avoid any potential data loss. For detailed guidance, consider visiting DevOpsRoles for tutorials.

How to reset admin password WordPress

Introduction

In this tutorial, I lost the admin password WordPress. How to reset admin password WordPress. Did you forget your WordPress admin password? Don’t worry; it happens to the best of us. Thankfully, resetting your admin password is a straightforward process.

How to reset admin password WordPress

You’ve logged into the MySQL database server as the root user and you’re trying to access the wordpressdb database. Here are the steps you’ve taken, For Example, I will use wordpressdb database.

[root@server1 ~]# mysql -u root -p
mysql> show databases;
mysql> use wordpressdb;

The output is below

[root@server1 ~]# mysql -u root -p

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wordpressdb        |
+--------------------+
5 rows in set (0.01 sec)

mysql> use wordpressdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 

Find the user table

mysql> show tables;

The output is as below:

mysql> show tables;
+-----------------------+
| Tables_in_wordpressdb |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_termmeta           |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+
12 rows in set (0.00 sec)

Find users

mysql> SELECT ID, user_login, user_pass FROM wp_users;

The output is below

mysql> SELECT ID, user_login, user_pass FROM wp_users;
+----+------------+------------------------------------+
| ID | user_login | user_pass                          |
+----+------------+------------------------------------+
|  1 | admin      | $P$B3jZNLbpdkZBm9NkkgtnfyaT7ECXWl0 |
+----+------------+------------------------------------+
1 row in set (0.00 sec)

The administrator is usually user number 1

mysql> SELECT * FROM wp_users where id = '1';

The output is as below:

mysql> SELECT * FROM wp_users where id = '1';
+----+------------+------------------------------------+---------------+--------------------+------------------------+---------------------+---------------------+-------------+--------------+
| ID | user_login | user_pass                          | user_nicename | user_email         | user_url               | user_registered     | user_activation_key | user_status | display_name |
+----+------------+------------------------------------+---------------+--------------------+------------------------+---------------------+---------------------+-------------+--------------+
|  1 | admin      | $P$B3jZNrepdkZBm9NkkgtnfyaT7ECXWl0 | admin         | pvhuu285@gmail.com | http://192.168.122.229 | 2020-09-04 04:11:59 |                     |           0 | admin        |
+----+------------+------------------------------------+---------------+--------------------+------------------------+---------------------+---------------------+-------------+--------------+
1 row in set (0.00 sec)

For MySQL 5 and higher

just execute a query like this to reset the password for the admin WordPress

mysql> UPDATE wp_users SET user_pass = MD5 ('123456789') WHERE ID = '1';

Check again

mysql> SELECT * FROM wp_users where id = '1';

The output is as below:

mysql> SELECT * FROM wp_users where id = '1';
+----+------------+------------------------------------+---------------+--------------------+------------------------+---------------------+---------------------+-------------+--------------+
| ID | user_login | user_pass                          | user_nicename | user_email         | user_url               | user_registered     | user_activation_key | user_status | display_name |
+----+------------+------------------------------------+---------------+--------------------+------------------------+---------------------+---------------------+-------------+--------------+
|  1 | admin      | $P$BaDO1CjqaJhGcYKwsk0va7dlAj5cXp. | admin         | pvhuu285@gmail.com | http://192.168.122.229 | 2020-09-04 04:11:59 |                     |           0 | admin        |
+----+------------+------------------------------------+---------------+--------------------+------------------------+---------------------+---------------------+-------------+--------------+
1 row in set (0.00 sec)

We log in through wp-login.php and input a new password, as usual.

http://192.168.122.229/wp-login.php

Conclusion

You have Reset admin password WordPress. I hope will this your helpful. Thank you for reading the DevopsRoles page!

How to install new python package using PIP

Introduction

In this tutorial, How to Setup a Python Virtual Environment. I will Install New Python package for the test environment.

In the preview post, I have installed and configured basic PIP here

Activate Your Virtual Environment

source venv/bin/activate

You can see “(venv)” indicator at the beginning of your command prompt.

(Test_Environment) [devopsroles@server1 ~]$

At any time, you can stop your virtual environment using the deactivate command.

(Test_Environment) [devopsroles@server1 ~]$ deactivate
[devopsroles@server1 ~]$ 

Install new python package using PIP

Install Packages

[devopsroles@server1 ~]$ pip3 freeze > package.txt
[devopsroles@server1 ~]$ python3 -m venv Test_Environment
[devopsroles@server1 ~]$ source Test_Environment/bin/activate
(Test_Environment) [devopsroles@server1 ~]$ pip install flask==1.0.0
(Test_Environment) [devopsroles@server1 ~]$ pip freeze > package_after.txt

After, install flask package in Test_Environmet as below

(Test_Environment) [devopsroles@server1 ~]$ ll
total 8
-rw-rw-r--. 1 devopsroles devopsroles  93 Sep  3 21:21 package_after.txt
-rw-rw-r--. 1 devopsroles devopsroles 297 Sep  3 21:20 package.txt
drwxrwxr-x. 5 devopsroles devopsroles 100 Sep  3 21:21 Test_Environment

You can difference confirmation as the picture below

Now, you can install everything package for another developer could easily recreate your environment and run code

git clone https://github.com/username/devopsroles.git
cd /home/huupv
source venv/bin/activate
pip install -r requirements.txt

Conclusion

You can install a new python package using PIP for a Virtual environment. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Install PIP on Centos 8

Introduction

In this tutorial. How to install pip on Centos 8. Pip is a package management system that allows you to install, remove, and otherwise manage software packages written in Python.

How to Install PIP on Centos 8

Two Python versions are Python 2 and Python 3.

Install pip3 for Python 3

sudo dnf install python3

The output is as below:

[devopsroles@server1 ~]$ sudo dnf install python3
[sudo] password for devopsroles: 
Last metadata expiration check: 0:07:32 ago on Wed 02 Sep 2020 09:15:00 PM +07.
Dependencies resolved.
==========================================================================================================
 Package                  Architecture Version                                      Repository       Size
==========================================================================================================
Installing:
 python36                 x86_64       3.6.8-2.module_el8.1.0+245+c39af44f          AppStream        19 k
Installing dependencies:
 python3-pip              noarch       9.0.3-16.el8                                 AppStream        19 k
 python3-setuptools       noarch       39.2.0-5.el8                                 BaseOS          162 k
Enabling module streams:
 python36                              3.6                                                               

Transaction Summary
==========================================================================================================
Install  3 Packages

Total download size: 201 k
Installed size: 466 k
Is this ok [y/N]: y
Downloading Packages:
(1/3): python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64.rpm            7.3 kB/s |  19 kB     00:02    
(2/3): python3-pip-9.0.3-16.el8.noarch.rpm                                7.1 kB/s |  19 kB     00:02    
(3/3): python3-setuptools-39.2.0-5.el8.noarch.rpm                          52 kB/s | 162 kB     00:03    
----------------------------------------------------------------------------------------------------------
Total                                                                      43 kB/s | 201 kB     00:04     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                  1/1 
  Installing       : python3-setuptools-39.2.0-5.el8.noarch                                           1/3 
  Installing       : python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64                              2/3 
  Running scriptlet: python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64                              2/3 
  Installing       : python3-pip-9.0.3-16.el8.noarch                                                  3/3 
  Running scriptlet: python3-pip-9.0.3-16.el8.noarch                                                  3/3 
  Verifying        : python3-pip-9.0.3-16.el8.noarch                                                  1/3 
  Verifying        : python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64                              2/3 
  Verifying        : python3-setuptools-39.2.0-5.el8.noarch                                           3/3 

Installed:
  python3-pip-9.0.3-16.el8.noarch                            python3-setuptools-39.2.0-5.el8.noarch       
  python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64       

Complete!

Check version pip

pip3 --version

The output as below

[devopsroles@server1 ~]$ pip3 --version
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)

To install and build Python modules with pip, you need to install the Development tools:

sudo yum install python3-devel
sudo yum groupinstall 'development tools'

Installing pip2 for Python 2

sudo dnf install python2
pip2 --version
sudo yum install python2-devel
sudo yum groupinstall 'development tools'

For example, I have installed two python3 and python2 on Centos 8 as below

[devopsroles@server1 ~]$ sudo yum list installed | grep pip
libpipeline.x86_64                          1.5.0-2.el8                           @anaconda 
platform-python-pip.noarch                  9.0.3-16.el8                          @anaconda 
python2-pip.noarch                          9.0.3-16.module_el8.2.0+381+9a5b3c3b  @AppStream
python2-pip-wheel.noarch                    9.0.3-16.module_el8.2.0+381+9a5b3c3b  @AppStream
python3-pip.noarch                          9.0.3-16.el8                          @AppStream
python3-pip-wheel.noarch                    9.0.3-16.el8                          @anaconda 

Managing Python Packages with pip

Typically, you should use pip inside a virtual environment only. It is used when separating the Python execution environment, such as when you want to change the Python execution environment for each project or you do not want to keep the local environment dirty venv.

In this section, we will go through several basic pip commands.

To install a package

pip install flask

If you want to install a specific version of the package

pip install flask==1.0.0

Uninstall a package

pip uninstall package_name

Search packages

pip search "package_name"

View the full list of packages

pip list

List outdated packages:

pip list --outdated

To upgrade an already installed package

pip3 install --upgrade package_name

Check pip dependencies

pip check

List of packages that need updating

pip list -o

Show packages by package name == version

pip freeze

Useful when outputting to requirements.txt

pip freeze > requirements.txt

Package update

pip install  -U package_name

Update pip itself

pip install  -U pip

Checking the details of the package

pip show package-name

Conclusion

How to install pip on CentOS 8 and how to easily install and uninstall Python modules with pip. Thank you for reading the DevopsRoles page!

Initial Server Setup Centos 8

Introduction

When launching a new server with CentOS 8, ensuring a secure and properly configured foundation is essential. Whether you’re setting up a VPS for the first time or deploying infrastructure for production, a systematic initial server setup will help harden your system and prepare it for future installations. In this guide, we’ll walk through the essential steps to Initial Server Setup CentOS 8 server – from creating a non-root sudo user to configuring your firewall and installing core utilities.

Update Your System

Before any configuration, ensure that your system is up to date. This not only secures your server but also ensures compatibility with future installations:

sudo dnf update -y
sudo dnf upgrade -y

Create a Sudo User and Granting Administrative Privileges

useradd devopsroles
passwd devopsroles
usermod -aG wheel devopsroles
su - devopsroles

The output terminal as below

Open a new terminal window

ssh devopsroles@SERVER_IP_ADDRESS

Disable SSH Root Login

We have new account. so, I can secure our server by disabling remote SSH account to the root account.

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config_BK
sudo sed -i -e 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
sudo cat /etc/ssh/sshd_config | grep PermitRootLogin

The output terminal as below

we need to restart the SSH

sudo systemctl reload sshd

Changing the hostname

sudo hostnamectl set-hostname server1.devopsroles.com

Setting Up a Basic Firewall CentOS 8

Firewalld is a firewall management tool for Linux operating systems. you need to make sure that the server firewall allows web access.

sudo firewall-cmd --add-service=http --permanent
sudo systemctl enable firewalld
sudo systemctl start firewalld
sudo systemctl status firewalld

Update CentOS 8 System

sudo yum check-update
sudo yum upgrade -y
sudo yum clean all

Install System Utilities

sudo yum -y install bind-utils net-tools  psmisc traceroute tree vim-enhanced wget

Conclusion

By following this initial server setup guide for CentOS 8, you’ve established a secure and functional baseline for your system. Creating a non-root user with sudo privileges, securing SSH access, enabling the firewall, and installing essential tools are critical steps to ensure stability, security, and readiness for production workloads. With this solid foundation, you’re now well-prepared to deploy applications, configure services, or automate infrastructure tasks confidently.

For ongoing security, remember to regularly update your system and monitor user access and services. If you’re planning to install a web server, database, or other services next, this setup ensures you start from a well-configured environment. I hope will this your helpful. Thank you for reading the DevopsRoles page!

RabbitMQ tutorial for beginners

Introduction

In this tutorial, I will witten about RabbitMQ: Install, configuration, and example. RabbitMQ is a message broker, written in Erlang. Now, let’s go to the RabbitMQ tutorial for beginners.

It is transferring data ( messages) between several services: one service adds a message to the queue, the other receives these messages.

How to install and Run use RabbitMQ

Debian/Ubuntu

# apt install rabbitmq-server

Centos

# yum -y install epel-release
# yum -y install erlang socat
# wget https://www.rabbitmq.com/releases/rabbitmq-server/v3.6.10/rabbitmq-server-3.6.10-1.el7.noarch.rpm
# rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc
# rpm -Uvh rabbitmq-server-3.6.10-1.el7.noarch.rpm

Docker

# docker run docker pull rabbitmq

RabbitMQ tutorial for beginners

Rabbitmq-plugins

The Documentation here

To view active plugin use list

$ sudo rabbitmq-plugins list

Show activate the plugin RabbitMQ Management

$ sudo rabbitmq-plugins enable rabbitmq_management

Active plugins are stored in a file /etc/rabbitmq/enabled_plugins

$ cat /etc/rabbitmq/enabled_plugins

HTTP API

The documention here

rabbitmq_management active API support on port 15672

# netstat -nplt | grep 15672

Example check it

# curl -i -u guest:guest localhost:15672/api/overview

rabbitmqadmin

Document here

rabbitmqctl

The Documentation here. It is mainly for managing nodes in a cluster – adding, deleting, rebooting, managing logs.

# rabbitmqctl status

View users

# rabbitmqctl list_users

WEB UI

After activating the plugin, the web interface for managing the server on port 15672.

Add a new user And the rights to everything. User access is guest denied by default.

# rabbitmqctl add_user test test
# rabbitmqctl set_user_tags test administrator
# rabbitmqctl set_permissions -p / test ".*" ".*" ".*"

There are three basic concepts for RabbitMQ

  • producer: the client doing the sending of the message
  • queue: the actual message queue
  • consumer: the client receiving messages from the queue
  • exchange: receives messages from the producer, and sends them to queues according to its type

Example

To work with RabbitMQ, AMQP (Advanced Message Queuing Protocol) support is required. we will install for python pika

$ sudo yum install python-pika

Sending a message

To create a script for the producer.

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',
                     routing_key='hello',
                     body='Hello World!')

print(" [x] Sent 'Hello World!'")

connection.close()

Run the script

$ ./producerScript.py

check with rabbitmqadmin

# rabbitmqadmin get queue='hello'

List of queues with rabbitmqctl

# rabbitmqctl list_queues

Receiving a message

The second script – consumertest.py- will receive a message from the queue

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

Run it

# ./consumer.py

We check the queue – now it’s empty here

# rabbitmqctl list_queues

Conclusion

Through the article, RabbitMQ tutorial for beginners. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Centos 8 LEMP WordPress

In this tutorial, How to Install and configure LEMP WordPress running a Centos 8. LEMP is an acronym for Linux, Nginx, MySQL, and PHP. Now, let’s go to Centos 8 LEMP WordPress.

Centos 8 LEMP WordPress Environment

  • Centos 8
  • Nginx
  • MySQL
  • WordPress

1.Nginx Install

The first, Your update for Centos 8.

[root@DevopsRoles vagrant]# dnf update

Next, Install Nginx on Centos 8

[root@DevopsRoles vagrant]# dnf install nginx
# Set to start automatically at reboot 
[root@DevopsRoles vagrant]# systemctl enable  --now nginx
# Check status
[root@DevopsRoles vagrant]# systemctl status nginx

Firewall port setting HTTP (80) Allow HTTPS(443)

[root@DevopsRoles vagrant]# firewall-cmd --add-port=80/tcp --permanent
[root@DevopsRoles vagrant]# firewall-cmd --reload

2.Install MySQL

[root@DevopsRoles vagrant]# dnf install @mysql
 # After installation, familiar service start, and automatic start setting
[root@DevopsRoles vagrant]# systemctl start mysqld
[root@DevopsRoles vagrant]# systemctl enable mysqld
 # The initial security settings of mysql
[root@DevopsRoles vagrant]# mysql_secure_installation
# Operation check
[root@DevopsRoles vagrant]# mysql -u root -p

3.Install PHP

[root@DevopsRoles vagrant]# dnf install php-fpm php-cli php-json php-opcache php-xml php-gd php-curl
# php is also set to start automatically
[root@DevopsRoles vagrant]# systemctl enable --now php-fpm

Extension php required to run WordPress on Centos 8

[root@DevopsRoles vagrant]# dnf install php-cli php-json php-opcache php-xml php-gd php-curl php-mysqlnd

4.Database Creation

[root@DevopsRoles vagrant]# mysql -u root -p
# db creation (name can be anything)
SQL> create database wordpressdb;
# Create user, set permissions and reflect settings
SQL> create user wpadmin@localhost identified by 'Stro123@.';
SQL> grant all on wordpressdb.* to wpadmin@localhost;
SQL> flush privileges;
SQL> quit

5.Install WordPress

# Download wordpress
[root@DevopsRoles vagrant]# wget https://wordpress.org/latest.tar.gz
# Create a folder for #wordpress
[root@DevopsRoles vagrant]# mkdir /usr/share/nginx/wp.devopsroles.com
# Decompress
[root@DevopsRoles vagrant]# tar xzf latest.tar.gz -C /usr/share/nginx/wp.devopsroles.com/ --strip-components=1
# Copy configuration file
[root@DevopsRoles vagrant]# cp /usr/share/nginx/wp.devopsroles.com/wp-config-sample.php /usr/share/nginx/wp.devopsroles.com/wp-config.php
# Open configuration file
[root@DevopsRoles vagrant]# vi /usr/share/nginx/wp.devopsroles.com/wp-config.php

Look like wp-config.php file as below:

/** MySQL database name */
define( 'DB_NAME', 'wordpressdb' );
/** MySQL database user name */
define( 'DB_USER', 'wpadmin' );

/** MySQL database password */
define( 'DB_PASSWORD', 'Stro123@.' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

/** Database character set when creating database table */
define( 'DB_CHARSET', 'utf8' );

/** Database collation (should not be changed in most cases) */
define( 'DB_COLLATE', '' );

Get the authentication unique key for WordPress.

curl -s https://api.wordpress.org/secret-key/1.1/salt/

Copy and paste the returned contents into wp-config.php

define('AUTH_KEY',         'balabal');
define('SECURE_AUTH_KEY',  'balabal');
define('LOGGED_IN_KEY',    'balabal');
define('NONCE_KEY',        'balabal');
define('AUTH_SALT',        'balabal');
define('SECURE_AUTH_SALT', 'balabal');
define('LOGGED_IN_SALT',   'balabal');
define('NONCE_SALT',       'balabal');

6.Nginx Settings

I have configured Nginx for WordPress with content as below:

vi /etc/nginx/conf.d/devopsroles.conf

server {
    listen       80 default_server;
    server_name  devopsroles.com;
    root         /usr/share/nginx/wp.devopsroles.com;

    access_log /var/log/nginx/access_devopsroles.com.log;
    error_log /var/log/nginx/error_devopsroles.com.log;

    index   index.php;

    location / {
        try_files    $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_index index.php;
    }
    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

Set user and group nginx for the folder as below

chown -R nginx:nginx /usr/share/nginx/wp.devopsroles.com/

Restart Nginx

systemctl restart nginx

I have installed success Centos 8 LEMP WordPress. Have a good nice 🙂 Thank you for reading the DevopsRoles page!

SonarQube from a Jenkins Pipeline job in Docker

Introduction

In today’s fast-paced DevOps environment, maintaining code quality is paramount. Integrating SonarQube with Jenkins in a Docker environment offers a robust solution for continuous code inspection and improvement.

This guide will walk you through the steps to set up SonarQube from a Jenkins pipeline job in Docker, ensuring your projects adhere to high standards of code quality and security.

Integrating SonarQube from a Jenkins Pipeline job in Docker: A Step-by-Step Guide.

Docker Compose for SonarQube

Create directories to keep SonarQube’s data

# mkdir -p /data/sonarqube/{conf,logs,temp,data,extensions,bundled_plugins,postgresql,postgresql_data}

Create a new user and change those directories owner

# adduser sonarqube
# usermod -aG docker sonarqube
# chown -R sonarqube:sonarqube /data/sonarqube/

Find UID of sonarqube user

# id sonarqube

Create a Docker Compose file using the UID in the user.

version: "3"

networks:
  sonarnet:
    driver: bridge

services:
  sonarqube:
    // use UID here
    user: 1005:1005
    image: sonarqube
    ports:
      - "9000:9000"
    networks:
      - sonarnet
    environment:
      - sonar.jdbc.url=jdbc:postgresql://db:5432/sonar
    volumes:
      - /data/sonarqube/conf:/opt/sonarqube/conf
      - /data/sonarqube/logs:/opt/sonarqube/logs
      - /data/sonarqube/temp:/opt/sonarqube/temp
      - /data/sonarqube/data:/opt/sonarqube/data
      - /data/sonarqube/extensions:/opt/sonarqube/extensions
      - /data/sonarqube/bundled_plugins:/opt/sonarqube/lib/bundled-plugins

  db:
    image: postgres
    networks:
      - sonarnet
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
    volumes:
      - /data/sonarqube/postgresql:/var/lib/postgresql
      - /data/sonarqube/postgresql_data:/var/lib/postgresql/data

Use docker-compose start

# docker-compose -f sonarqube-compose.yml up

Install and configure Nginx

Nginx Install

# yum install nginx

Start Nginx service

# service nginx start

Configure nginx

I have created a “/etc/nginx/conf.d/sonar.devopsroles.com.conf” file, look like as below:

upstream sonar {
    server 127.0.0.1:9000;
}


server {

    listen 80;
    server_name  dev.sonar.devopsroles.com;


    root /var/www/html;
        allow all;
    }

    location / {
        return 301 https://dev.sonar.devopsroles.com;
    }
}

server {

    listen       443 ssl;
    server_name  dev.sonar.devopsroles.com;

    access_log  /var/log/nginx/dev.sonar.devopsroles.com-access.log proxy;
    error_log /var/log/nginx/dev.sonar.devopsroles.com-error.log warn;

    location / {
        proxy_http_version 1.1;
        proxy_request_buffering off;
        proxy_buffering off;

        proxy_redirect          off;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto   $scheme;
        proxy_pass http://sonar$request_uri;
    }
}

Check syntax and reload NGINX’s configs

# nginx -t && systemctl start nginx

Jenkins Docker Compose

Here is an example of a Jenkins Docker Compose setup that could be used for integrating SonarQube from a Jenkins pipeline job:

version: '3'

services:
  jenkins:
    image: jenkins/jenkins:lts
    container_name: jenkins
    ports:
      - "8080:8080"
      - "50000:50000"
    volumes:
      - jenkins_home:/var/jenkins_home
    networks:
      - jenkins-sonarqube

  sonarqube:
    image: sonarqube:latest
    container_name: sonarqube
    ports:
      - "9000:9000"
    environment:
      - SONAR_JDBC_URL=jdbc:postgresql://db:5432/sonarqube
      - SONAR_JDBC_USERNAME=sonar
      - SONAR_JDBC_PASSWORD=sonar
    networks:
      - jenkins-sonarqube

  db:
    image: postgres:latest
    container_name: postgres
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
      - POSTGRES_DB=sonarqube
    networks:
      - jenkins-sonarqube

networks:
  jenkins-sonarqube:

volumes:
  jenkins_home:

Explanation:

  • Jenkins Service: Runs Jenkins on the default LTS image. It exposes ports 8080 (Jenkins web UI) and 50000 (Jenkins slave agents).
  • SonarQube Service: Runs SonarQube on the latest image. It connects to a PostgreSQL database for data storage.
  • PostgreSQL Service: Provides the database backend for SonarQube.
  • Networks and Volumes: Shared network (jenkins-sonarqube) and a named volume (jenkins_home) for Jenkins data persistence.

Conclusion

By following this comprehensive guide, you have successfully integrated SonarQube with Jenkins using Docker, enhancing your continuous integration pipeline. This setup not only helps in maintaining code quality but also ensures your development process is more efficient and reliable. Thank you for visiting DevOpsRoles, and we hope this tutorial has been helpful in improving your DevOps practices.

Using Netdata to Monitor PHP-FPM

In this tutorial, How to Monitor the PHP-fpm using Netdata on Centos 7. Netdata is a free open source. It is very easy to install and configure for real-time monitoring. Now, let’s go to Netdata to Monitor PHP-FPM.

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI. It is commonly used in LEMP ( Linux Nginx MySQL/MariaDB PHP) stack; Nginx uses PHP FastCGI for serving dynamic HTTP content.

Steps install and configure

  • Centos 7 Server or RHEL 7 Server
  • Install PHP and enable PHP-FPM status
  • Install Netdata and configure Monitor for PHP-FPM status.

1. How to Install PHP

For example, How to install PHP version 7.3 on Centos 7.

2. Enable the PHP-FPM status page.

Check php-fpm running on your system.

[root@DevopsRoles vagrant]# netstat -nplt | grep php-fpm
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      4328/php-fpm: maste 
[root@DevopsRoles vagrant]# ps -ef | grep php
root      4328     1  0 21:50 ?        00:00:00 php-fpm: master process (/etc/opt/remi/php73/php-fpm.conf)
apache    4329  4328  0 21:50 ?        00:00:00 php-fpm: pool www
apache    4330  4328  0 21:50 ?        00:00:00 php-fpm: pool www
apache    4331  4328  0 21:50 ?        00:00:00 php-fpm: pool www
apache    4332  4328  0 21:50 ?        00:00:00 php-fpm: pool www
apache    4333  4328  0 21:50 ?        00:00:00 php-fpm: pool www
root      4381  3003  0 21:52 pts/0    00:00:00 grep --color=auto php

Configure file php-fpm status as shown.

$ sudo vim /etc/php-fpm.d/www.conf 
OR
$ sudo vim /etc/opt/remi/php73/php-fpm.d/www.conf	#for PHP versions 7.0, 7.1,7.3 vvv

You find and uncomment the variable pm.status_path = /status as shown in the screenshot.

[root@DevopsRoles vagrant]# cat /etc/opt/remi/php73/php-fpm.d/www.conf | grep pm.status_path
pm.status_path = /status

Save the changes and exit the file.

Check the PHP-FPM configuration file for any errors

$ sudo php-fpm -t
OR
$ sudo systemctl restart php73-php-fpm

Create a new Nginx config for PHP-FPM as shown

[root@DevopsRoles ~]# vi /etc/nginx/conf.d/php-fpm.conf

# The content as below:

    server {

        listen       80;
        #listen       [::]:80 default_server;
        server_name  127.0.0.1;

    location  /status {
        access_log off;
        allow 127.0.0.1;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
        deny all;
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
   }
   }

Reload PHP-FPM and Nginx config for changes to take effect.

[root@DevopsRoles ~]# systemctl reload php73-php-fpm
[root@DevopsRoles ~]# systemctl reload nginx

Now, Use the curl command to check php-fpm status.

root@DevopsRoles ~]# curl http://127.0.0.1/status
pool:                 www
process manager:      dynamic
start time:           18/Jul/2020:10:16:17 +0700
start since:          878
accepted conn:        14
listen queue:         0
max listen queue:     0
listen queue len:     128
idle processes:       4
active processes:     1
total processes:      5
max active processes: 1
max children reached: 0
slow requests:        0

Memo: meaning of different values of PHP-FPM Status

-pool – the name of the pool.
-process manager – possible values static, dynamic or ondemand. We never use static.  Trying ondemand is on todo list.
-start time – the date and time FPM has started or reloaded.
-start since – number of seconds since FPM has started
-accepted conn – the number of request accepted by the pool
-listen queue – the number of request in the queue of pending connections. If this number is non-zero, then you better increase number of process FPM can spawn.
-max listen queue – the maximum number of requests in the queue of pending connections since FPM has started
-listen queue len – the size of the socket queue of pending -connections
-idle processes – the number of idle processes
-active processes – the number of active processes
-total processes – the number of idle + active processes
-max active processes – the maximum number of active processes since FPM has started
-max children reached – number of times, the process limit has been reached, when pm tries to start more children. If that value is not zero, then you may need to increase max process limit for your PHP-FPM pool. Like this, you can find other useful information to tweak your pool better way.
-slow requests – Enable php-fpm slow-log before you consider this. If this value is non-zero you may have slow php processes. Poorly written mysql queries are generally culprit.

3. Install Netdata on Centos 7 here

4. Configure Netdata to Monitor PHP-FPM

The Netdata configure for Nginx in folder /etc/netdata/python.d which is written in YAML format.

You can open it or create a new file php-fpm.conf as shown

[root@DevopsRoles python.d]# cat /etc/netdata/python.d/php-fpm.conf 
# The output as below:
localhost:
  name : 'local'
  url  : 'http://localhost/status'

localipv4:
  name : 'local'
  url  : 'http://127.0.0.1/status'

Restart Netdata Server

[root@DevopsRoles ~]# systemctl restart netdata 

5. Using Netdata to Monitor PHP-FPM

Open a web browser access the netdata web UI.

http://NETDATA_SERVER_IP:19999
or
http://DOMAIN_NAME:19999

The result as the picture below

Conclusion

Through the article, you can use Netdata to monitor PHP-FPM. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Devops Tutorial

Exit mobile version