One place for hosting & domains

      How To Move an Nginx Web Root to a New Location on Ubuntu 20.04


      Introduction

      On Ubuntu, the Nginx web server stores its documents in /var/www/html, which is typically located on the root filesystem with the rest of the operating system. Sometimes, though, it’s helpful to move the document root to another location, such as a separate mounted filesystem. For example, if you serve multiple websites from the same Nginx instance, putting each site’s document root on its own volume allows you to scale in response to the needs of a specific site or client.

      In this guide, you will move an Nginx document root to a new location.

      Prerequisites

      To complete this guide, you will need:

      We will use the domain name your_domain in this tutorial, but you should substitute this with your own domain name.

      • A new location for your document root. In this tutorial, we will use the /mnt/volume-nyc3-01 directory for our new location. If you are using Block Storage on DigitalOcean, this guide will show you how to create and attach your volume. Your new document root location is configurable based on your needs, however. If you are moving your document root to a different storage device, you will want to select a location under the device’s mount point.

      Step 1 — Copying Files to the New Location

      On a fresh installation of Nginx, the document root is located at /var/www/html. By following the prerequisite guides, however, you created a new document root, /var/www/your_domain/html. You may have additional document roots as well. In this step, we will establish the location of our document roots and copy the relevant files to their new location.

      You can search for the location of your document roots using grep. Let’s search in the /etc/nginx/sites-enabled directory to limit our focus to active sites. The -R flag ensures that grep will print both the line with the root directive and the full filename in its output:

      • grep -R "root" /etc/nginx/sites-enabled

      If you followed the prerequisite tutorials on a fresh server, the result will look like this:

      Output

      /etc/nginx/sites-enabled/your_domain: root /var/www/your_domain/html; /etc/nginx/sites-enabled/default: root /var/www/html; /etc/nginx/sites-enabled/default: # deny access to .htaccess files, if Apache's document root /etc/nginx/sites-enabled/default:# root /var/www/your_domain;

      If you have pre-existing setups, your results may differ from what’s shown here. In either case, you can use the feedback from grep to make sure you’re moving the desired files and updating the appropriate configuration files.

      Now that you’ve confirmed the location of your document root, you can copy the files to their new location with rsync. Using the -a flag preserves the permissions and other directory properties, while -v provides verbose output so you can follow the progress of the sync:

      Note: Be sure there is no trailing slash on the directory, which may be added if you use tab completion. When there’s a trailing slash, rsync will dump the contents of the directory into the mount point instead of transferring it into a containing html directory.

      • sudo rsync -av /var/www/your_domain/html /mnt/volume-nyc3-01

      You will see output like the following:

      Output

      sending incremental file list created directory /mnt/volume-nyc3-01 html/ html/index.html sent 318 bytes received 39 bytes 714.00 bytes/sec total size is 176 speedup is 0.49

      With our files in place, let’s move on to modifying our Nginx configuration to reflect these changes.

      Step 2 — Updating the Configuration Files

      Nginx makes use of both global and site-specific configuration files. For background about the hierarchy of configuration files, take a look at “How To Configure The Nginx Web Server On a Virtual Private Server”. We will modify the server block file for our your_domain project: /etc/nginx/sites-enabled/your_domain.

      Note: Remember to replace your_domain with your domain name, and remember that you will be modifying the server block files that were output when you ran the grep command in Step 1.

      Start by opening /etc/nginx/sites-enabled/your_domain in an editor:

      • sudo nano /etc/nginx/sites-enabled/your_domain

      Find the line that begins with root and update it with the new root location. In our case this will be /mnt/volume-nyc3-01/html:

      /etc/nginx/sites-enabled/your_domain

      server {
      
              root /mnt/volume-nyc3-01/html;
              index index.html index.htm index.nginx-debian.html;
              . . .
      }
      . . .
      

      Keep an eye out for any other places that you see the original document root path outputted by grep in Step 1, including in aliases or rewrites. You will also want to update these to reflect the new document root location.

      When you’ve made all of the necessary changes, save and close the file.

      Step 3 — Restarting Nginx

      Once you’ve finished making the configuration changes, you can restart Nginx and test the results.

      First, make sure the syntax is correct:

      If everything is in order, it should return:

      Output

      nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

      If the test fails, track down and fix the problems.

      Once the test passes, restart Nginx:

      • sudo systemctl restart nginx

      When the server has restarted, visit your affected sites and ensure they’re working as expected. Once you’re comfortable that everything is in order, don’t forget to remove the original copy of the data:

      • sudo rm -Rf /var/www/your_domain/html

      You have now successfully moved your Nginx document root to a new location.

      Conclusion

      In this tutorial, we covered how to change the Nginx document root to a new location. This can help you with basic web server administration, like effectively managing multiple sites on a single server. It also allows you to take advantage of alternative storage devices such as network block storage, which can be helpful in scaling a web site as its needs change.

      If you’re managing a busy or growing web site, you might be interested in learning how to set up Nginx with HTTP/2 to take advantage of its high transfer speed for content.



      Source link

      How To Reset Your MySQL or MariaDB Root Password on Ubuntu 20.04


      The author selected the COVID-19 Relief Fund to receive a donation as part of the Write for DOnations program.

      Introduction

      Forgot your database password? It happens to the best of us. If you’ve forgotten or lost the root password to your MySQL or MariaDB database, you can still gain access and reset the password if you have access to the server and a user account with sudo privileges.

      This tutorial demonstrates how to reset the root password for MySQL and MariaDB databases installed with the apt package manager on Ubuntu 20.04. The procedure for changing the root password differs depending on whether you have MySQL or MariaDB installed and the default systemd configuration that ships with the distribution or packages from other vendors. While the instructions in this tutorial may work with other system or database server versions, they have been tested with Ubuntu 20.04 and distribution-supplied packages.

      Note: On fresh Ubuntu 20.04 installations, the default MySQL or MariaDB configuration usually allows you to access the database (with full administrative privileges) without providing a password as long as you make the connection from the system’s root account. In this scenario, it may not be necessary to reset the password. Before you proceed with resetting your database root password, try to access the database with the sudo mysql command. Only if the default configuration for authentication was altered, and this results in an access denied error, follow the steps in this tutorial.

      Prerequisites

      To recover your MySQL or MariaDB root password, you will need:

      Note: Both database installation guides retain the default configuration for the database root account where a password is not needed to authenticate, as long as you can access the system’s root account. You can still follow this guide to set and verify a new password.

      Step 1 — Identifying the Database Version and Stopping the Server

      Ubuntu 20.04 runs either MySQL or MariaDB—a popular drop-in replacement that is fully compatible with MySQL. You’ll need to use different commands to recover the root password depending on which of these you have installed, so follow the steps in this section to determine which database server you’re running.

      Check your version with the following command:

      If you’re running MariaDB, you’ll see “MariaDB” preceded by the version number in the output:

      MariaDB output

      mysql Ver 15.1 Distrib 10.3.25-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

      You’ll see output like this if you’re running MySQL:

      MySQL output

      mysql Ver 8.0.22-0ubuntu0.20.04.3 for Linux on x86_64 ((Ubuntu))

      Note the database you are running. This will determine the appropriate commands to follow in the rest of this tutorial.

      In order to change the root password, you’ll need to shut down the database server. If you’re running MariaDB, you can do so with the following command:

      • sudo systemctl stop mariadb

      For MySQL, shut down the database server by running:

      • sudo systemctl stop mysql

      With the database stopped, you can restart it in safe mode to reset the root password.

      Step 2 — Restarting the Database Server Without Permission Checks

      Running MySQL and MariaDB without permission checking allows accessing the database command line with root privileges without providing a valid password. To do this, you need to stop the database from loading the grant tables, which store user privilege information. Since this is a bit of a security risk, you may also want to disable networking to prevent other clients from connecting to the temporarily vulnerable server.

      Depending on which database server you’ve installed, the way of starting the server without loading the grant tables differs.

      Configuring MariaDB to Start Without Grant Tables

      In order to start the MariaDB server without the grant tables, we’ll use the systemd unit file to set additional parameters for the MariaDB server daemon.

      Execute the following command, which sets the MYSQLD_OPTS environment variable used by MariaDB upon startup. The --skip-grant-tables and --skip-networking options tell MariaDB to start up without loading the grant tables or networking features:

      • sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking"

      Then start the MariaDB server:

      • sudo systemctl start mariadb

      This command won’t produce any output, but it will restart the database server, taking into account the new environment variable settings.

      You can ensure it started with sudo systemctl status mariadb.

      Now you should be able to connect to the database as the MariaDB root user without supplying a password:

      You’ll immediately see a database shell prompt:

      Now that you have access to the database server, you can change the root password as shown in Step 3.

      Configuring MySQL to Start Without Grant Tables

      In order to start the MySQL server without its grant tables, you’ll alter the systemd configuration for MySQL to pass additional command-line parameters to the server upon startup.

      To do this, execute the following command:

      • sudo systemctl edit mysql

      This command will open a new file in the nano editor, which you’ll use to edit MySQL’s service overrides. These change the default service parameters for MySQL.

      This file will be empty. Add the following content:

      MySQL service overrides

      [Service]
      ExecStart=
      ExecStart=/usr/sbin/mysqld --skip-grant-tables --skip-networking
      

      The first ExecStart statement clears the default value, while the second one provides systemd with the new startup command, including parameters to disable loading the grant tables and networking capabilities.

      Press CTRL-x to exit the file, then Y to save the changes that you made, then ENTER to confirm the file name.

      Reload the systemd configuration to apply these changes:

      • sudo systemctl daemon-reload

      Now start the MySQL server:

      • sudo systemctl start mysql

      The command will show no output, but the database server will start. The grant tables and networking will not be enabled.

      Connect to the database as the root user:

      You’ll immediately see a database shell prompt:

      Now that you have access to the server, you can change the root password.

      Step 3 — Changing the Root Password

      The database server is now running in a limited mode; the grant tables are not loaded, and there’s no networking support enabled. This lets you access the server without providing a password, but it prohibits you from executing commands that alter data. To reset the root password, you must load the grant tables now that you’ve gained access to the server.

      Tell the database server to reload the grant tables by issuing the FLUSH PRIVILEGES command:

      You can now change the root password. The method you use depends on whether you are using MariaDB or MySQL.

      Changing the MariaDB Password

      If you are using MariaDB, execute the following statement to set the password for the root account, making sure to replace new_password with a strong new password that you’ll remember:

      • ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

      You’ll see this output indicating that the password changed:

      Output

      Query OK, 0 rows affected (0.001 sec)

      MariaDB allows using custom authentication mechanisms, so execute the following two statements to make sure MariaDB will use its default authentication mechanism for the new password you assigned to the root account:

      • UPDATE mysql.user SET authentication_string = '' WHERE user="root";
      • UPDATE mysql.user SET plugin = '' WHERE user="root";

      You’ll see the following output for each statement:

      Output

      Query OK, 0 rows affected (0.01 sec)

      The password is now changed. Type exit to exit the MariaDB console and proceed to Step 4 to restart the database server in normal mode.

      Changing the MySQL Password

      For MySQL, execute the following statement to change the root user’s password, replacing new_password with a strong password you’ll remember. MySQL allows using custom authentication mechanisms, so the following statement also makes sure that MySQL will use its default authentication mechanism to authenticate the root user using the new password:

      • ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'new_password';

      You’ll see this output indicating the password was changed successfully:

      Output

      Query OK, 0 rows affected (0.01 sec)

      The password is now changed. Exit the MySQL console by typing exit.

      Let’s restart the database in normal operational mode.

      Step 4 — Reverting Your Database Server to Normal Settings

      In order to restart the database server in its normal mode, you have to revert the changes you made so that networking is enabled and the grant tables are loaded. Again, the method you use depends on whether you used MariaDB or MySQL.

      For MariaDB, unset the MYSQLD_OPTS environment variable you set previously:

      • sudo systemctl unset-environment MYSQLD_OPTS

      Then, restart the service using systemctl:

      • sudo systemctl restart mariadb

      For MySQL, remove the modified systemd configuration:

      • sudo systemctl revert mysql

      You’ll see output similar to the following:

      Output

      Removed /etc/systemd/system/mysql.service.d/override.conf. Removed /etc/systemd/system/mysql.service.d.

      Then, reload the systemd configuration to apply the changes:

      • sudo systemctl daemon-reload

      Finally, restart the service:

      • sudo systemctl restart mysql

      The database is now restarted and is back to its normal state. Confirm that the new password works by logging in as the root user with a password:

      You’ll be prompted for a password. Enter your new password, and you’ll gain access to the database prompt as expected.

      Conclusion

      You have restored administrative access to the MySQL or MariaDB server. Make sure the new password you chose is strong and secure, and keep it in a safe place.

      For more information on user management, authentication mechanisms, or ways of resetting database passwords for other versions of MySQL or MariaDB, please refer to the official MySQL documentation or MariaDB documentation.



      Source link

      Reset the Root Password on your Linode


      Updated by Linode

      Written by Nick Brewer

      This Quick Answer will show you how to reset the root password for the Linux distribution running on your Linode. See our Accounts and Passwords guide for additional information.

      1. Click the Linodes link in the sidebar.

      2. Select a Linode to navigate to its detail page.

      3. If the Linode’s Status menu displays Offline, proceed to step 6.

      4. If the Linode’s Status menu displays Running, click on the Status menu.

      5. Then, select the Power Off option from the exposed dropdown menu. A progress bar will appear that you can use to monitor the status of this operation.

        Power off a Linode

      6. When the Linode is offline, navigate to the Linode’s Settings tab.

      7. Under Settings, click on Reset Root Password to expand that panel.

      8. Select your primary disk from the Disk dropdown menu.

      9. Enter a new password for the root user in the Password field.

      10. Click Save

      11. Return to the Linode’s Summary tab.

      12. Click on the Status menu, then select the Power On option from the exposed dropdown menu to power on your Linode.

      Find answers, ask questions, and help others.

      This guide is published under a CC BY-ND 4.0 license.



      Source link