Thursday, June 13, 2019

How to Backup Website using Command Line and Download to your Computer

To backup website web source and database using Command Line



1. MySQL Back up website database

·       
  • To verify what database name is used in the website, check website’s wp-config.php
  • Once website database name is verified in wp-config.php;  Verify it in mysql ; Once verified.
  • Run command to back up database, don’t worry it won’t affect current database, this will only create a copy.


            # mysqldump –u root –p database_name > backUpDatabaseNameDate.sql

            e.g:  # mysqldump –u root –p database_name > mysqldump_db_backup_WebsiteDatabase_name20190314.sql

2. Back up website web source

  •          Locate folder that contains WordPress files.
  •          Verify if you’re in the folder where you can see the folder that holds WordPress files.


            # tar -zcvf newWebNameBackUpDate.tar.gz  folderName
            e.g:  # tar -zcvf web_castleoaklp_20140418.tar.gz  web_castleoaklp
           
  • Verify that backup tar websource and backup mysqdump is stored at website folder websource (e.g public_html)
  • Create "wgetdown" folder to store your downloads using wget command. Inside wgetdown folder, run command.


            wgetdown] # wget http://test.example.com/fileBackUp2019611.tar.gz


To backup website web source and database


1. Back up website database

·       
  • To verify what database name is used in the website, check website’s wp-config.php
  • Once website database name is verified in wp-config.php;  Verify it in mysql ; Once verified.
  • Run command to back up database, don’t worry it won’t affect current database, this will only create a copy.

            # mysqldump –u root –p database_name > backUpDatabaseNameDate.sql
            e.g:  # mysqldump –u root –p database_name > mysqldump_db_backup_WebsiteDatabase_name20190314.sql

2. Back up website web source

  •          Locate folder that contains WordPress files.
  •          Verify if you’re in the folder where you can see the folder that holds WordPress files.

            # tar -zcvf newWebNameBackUpDate.tar.gz  folderName
            e.g:  # tar -zcvf web_castleoaklp_20140418.tar.gz  web_castleoaklp
           
  • Verify that backup tar websource and backup mysqdump is stored at website folder websource (e.g public_html)
  • Create "wgetdown" folder to store your downloads using wget command. Inside wgetdown folder, run command.

            wgetdown] # wget http://test.example.com/fileBackUp2019611.tar.gz

Restore wordpress back up to localhost via virtualhost


·      Always check if you are at source folder /var/www/FolderName/
·      FolderName] #
·      If you’re not inside your FolderName specify the correct path written in terminal /var/www/…/…
User: root
Password: admin1234

Mysql Password: 123456

1. Add new folder to var/www directory (add your desired website name; sample ‘folderName’ : path /var/www/folderName)
            Add 2 folders: log and public_html
            # mkdir log                                                     create ‘log’ folder
        Or # mv web_castleoaklp public_html                        rename extracted ‘web_castleoaklp’ folder to public_html
            # unzip folder.zip                                           extract zip file
            # tar -zxvf web_castleoaklp.tar.gz               extract tar file
            # tar -zcvf newfile.tar.gz folderName          compress folder in tar
            # tar -tvf file.tar.gz                                        show list inside tar file

2. Rename database sql file (with your desired database name; sample 'db_name.sql') 

            FolderName] # mv OldName.sql NewName.sql

3. On sql file -> Replace website url with test.example.com     
·      Ctrl H, Replace all www.castleoaklp.com to test.example.com
·      cat (search)
FolderName] # cat filename.sql | grep “TextToSearch”         
·      sed -i (replace)
FolderName] # sed –i ‘s/OldText/NewTextToReplace/g’ filename.sql

4. Create new database at phpMyAdmin (with your desired database name; sample 'db_name.sql')
# mysql –u root –p –e “CREATE database db_name”
FolderName] # mysql –u root –p - -database=db_name < db_sourceFileName.sql

·      In phpMyAdmin, select your database (sample db_name)
# mysql –u root –p
# show databases;
# use db_name;
db_name] # show tables;
# exit;

5. Update wp_config.php at www/folderName/public_html:
            # vi /var/www/FolderName/public_html/wp-config.php                        if you’re not in sourceFolder
       Or  # vi wp-config.php                                                                           if you’re in sourceFolder
            Press letter “i" on the keyboard to allow input to vi editor
            After changes to file is done;
            Press ‘esc’ key
            Type in
                        :wq!                 To save changes to file and exit
                        :q!                    To exit without saving changes to file
·   db_name ->db_name        (this can be changed, it depends on your desired name made at step 2)
·   db-user -> root
·   db_password -> '123456'  
·   db_host -> localhost

6. Add  vhost info
            # sudo vi /etc/httpd/conf.d/httpd-vhost.conf
                       
            <VirtualHost *:80>
                        DocumentRoot "/var/www/folderName/public_html"      (the location of your WordPress files)
                        ServerName test.example.com                                (your desired domain)
                        Errorlog /var/www/folderName/log/error.log
                        CustomLog /var/www/folderName/log/request.log combined
            </VirtualHost>
             
7. Add info below at hosts file
            # sudo vi /etc/hosts

            127.0.0.1         test.example.com       (your desired domain)

8. Restart apache services                                
            # sudo systemctl restart httpd.service

9. Visit your site (sample test.example.com)


10.  If Apache does not restart because of an error; to view the error run this command:
            # sudo systemctl status httpd.service –l

Try to visit your site first if it’s working without doing step 11, coz I made changes on htpd.conf file.










11. If WordPress permalinks not working 
·         Change the file’s permission so that it is writeable by Apache.
               # chown apache /var/www/foldername/public_html/.htaccess

·         Edit your httpd.conf file in /etc/httpd/conf/, add the following info below:
                        # sudo vi /etc/httpd/conf/httpd.conf

                        <Directory “/var/www/foldername”>
                                    AllowOverride All
                        </Directory>

·         Restart your Apache.
                        # sudo systemctl restart httpd.service








Monday, June 10, 2019

SOLVED MySQL Error 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

Have you encountered this error? Like I did? 

Error 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

If yes, then lucky us we will definitely learn for our mistakes.

So, here's what caused me to achieve this error.

While I was in the middle of installing LAMP stack to CentOS 7 computer, after successfully installed MariaDB, with the steps below:

 - - - - - - -  - - - - - - -  - - - - - - -

Install MariaDB Database in CentOS

MariaDB stores information of our websites. Run the command:

[root@localhost ~] #  sudo yum install mariadb-server mariadb

Confirm the installation, by pressing Y and <Enter>.

Once MariaDB is installed successfully, to start the service, run command:

[root@localhost ~] #  sudo systemctl start mariadb


Securing MariaDB in CentOS

MariaDB is not secured by default, the reason why we should remove anonymous user, test database and configure other settings. Run command:

[root@localhost ~] # sudo mysql_secure_installation

You will be prompted to enter your MariaDB root password, just press Enter because you haven’t set any password yet. Please note that this should not be confused by the root password of your server.

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

Please pause here! Attention! Read the RED text again carefully to avoid errors. Also you cannot proceed successfully in the next step below if you mess up this instruction.

Like what happened to me, you will be on the loop, where it will ALWAYS asks you the password. In my case, the CentOS computer I was working on has NO Desktop installed yet, I was really stuck for hours not knowing how to exit the loop. 

So what happened next, which I made a huge mistake? it's the question here:


In order to log into MariaDB to secure it, we'll need the current password for the roor user. If you've just installed MariaDB, and you haben't set the root passwoed yet, the password will be blank, so you should just press ENTER here.

Enter current password for root (ENTER for none): <Enter key>

Well, what I did was I typed in my user root password and it was 'admin1234', which instead I should have just pressed the <Enter> key. Which triggered this loop:



Enter current password for root (ENTER for none): 
Error 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
Enter current password for root (ENTER for none): 
Error 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

The infinite loop, which probably will take like forever? If I had a desktop environment installed in this CentOS computer then it would have been easier to just close the konsole terminal, but since desktop environment is not installed yet. My last resort was to force reboot the computer, remove maria db and install it again.


SOLVED: Completely Remove MariaDB or MySQL from CentOS 7 and Installed Again Successfully

These steps works for CentOS 6.5, CentOS 7 and MariaDB 10

[root@localhost ~] # yum remove mariadb mariadb-server
[root@localhost ~] # rm -rf /var/lib/mysql       If your datadir in /etc/my.cnf points to a different directory, remove that directory instead of /var/lib/mysql
[root@localhost ~] # rm /etc/my.cnf
Optional step: rm ~/.my.cnf
[root@localhost ~] # yum install mariadb mariadb-server



For MariaDB 10.1 on CentOS 7 steps are:

[root@localhost ~] # yum remove MariaDB-server MariaDB-client
[root@localhost ~] # rm -rf /var/lib/mysql       If your datadir in /etc/my.cnf points to a different directory, remove that directory instead of /var/lib/mysql
[root@localhost ~] # rm /etc/my.cnf
Optional step: rm ~/.my.cnf
[root@localhost ~] # yum install MariaDB-server MariaDB-client


Hope you could also share how you encountered this error and how you solved it!

How to Install PhpMyAdmin and Epel on CentOS 7

If you haven't installed LAMP on your CentOS 7, you're most required to install LAMP first. Steps in installing LAMPS are available here.

Before you can run your WordPress website in your localhost or virtual host. You significantly need to have PhpMyAdmin installed in your CentOS 7 computer.

Before proceeding, check if your computer is properly connected to the internet.


1. Install Apache Web Server

The first prerequisite of phpMyAdmin is an Apache web server. Run command:

[root@localhost ~] # yum install httpd -y

Next, check the status of apache with the command:

[root@localhost ~] # systemctl status httpd

Status should display a message in which a green text saying Active(running).

To verify the Apache is running, open your browser type in the address bar: your server’s IP address or localhost. You should see the Apache testing splash screen:




2.Install the EPEL Repository


phpMyAdmin comes in the EPEL repository (Extra Packages for Enterprise Linux). To access EPEL you need to install a special package – epel-release. To install epel-release on your CentOS, run command:

[root@localhost ~] # sudo yum install epel-release

Now that the EPEL repo is made available, we can proceed to install phpMyAdmin on CentOS 7.


3. Install phpMyAdmin on CentOS 7

Run the following command:

[root@localhost ~] # sudo yum install phpmyadmin

 You will have to edit the phpMyAdmin configuration file, once the phpMyAdmin installation is completed.

4. Configure the phpMyAdmin.conf File

The edit will make sure that you can access your phpMyAdmin client remotely. We’ll open the phpMyAdmin.conf file located in the directory /etc/httpd/conf.d/ using the vi editor:

[root@localhost ~] # vi /etc/httpd/conf.d/phpMyAdmin.conf

Here you will see four different require IP strings matched with long IPs. The default value is 127.0.0.1. Replace that value with the IP of the machine you will be using to access phpMyAdmin. You can use a tool like WhatsMyIP to check your IP. Save and exit the file by hitting escape and typing in :wq!.


5. Restart the Apache Web Server

To restart the Apache web server, run command:

[root@localhost ~] # systemctl restart httpd

6. Check if phpMyAdmin is Working

Finally, we can open phpMyAdmin utility by entering in your server’s IP address in a browser’s address bar with the addition /phpmyadmin string like so – 127.0.0.1/phpmyadmin or localhost/phpmyadmin. There you should see the login screen.

7. Locate Your phpMyAdmin Username and Password

Your username and password will be located in the file config.inc.php. The file will be in the phpMyAdmin installation directory. For us it is /etc/phpMyAdmin.

Open the file using the vi editor:

[root@localhost ~] # vi config.inc.php

Inside you can set the username and password. Press letter "i" in your keyboard to enable input/insert feature.

The phpMyAdmin user configuration file opened in the vi editor.

Once changes are done. Save and exit the file by hitting esc and :wq! .


Success!

You have completed the phpMyAdmin install on CentOS 7. You may now log in and import your database information to run your WordPress website.

Hope it helps!



How to Install LAMP and PhpMyAdmin in CentOS7



LAMP stack is an acronym of open-source software that are essential to create websites and web applications. 

Linux is an open source Operating System.
Apache is the HTTP server. 
MySQL/MariaDB is a relational database management system.
PHP is a server-side programming language. 

These software's are highly significant to run your WordPress websites. Always check that your computer is properly connected to the internet before you proceed below.


Install  LAMP stack on your CentOS 7 computer. 

1. Update your system

To verify your CentOS 7 server is up to date, before running the applications, run command:

[root@localhost ~] # sudo yum update

2. Install Apache

We'll use Yum package manager to install software applications on CentOS 7. To install Apache HTTP server, run the command below:

[root@localhost ~] # sudo yum install httpd

When prompted to confirm the installation, press Y and <Enter> to proceed.

Next, we need to start Apache service using command below:

[root@localhost ~] # sudo systemctl start httpd.service


This page will display after Apache was installed successfully on your CentOS 7 server. Now try to open your  browser, type in the address bar: localhost or IP address 192.168.xxx.xxx. You should get the default Apache page as shown above which means it's working properly.

Next, you need to activate Apache to start during system boot:

[root@localhost ~] #  sudo systemctl enable httpd.service

3. Install MariaDB Database

MariaDB is a forked version of MySQL. Thus most commands used in MySQL works in MariaDB. MariaDB stores information of our websites. Type in the command:

[root@localhost ~] #  sudo yum install mariadb-server mariadb

Confirm the installation, by pressing Y and <Enter>.

Once MariaDB is installed successfully, to start the service, run command:

[root@localhost ~] #  sudo systemctl start mariadb

4. Securing MariaDB

MariaDB is not secured by default, the reason why we should remove anonymous user, test database and configure other settings. Run command:

[root@localhost ~] # sudo mysql_secure_installation

You will be prompted to enter your MariaDB root password, just press Enter because you haven’t set any password yet. Please note that this should not be confused by the root password of your server.

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Please pause here! Attention! Read the RED text again carefully to avoid errors. Also you cannot proceed successfully next step below if you mess up this instruction. (Like what happened to me at first. You will be on the loop, where it will ALWAYS asks you the password. In my case, the CentOS computer I was working on has NO Desktop installed yet, I was really stuck for hours not knowing how to exit the loop. My last resort was to force reboot the computer, remove maria db and install it again.) Here's my MariaDB installation errors and solution.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

Okay back to installation step.I hope you were warned enough and follows the guidance above.

Message will display: PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press ENTER here.

Enter current password for root (ENTER for none): <Enter key>


You will be asked series of questions. Answers are provided for simplicity:

Set root password? [Y/n] Y
New password: Enter your password here
Re-enter new password: repeat your password
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

Once the installation is complete, you should see the following message:

 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB Installation should now be secure.


Your MariaDB server is now secure. Run command below to enable it during boot:

[root@localhost ~] #  sudo systemctl enable mariadb.service


5. Install PHP


To install PHP and MySQL extension using yum package installer, run command:

[root@localhost ~] # sudo yum install php php-mysql

Confirm the installation, by pressing Y and <Enter>.

We need to restart Apache web server one more time for it to work with PHP, using command:

[root@localhost ~] #   sudo systemctl restart httpd.service



Test PHP

Apache installs a default website on the directory ‘/var/www/html/’. To test PHP on our CentOs 7 Server, we need to create a file there using a nano editor.

First, let’s install nano by running the command below:

[root@localhost ~] # sudo yum install nano

Next, create ‘info.php’ file by typing the command:

[root@localhost ~] # sudo nano /var/www/html/info.php

A blank text file should be displayed, just copy paste the content below on the page:

<?php 
phpinfo(); 
?>

Then press CTRL + X, Y and Enter to save the changes

To check whether PHP is working, open your browser type in the address bar: 
localhost/info.php
or
192.168.xxx.xx/info.php


This page will display on your browser once PHP runs successfully on your CentOS 7 server. 

LAMP has successfully been installed in your computer.

6. Install PhpMyAdmin





Success! You have completed your installation!

Hope this helps!