{"id":8762,"date":"2020-07-11T10:02:37","date_gmt":"2020-07-11T14:02:37","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=8762"},"modified":"2021-12-04T07:54:53","modified_gmt":"2021-12-04T07:54:53","slug":"install-apache-mysql-php-ubuntu-20-04-lts","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/install-apache-mysql-php-ubuntu-20-04-lts\/","title":{"rendered":"How To Install Apache, MySQL, and PHP on Ubuntu 20.04 LTS"},"content":{"rendered":"\n
In this tutorial, we will be showing you how to set up a LAMP stack on an Ubuntu 20.04 server. LAMP is an acronym that represents the Linux operating system<\/a>, where we install an Apache web server with support for site data stored in a MySQL database, and dynamic content processed by PHP.<\/p>\n\n\n\n A LAMP stack is therefore a group of open-source software that is typically installed and working together in order to enable a server to host dynamic database-driven websites and web<\/a> apps written in PHP<\/a>.<\/p>\n\n\n\n Some components of the LAMP stack are interchangeable with other open-source software. For instance, Ubuntu<\/a> can be replaced with almost any other flavor of Linux<\/a> such as CentOS<\/a>, Redhat, Suse, Debian, Manjaro, and many others.<\/p>\n\n\n\n The database component can work with MariaDB, PostgreSQL, and others as opposed to MySQL. The web server component can use the likes of Nginx instead of Apache.<\/p>\n\n\n\n For brevity’s sake and for the purposes of having a complete tutorial, we will be sticking with Ubuntu 20.04, Apache, MySQL, and PHP. We will also be using the versions that come as the Linux server’s default package.<\/p>\n\n\n\n This tutorial assumes that you have an Ubuntu 20.04 server with a non-root sudo-enabled user account and a basic firewall. The steps in this tutorial can also work for Ubuntu 18.04 and prior versions to some extent.<\/p>\n\n\n\n Use Ubuntu’s package manager, apt to install Apache:<\/p>\n\n\n\n Accept the installation prompts by pressing Y<\/strong>, then ENTER<\/strong>.<\/p>\n\n\n\n Once the installation is finished, you will need to adjust your firewall configuration to allow HTTP traffic in and out of the server.<\/p>\n\n\n\n Here we are using the UFW firewall. This firewall comes with convenient application profiles that you can use to allow and block certain protocols and ports. To list all currently available UFW application profiles, you can run the following command:<\/p>\n\n\n\n The output will look something similar to this:<\/p>\n\n\n\n We are interested in the Apache<\/a> profiles. This is what each of the three Apache profiles means:<\/p>\n\n\n\n We will only allow connections on port 80 since this is a fresh install and we have not yet configured an SSL certificate. This can be changed later after the SSL certificate has been installed.<\/p>\n\n\n\n To only allow traffic on port 80, use the following Apache profile:<\/p>\n\n\n\n Verify the updated status using the following command:<\/p>\n\n\n\n Apache is now installed and ready to use. You can vary this by accessing your server’s public IP address in your web browser.<\/p>\n\n\n\n This should give you the default Ubuntu 20.04 Apache web page. It should look something like the one below and seeing this page means that Apache2 is installed and running well:<\/p>\n\n\n\n You can use the curl utility to contact icanhazip<\/strong> to tell you your server’s public IP address. Run the following command:<\/p>\n\n\n\n Next, we will need to install the database system to be able to store and manage data for your website. MySQL<\/a> the popular database management system is the natural choice to use within PHP environments.<\/p>\n\n\n\n We will use apt to acquire and install this software. We do not need to update the apt index since we did it a few minutes ago in Step 1<\/strong>. Run the following command to get started:<\/p>\n\n\n\n Confirm installation by typing Y<\/strong>, and then ENTER<\/strong> at the prompt.<\/p>\n\n\n\n After installation is finished you will need to run a security script that comes pre-installed with MySQL. This script removes some insecure default settings and locks down access to your MySQL database<\/a> server. You can run the interactive script by running:<\/p>\n\n\n\n You will be asked if you want to configure the VALIDATE PASSWORD PLUGIN<\/strong>. This plugin will enforce strong passwords. This can be left unconfigured but you will need to ensure you use a strong password.<\/p>\n\n\n\n Answer Y<\/strong> for yes<\/strong>, or anything else to continue without enabling.<\/p>\n\n\n\n If you answer “yes<\/strong>“, you\u2019ll be asked to select a level of password validation. The following is what you will expect to see.<\/p>\n\n\n\n Next, whether you chose to set up the VALIDATE PASSWORD PLUGIN<\/strong>, your server will next ask you to select and confirm a password for the MySQL root user. Either way, you should define a strong password here.<\/p>\n\n\n\n If you enabled password validation, you will be shown the password strength for the root password you just entered. Your server will ask if you want to continue with the password. If you are happy with your current password, enter Y<\/strong> for “yes<\/strong>” at the prompt:<\/p>\n\n\n\n For the rest of the questions, press Y<\/strong> and hit the ENTER<\/strong> key at each prompt.<\/p>\n\n\n\n These other prompts will remove the anonymous user, the test database, disable remote root logins, then load these new rules so that MySQL immediately adopts the changes.<\/p>\n\n\n\n You can now test your new password and installation by running the following to open the MySQL console.<\/p>\n\n\n\n This connects to the MySQL server as the administrative database user root inferred by using sudo. This is the output you should see:<\/p>\n\n\n\n You can exit the MySQL console to continue with the setup by running the following command:<\/p>\n\n\n\n Note: You didn’t need to provide a password to connect as the root user even though you set one via the mysql_secure_installation<\/strong> script. This is not a security leak as with Ubuntu and MySQL, the default authentication method for the administrative MySQL user is unix_socket<\/strong> instead of password<\/strong>.<\/p>\n\n\n\n This ensures only system users with sudo privileges are allowed to log in as the root MySQL user. Setting a password for the root MySQL account works as a safeguard, in case the default authentication method is changed from unix_socket<\/strong> to password<\/strong>.<\/p>\n\n\n\n Also because of this, you won’t be able to use the administrative database root user to connect from your PHP application.<\/p>\n\n\n\n Therefore for increased security, you will need to have dedicated user accounts with less expansive privileges set up for every database that will be used to access your MySQL server from your PHP applications.<\/p>\n\n\n\n You can follow this phpMyAdmin tutorial here<\/a> to learn how you can create additional users for use in your PHP applications as well as use it as a web-based client to manage your databases and their internal objects.<\/p>\n\n\n\n Note: It may currently be that the native MySQL PHP library mysqlnd <\/strong>doesn’t support caching_sha2_authentication<\/strong> which is the default authentication method for MySQL 8. Therefore, when creating database users for PHP applications on MySQL 8, you will need to make sure they are configured to use mysql_native_password<\/strong> instead.<\/em><\/p>\n\n\n\n Now that Apache and MySQL are installed, we are now ready to install PHP 7.4 packages which are available under the default repositories on Ubuntu 20.04 LTS. In addition to the PHP package, you will need to install the php-mysql<\/strong> module which will allow PHP to communicate with MySQL-based databases.<\/p>\n\n\n\n You will also need libapache2-mod-php<\/strong> to enable Apache to handle PHP files properly. The core PHP packages will be installed as dependencies automatically.<\/p>\n\n\n\n You can then run the following command to confirm your PHP version with the output below it:<\/p>\n\n\n\n You have now successfully completed the install Apache, MySQL, and PHP on Ubuntu 20.04 LTS as a LAMP stack.<\/p>\n\n\n\n You will need to manage the above services occasionally. These are a few commands to aid you in starting, stoping, and restarting the said components.<\/p>\n\n\n\n To restart Apache and MySQL services, type:<\/p>\n\n\n\n To start Apache and MySQL services, type:<\/p>\n\n\n\n To stop Apache and MySQL services, type:<\/p>\n\n\n\n Next, we will create a PHP test script to confirm that Apache is able to handle and process requests for PHP files. Earlier we had tested the server with the default index.html file but this time we will need to do the same with the PHP file.<\/p>\n\n\n\n Create a new file named info.php inside your document root folder. The location of the document folder can be gotten from the default Apache web page. Visit that page using your web browser.<\/p>\n\n\n\n In the Document Root section, you will find the default Ubuntu document root path which is usually \/var\/www\/html<\/strong>. We will save our file there using the following:<\/p>\n\n\n\n This will open a blank file. Add the following PHP code, inside the file:<\/p>\n\n\n\n When you are finished, save and close the file.<\/p>\n\n\n\n Go to your web browser and enter the following URL:<\/p>\n\n\n\n This page provides information about your server as PHP sees it. If you can see this page in your browser, then your PHP installation is working as expected. Use this page for debugging, confirming modules, and other server configurations when needed.<\/p>\n\n\n\n For now, it’s advisable to remove the file as it reveals sensitive information about your PHP environment and your Ubuntu server. You can always recreate this page if you need to access the information again later. Use rm to delete the file right now:<\/p>\n\n\n\n Now that you have reached this point, it means that you have successfully set up a base LAMP stack that is ready to serve websites and PHP applications. This is by no means a comprehensive tutorial that guides beyond the basics. <\/p>\n\n\n\n Here we have not addressed how to install Apache virtual hosts, how to connect to a MySQL database using PHP, or how to install an SSL certificate to encrypt your server requests and responses, or how to harden your server against cyber attacks<\/a> as well as DDOS attacks<\/a> and more. These will be covered in later tutorials.<\/p>\n","protected":false},"excerpt":{"rendered":" In this tutorial, we will be showing you how to set up a LAMP stack on an Ubuntu 20.04 server. LAMP is an acronym that represents the Linux operating system,…<\/p>\n","protected":false},"author":1,"featured_media":8763,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,9,16],"tags":[58,121,180,193,320,354,365,393,424,431,433,449,452,531,573,591,598,635,636],"yoast_head":"\nPrerequisites<\/h2>\n\n\n\n
Step 1: Install Apache and Update the Firewall Configuration<\/h2>\n\n\n\n
$ sudo apt update\n$ sudo apt install apache2<\/code><\/pre>\n\n\n\n
$ sudo ufw app list<\/code><\/pre>\n\n\n\n
Available applications:\n Apache\n Apache Full\n Apache Secure\n OpenSSH<\/code><\/pre>\n\n\n\n
$ sudo ufw allow in "Apache"<\/code><\/pre>\n\n\n\n
$ sudo ufw status<\/code><\/pre>\n\n\n\n
Status: active\n\nTo Action From\n-- ------ ----\nOpenSSH ALLOW Anywhere \nApache ALLOW Anywhere \nOpenSSH (v6) ALLOW Anywhere (v6) \nApache (v6) ALLOW Anywhere (v6) <\/code><\/pre>\n\n\n\n
http:\/\/your_server_ip<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
$ curl http:\/\/icanhazip.com<\/code><\/pre>\n\n\n\n
Step 2 \u2014 Install MySQL<\/h2>\n\n\n\n
$ sudo apt install mysql-server<\/code><\/pre>\n\n\n\n
$ sudo mysql_secure_installation<\/code><\/pre>\n\n\n\n
VALIDATE PASSWORD PLUGIN can be used to test passwords\nand improve security. It checks the strength of password\nand allows the users to set only those passwords which are\nsecure enough. Would you like to setup VALIDATE PASSWORD plugin?\n\nPress y|Y for Yes, any other key for No:<\/code><\/pre>\n\n\n\n
There are three levels of password validation policy:\n\nLOW Length >= 8\nMEDIUM Length >= 8, numeric, mixed case, and special characters\nSTRONG Length >= 8, numeric, mixed case, special characters and dictionary file\n\nPlease enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1<\/code><\/pre>\n\n\n\n
Estimated strength of the password: 100 \nDo you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y<\/code><\/pre>\n\n\n\n
$ sudo mysql<\/code><\/pre>\n\n\n\n
Welcome to the MySQL monitor. Commands end with ; or \\g.\nYour MySQL connection id is 22\nServer version: 8.0.19-0ubuntu5 (Ubuntu)\n\nCopyright (c) 2000, 2020, Oracle and\/or its affiliates. All rights reserved.\n\nOracle is a registered trademark of Oracle Corporation and\/or its\naffiliates. Other names may be trademarks of their respective\nowners.\n\nType 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.\n\nmysql> <\/code><\/pre>\n\n\n\n
mysql> exit<\/code><\/pre>\n\n\n\n
Step 3 \u2013 Install PHP<\/h2>\n\n\n\n
$ sudo apt install php libapache2-mod-php php-mysq<\/code><\/pre>\n\n\n\n
$ php -v<\/code><\/pre>\n\n\n\n
PHP 7.4.3 (cli) (built: Mar 26 2020 20:24:23) ( NTS )\nCopyright (c) The PHP Group\nZend Engine v3.4.0, Copyright (c) Zend Technologies\n with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies<\/code><\/pre>\n\n\n\n
Manage Services<\/h2>\n\n\n\n
$ sudo systemctl restart apache2\n$ sudo systemctl restart mysql<\/code><\/pre>\n\n\n\n
sudo systemctl start apache2\n$ sudo systemctl start mysql<\/code><\/pre>\n\n\n\n
$ sudo systemctl stop apache2\n$ sudo systemctl stop mysql<\/code><\/pre>\n\n\n\n
Step 4: Test PHP Processing on your Web Server<\/h2>\n\n\n\n
http:\/\/your_server_ip<\/code><\/pre>\n\n\n\n
http://your_server_ip<\/code><\/pre>\n\n\n\n
$ sudo nano \/var\/www\/html\/info.php<\/code><\/pre>\n\n\n\n
<?php\nphpinfo();<\/code><\/pre>\n\n\n\n
http:\/\/your_server_ip\/info.php<\/code><\/pre>\n\n\n\n
$ sudo rm \/var\/www\/html\/info.php<\/code><\/pre>\n\n\n\n
Conclusion<\/h2>\n\n\n\n