{"id":552,"date":"2020-04-04T03:18:00","date_gmt":"2020-04-03T19:18:00","guid":{"rendered":"http:\/\/www.sysadmindayph.com\/blog\/?p=552"},"modified":"2020-04-04T03:18:03","modified_gmt":"2020-04-03T19:18:03","slug":"installing-linux-apache-mysql-php-lamp-stack-on-ubuntu-18-04-on-an-aws-ec2-instance","status":"publish","type":"post","link":"https:\/\/www.sysadmindayph.com\/blog\/installing-linux-apache-mysql-php-lamp-stack-on-ubuntu-18-04-on-an-aws-ec2-instance\/","title":{"rendered":"Installing Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 18.04 &#8211; on an AWS EC2 Instance"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>Step 1 \u2014 Installing Apache<\/p>\n\n\n\n<p>Install Apache using Ubuntu\u2019s package manager,&nbsp;<code>apt<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt install apache2<\/code><\/pre>\n\n\n\n<p>Test after staring service &#8211; systemctl start apache2<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;your_server_ip<\/code><\/pre>\n\n\n\n<p>How To Find your Server\u2019s Public IP Address<\/p>\n\n\n\n<p>ip addr show eth0 | grep inet | awk &#8216;{ print $2; }&#8217; | sed &#8216;s\/\\\/.*$\/\/&#8217;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Step 2 \u2014 Installing MySQL<\/p>\n\n\n\n<p>Again, use&nbsp;<code>apt<\/code>&nbsp;to acquire and install this software:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install mysql-server<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mysql_secure_installation\n<\/code><\/pre>\n\n\n\n<p>This will ask if you want to configure the\u00a0<code>VALIDATE PASSWORD PLUGIN<\/code>.<\/p>\n\n\n\n<p>If you prefer to use a password when connecting to MySQL as&nbsp;<strong>root<\/strong>, you will need to switch its authentication method from&nbsp;<code>auth_socket<\/code>&nbsp;to&nbsp;<code>mysql_native_password<\/code>. To do this, open up the MySQL prompt from your terminal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mysql\n<\/code><\/pre>\n\n\n\n<p>Next, check which authentication method each of your MySQL user accounts use with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT user,authentication_string,plugin,host FROM mysql.user;\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output+------------------+-------------------------------------------+-----------------------+-----------+\n| user             | authentication_string                     | plugin                | host      |\n+------------------+-------------------------------------------+-----------------------+-----------+\n| root             |                                           | auth_socket           | localhost |\n| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |\n| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |\n| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |\n+------------------+-------------------------------------------+-----------------------+-----------+\n4 rows in set (0.00 sec)\n<\/code><\/pre>\n\n\n\n<p>In this example, you can see that the&nbsp;<strong>root<\/strong>&nbsp;user does in fact authenticate using the&nbsp;<code>auth_socket<\/code>&nbsp;plugin. To configure the&nbsp;<strong>root<\/strong>&nbsp;account to authenticate with a password, run the following&nbsp;<code>ALTER USER<\/code>&nbsp;command. Be sure to change&nbsp;<code>password<\/code>&nbsp;to a strong password of your choosing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';\n<\/code><\/pre>\n\n\n\n<p>Then, run&nbsp;<code>FLUSH PRIVILEGES<\/code>&nbsp;which tells the server to reload the grant tables and put your new changes into effect:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FLUSH PRIVILEGES;\n<\/code><\/pre>\n\n\n\n<p>Check the authentication methods employed by each of your users again to confirm that&nbsp;<strong>root<\/strong>&nbsp;no longer authenticates using the&nbsp;<code>auth_socket<\/code>&nbsp;plugin:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT user,authentication_string,plugin,host FROM mysql.user;\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output+------------------+-------------------------------------------+-----------------------+-----------+\n| user             | authentication_string                     | plugin                | host      |\n+------------------+-------------------------------------------+-----------------------+-----------+\n| root             | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost |\n| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |\n| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |\n| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |\n+------------------+-------------------------------------------+-----------------------+-----------+\n4 rows in set (0.00 sec)\n<\/code><\/pre>\n\n\n\n<p>You can see in this example output that the&nbsp;<strong>root<\/strong>&nbsp;MySQL user now authenticates using a password. Once you confirm this on your own server, you can exit the MySQL shell:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exit\n<\/code><\/pre>\n\n\n\n<p>At this point, your database system is now set up and you can move on to installing PHP, the final component of the LAMP stack.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Step 3 \u2014 Installing PHP<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Once again, leverage the&nbsp;<code>apt<\/code>&nbsp;system to install PHP. In addition, include some helper packages this time so that PHP code can run under the Apache server and talk to your MySQL database:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install php libapache2-mod-php php-mysql\n<\/code><\/pre>\n\n\n\n<p>This should install PHP without any problems. We\u2019ll test this in a moment.<\/p>\n\n\n\n<p>In most cases, you will want to modify the way that Apache serves files when a directory is requested. Currently, if a user requests a directory from the server, Apache will first look for a file called&nbsp;<code>index.html<\/code>. We want to tell the web server to prefer PHP files over others, so make Apache look for an&nbsp;<code>index.php<\/code>&nbsp;file first.<\/p>\n\n\n\n<p>To do this, type this command to open the&nbsp;<code>dir.conf<\/code>&nbsp;file in a text editor with root privileges:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/apache2\/mods-enabled\/dir.conf\n<\/code><\/pre>\n\n\n\n<p>It will look like this:\/etc\/apache2\/mods-enabled\/dir.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;IfModule mod_dir.c>\n    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm\n&lt;\/IfModule>\n<\/code><\/pre>\n\n\n\n<p>Move the PHP index file (highlighted above) to the first position after the&nbsp;<code>DirectoryIndex<\/code>&nbsp;specification, like this:\/etc\/apache2\/mods-enabled\/dir.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;IfModule mod_dir.c>\n    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm\n&lt;\/IfModule>\n<\/code><\/pre>\n\n\n\n<p>When you are finished, save and close the file by pressing\u00a0<code>CTRL+X<\/code>. Confirm the save by typing\u00a0<code>Y<\/code>\u00a0and then hit\u00a0<code>ENTER<\/code>\u00a0to verify the file save location.<\/p>\n\n\n\n<p>check Apache&#8217;s config file for syntax error:<\/p>\n\n\n\n<p>apachectl configtest<\/p>\n\n\n\n<p>After this, restart the Apache web server in order for your changes to be recognized. Do this by typing this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart apache2\n<\/code><\/pre>\n\n\n\n<p>You can also check on the status of the&nbsp;<code>apache2<\/code>&nbsp;service using&nbsp;<code>systemctl<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl status apache2\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Sample Output\u25cf apache2.service - LSB: Apache2 web server\n   Loaded: loaded (\/etc\/init.d\/apache2; bad; vendor preset: enabled)\n  Drop-In: \/lib\/systemd\/system\/apache2.service.d\n           \u2514\u2500apache2-systemd.conf\n   Active: active (running) since Tue 2018-04-23 14:28:43 EDT; 45s ago\n     Docs: man:systemd-sysv-generator(8)\n  Process: 13581 ExecStop=\/etc\/init.d\/apache2 stop (code=exited, status=0\/SUCCESS)\n  Process: 13605 ExecStart=\/etc\/init.d\/apache2 start (code=exited, status=0\/SUCCESS)\n    Tasks: 6 (limit: 512)\n   CGroup: \/system.slice\/apache2.service\n           \u251c\u250013623 \/usr\/sbin\/apache2 -k start\n           \u251c\u250013626 \/usr\/sbin\/apache2 -k start\n           \u251c\u250013627 \/usr\/sbin\/apache2 -k start\n           \u251c\u250013628 \/usr\/sbin\/apache2 -k start\n           \u251c\u250013629 \/usr\/sbin\/apache2 -k start\n           \u2514\u250013630 \/usr\/sbin\/apache2 -k start\n<\/code><\/pre>\n\n\n\n<p>Press\u00a0<code>Q<\/code>\u00a0to exit this status output.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-4-\u2014-setting-up-virtual-hosts-recommended\">Step 4 \u2014 Setting Up Virtual Hosts (Recommended)<\/h2>\n\n\n\n<p>When using the Apache web server, you can use&nbsp;<em>virtual hosts<\/em>&nbsp;(similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server. We will set up a domain called&nbsp;<strong>your_domain<\/strong>, but you should&nbsp;<strong>replace this with your own domain name<\/strong>. To learn more about setting up a domain name with DigitalOcean, see our&nbsp;<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/an-introduction-to-digitalocean-dns\">Introduction to DigitalOcean DNS<\/a>.<\/p>\n\n\n\n<p>Apache on Ubuntu 18.04 has one server block enabled by default that is configured to serve documents from the&nbsp;<code>\/var\/www\/html<\/code>&nbsp;directory. While this works well for a single site, it can become unwieldy if you are hosting multiple sites. Instead of modifying&nbsp;<code>\/var\/www\/html<\/code>, let\u2019s create a directory structure within&nbsp;<code>\/var\/www<\/code>&nbsp;for our&nbsp;<strong>your_domain<\/strong>&nbsp;site, leaving&nbsp;<code>\/var\/www\/html<\/code>&nbsp;in place as the default directory to be served if a client request doesn\u2019t match any other sites.<\/p>\n\n\n\n<p>Create the directory for&nbsp;<strong>your_domain<\/strong>&nbsp;as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mkdir \/var\/www\/your_domain\n<\/code><\/pre>\n\n\n\n<p>Next, assign ownership of the directory with the&nbsp;<code>$USER<\/code>&nbsp;environment variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo chown -R $USER:$USER \/var\/www\/your_domain\n<\/code><\/pre>\n\n\n\n<p>The permissions of your web roots should be correct if you haven\u2019t modified your&nbsp;<code>unmask<\/code>&nbsp;value, but you can make sure by typing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo chmod -R 755 \/var\/www\/your_domain\n<\/code><\/pre>\n\n\n\n<p>Next, create a sample&nbsp;<code>index.html<\/code>&nbsp;page using&nbsp;<code>nano<\/code>&nbsp;or your favorite editor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nano \/var\/www\/your_domain\/index.html\n<\/code><\/pre>\n\n\n\n<p>Inside, add the following sample HTML:\/var\/www\/your_domain\/index.html<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html>\n    &lt;head>\n        &lt;title>Welcome to Your_domain!&lt;\/title>\n    &lt;\/head>\n    &lt;body>\n        &lt;h1>Success!  The your_domain server block is working!&lt;\/h1>\n    &lt;\/body>\n&lt;\/html>\n<\/code><\/pre>\n\n\n\n<p>Save and close the file when you are finished.<\/p>\n\n\n\n<p>In order for Apache to serve this content, it\u2019s necessary to create a virtual host file with the correct directives. Instead of modifying the default configuration file located at&nbsp;<code>\/etc\/apache2\/sites-available\/000-default.conf<\/code>&nbsp;directly, let\u2019s make a new one at&nbsp;<code>\/etc\/apache2\/sites-available\/your_domain.conf<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/apache2\/sites-available\/your_domain.conf\n<\/code><\/pre>\n\n\n\n<p>Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain name:\/etc\/apache2\/sites-available\/your_domain.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;VirtualHost *:80>\n    ServerAdmin webmaster@localhost\n    ServerName your_domain\n    ServerAlias www.your_domain\n    DocumentRoot \/var\/www\/your_domain\n    ErrorLog ${APACHE_LOG_DIR}\/error.log\n    CustomLog ${APACHE_LOG_DIR}\/access.log combined\n&lt;\/VirtualHost>\n<\/code><\/pre>\n\n\n\n<p>Notice that we\u2019ve updated the&nbsp;<code>DocumentRoot<\/code>&nbsp;to our new directory and&nbsp;<code>ServerAdmin<\/code>&nbsp;to an email that the&nbsp;<strong>your_domain<\/strong>&nbsp;site administrator can access. We\u2019ve also added two directives:&nbsp;<code>ServerName<\/code>, which establishes the base domain that should match for this virtual host definition, and&nbsp;<code>ServerAlias<\/code>, which defines further names that should match as if they were the base name.<\/p>\n\n\n\n<p>Save and close the file when you are finished.<\/p>\n\n\n\n<p>Let\u2019s enable the file with the&nbsp;<code>a2ensite<\/code>&nbsp;tool:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2ensite your_domain.conf\n<\/code><\/pre>\n\n\n\n<p>Disable the default site defined in&nbsp;<code>000-default.conf<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2dissite 000-default.conf\n<\/code><\/pre>\n\n\n\n<p>Next, let\u2019s test for configuration errors:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apache2ctl configtest\n<\/code><\/pre>\n\n\n\n<p>You should see the following output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OutputSyntax OK\n<\/code><\/pre>\n\n\n\n<p>Restart Apache to implement your changes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart apache2\n<\/code><\/pre>\n\n\n\n<p>Apache should now be serving your domain name. You can test this by navigating to&nbsp;<code>http:\/\/your_domain<\/code>, where you should see something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.digitalocean.com\/articles\/apache_virtual_hosts_ubuntu\/vhost_your_domain.png\" alt=\"Apache virtual host example\"\/><\/figure>\n\n\n\n<p>With that, you virtual host is fully set up. Before making any more changes or deploying an application, though, it would be helpful to proactively test out your PHP configuration in case there are any issues that should be addressed.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Step 5 \u2014 Testing PHP Processing on your Web Server<\/p>\n\n\n\n<p>Create the file at the web root you created in the previous step by running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/var\/www\/your_domain\/info.php\n<\/code><\/pre>\n\n\n\n<p>This will open a blank file. Add the following text, which is valid PHP code, inside the file:info.php<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nphpinfo();\n?><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to solve the phpmyadmin not found issue 404 <\/h3>\n\n\n\n<p>Create a link in&nbsp;<code>\/var\/www<\/code>&nbsp;like this:<\/p>\n\n\n\n<p><code>sudo ln -s \/usr\/share\/phpmyadmin \/var\/www\/<\/code><\/p>\n\n\n\n<p><em>Note: since 14.04 you may want to use&nbsp;<code>\/var\/www\/html\/<\/code>&nbsp;instead of&nbsp;<code>\/var\/www\/<\/code><\/em><\/p>\n\n\n\n<p>If that&#8217;s not working for you, you need to include PHPMyAdmin inside apache configuration.<\/p>\n\n\n\n<p>Open&nbsp;<code>apache.conf<\/code>&nbsp;using your favorite editor, mine is&nbsp;<code>vim<\/code>&nbsp;\ud83d\ude42<\/p>\n\n\n\n<p><code>sudo vim \/etc\/apache2\/apache2.conf<\/code><\/p>\n\n\n\n<p>Then add the following line:<\/p>\n\n\n\n<p><code>Include \/etc\/phpmyadmin\/apache.conf<\/code><\/p>\n\n\n\n<p><strong>For Ubuntu 15.04 and 16.04<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>sudo ln -s \/etc\/phpmyadmin\/apache.conf \/etc\/apache2\/conf-available\/phpmyadmin.conf<\/code><\/li><li><code>sudo a2enconf phpmyadmin.conf<\/code><\/li><li><code>sudo service apache2 reload<\/code><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Step 1 \u2014 Installing Apache Install Apache using Ubuntu\u2019s package manager,&nbsp;apt: Test after staring service &#8211; systemctl start apache2 How To Find your Server\u2019s Public IP Address ip addr show eth0 | grep inet | &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[],"class_list":["post-552","post","type-post","status-publish","format-standard","hentry","category-apache"],"_links":{"self":[{"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts\/552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/comments?post=552"}],"version-history":[{"count":1,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts\/552\/revisions"}],"predecessor-version":[{"id":553,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts\/552\/revisions\/553"}],"wp:attachment":[{"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/media?parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/categories?post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/tags?post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}