Tutorial:Install LAMP Web Server with Linux

LAMP Web Server,The following tactics assist you put in an Apache web server with PHP and MySQL support for your Amazon Linux instance (from time to time referred to as a internet server stack). You can use this server to host a static website or installation a dynamic PHP application that reads and writes statistics to a database.

Important: To set up a LAMP internet server on Amazon Linux 2, see Tutorial: Install a LAMP Web Server on Amazon Linux 2. If you are trying to installation a LAMP net server on an Ubuntu or Red Hat Enterprise Linux instance, this tutorial will now not work for you. For extra information approximately other distributions, see their particular documentation. For records approximately LAMP net servers on Ubuntu, see the Ubuntu network documentation ApacheMySQLPHP topic.

Step 1: LAMP Web Server,Prepare the LAMP Server

LAMP Web Server,Prerequisites

LAMP Web Server,This tutorial assumes which you have already released a new instance the usage of the Amazon Linux AMI, with a public DNS name that is accessible from the net. For greater records, see Step 1: Launch an Instance. You ought to also have configured your safety institution to allow SSH (port 22), HTTP (port 80), and HTTPS (port 443) connections. For more statistics approximately those prerequisites, see Authorizing Inbound Traffic for Your Linux Instances.

LAMP Web Server,To install and start the LAMP web server with the Amazon Linux AMI

  1. Connect on your instance.
  2. To ensure that all of your software packages are up to date, carry out a quick software update to your instance. This method may additionally take a few minutes, however it’s far essential to make sure that you have the state-of-the-art security updates and malicious program fixes.

Tutorial:Install LAMP,The -y alternative installs the updates with out asking for affirmation.

[ec2-user ~]$ sudo yum update -y

3. Now that your example is current, you can set up the Apache web server, MySQL, and PHP software applications.

Note: Some packages may not be like minded with the following recommended software program environment. Before installing those programs, take a look at whether or not your LAMP programs are compatible with them. If there’s a problem, you could need to install an opportunity environment. For greater records, see The software software I need to run on my server is incompatible with the established PHP model or other software program

Prepare the LAMP Server

Tutorial:Install LAMP,Use the yum install command to put in multiple software applications and all related dependencies on the identical time.

[ec2-user ~]$ sudo yum install -y httpd24 php70 mysql56-server php70-mysqlnd

Note: If you get hold of the error No bundle package-call to be had, then your example become no longer released with the Amazon Linux AMI (possibly you are the use of Amazon Linux 2 rather).

cat /etc/system-release

4. Start the Apache web server.

[ec2-user ~]$ sudo service httpd start
Starting httpd:                                            [  OK  ]

5. Use the chkconfig command to configure the Apache net server to begin at every system boot.

[ec2-user ~]$ sudo chkconfig httpd on

Tutorial:Install LAMP,The chkconfig command does no longer provide any confirmation message when you efficiently use it to permit a service.

Tutorial:Install LAMP,You can verify that httpd is on with the aid of walking the following command:

[ec2-user ~]$ chkconfig --list httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

Tutorial:Install LAMP,Here, httpd is on in runlevels 2, three, 4, and five (that’s what you want to peer).

6.By default, a release-wizard-N safety group become installation to your example in the course of initialization. This group includes a unmarried rule to permit SSH connections.

  • Open the Amazon EC2 console at https://console.Aws.Amazon.Com/ec2/.
  • Choose Instances and pick out your example.
  • Under Security agencies, pick view inbound policies.
  •  
Security Groups associated with i-1234567890abcdef0
Ports     Protocol     Source     launch-wizard-N
22        tcp          0.0.0.0/0          ✔

server with the Amazon

Tutorial:Install LAMP,Using the techniques in Adding Rules to a Security Group, upload a new inbound protection rule with the following values:

  • Type: HTTP
  • Protocol: TCP
  • Port Range: 80
  • Source: Custom

7. Test your net server. In a web browser, type the public DNS deal with (or the public IP deal with) of your example. If there is no content in /var/www/html, you ought to see the Apache test page. You can get the general public DNS to your example using the Amazon EC2 console (take a look at the Public DNS column; if this column is hidden, select Show/Hide Columns (the gear-formed icon) and select Public DNS).

LAMP Web Server ,the LAMP web server with the Amazon

Tutorial:Install LAMP,If you are not able to see the Apache test web page, test that the safety institution you’re the usage of incorporates a rule to allow HTTP (port 80) traffic. For data about adding an HTTP rule on your safety group, see Adding Rules to a Security Group.

Important: For extra statistics approximately a way to configure the firewall, see the documentation to your precise distribution.

Note: This take a look at web page seems most effective while there’s no content material in /var/www/html. When you upload content material to the file root, your content seems at the public DNS address of your example instead of this take a look at page.

Tutorial:Install LAMP,Apache httpd serves documents that are kept in a listing referred to as the Apache file root. The Amazon Linux Apache record root is /var/www/html, which by way of default is owned via root.

[ec2-user ~]$ ls -l /var/www
total 16
drwxr-xr-x 2 root root 4096 Jul 12 01:00 cgi-bin
drwxr-xr-x 3 root root 4096 Aug  7 00:02 error
drwxr-xr-x 2 root root 4096 Jan  6  2012 html
drwxr-xr-x 3 root root 4096 Aug  7 00:02 icons
drwxr-xr-x 2 root root 4096 Aug  7 21:17 noindex

To allow the ec2-person account to manipulate files on this directory, you have to adjust the ownership and permissions of the listing. There are many methods to perform this assignment. In this academic, you add ec2-consumer to the apache institution, to give the apache institution possession of the /var/www listing and assign write permissions to the group.

To set file permissions

  1. Add your user (in this case, ec2-user) to the apache group.
[ec2-user ~]$ sudo usermod -a -G apache ec2-user

a. Log out (use the exit command or near the terminal window):

[ec2-user ~]$ exit

To verify your membership inside the apache organization, reconnect for your instance, after which run the subsequent command:

[ec2-user ~]$ groups
ec2-user wheel apache

3. Change the group ownership of /var/www and its contents to the apache group.

[ec2-user ~]$ sudo chown -R ec2-user:apache /var/www

4. To upload group write permissions and to set the group ID on destiny subdirectories, alternate the directory permissions of /var/www and its subdirectories.

[ec2-user ~]$ sudo chmod 2775 /var/www
[ec2-user ~]$ find /var/www -type d -exec sudo chmod 2775 {} \;

5. To upload organization write permissions, recursively trade the document permissions of /var/www and its subdirectories:

[ec2-user ~]$ find /var/www -type f -exec sudo chmod 0664 {} \;

Now, ec2-person (and any future members of the apache group) can upload, delete, and edit documents in the Apache document root, enabling you to feature content material, consisting of a static website or a PHP application.

(Optional) Secure your web server

A net server running the HTTP protocol offers no shipping protection for the information that it sends or receives.The best exercise for securing your web server is to put in support for HTTPS (HTTP Secure), which protects your facts with SSL/TLS encryption.

For data approximately allowing HTTPS in your server, see Tutorial: Configure SSL/TLS on Amazon Linux.

Step 2: Test Your Lamp Server

If your server is mounted and running, and your file permissions are set efficiently, your ec2-consumer account should be able to create a PHP document within the /var/www/html listing that is to be had from the internet.

To test your LAMP web server

  1. Create a PHP file in the Apache document root.
[ec2-user ~]$ echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

If you get a “Permission denied” mistakes while seeking to run this command, attempt logging out and logging back in once more to select up the right institution permissions which you configured in Step 1: Prepare the LAMP Server.

2. In an internet browser, type the URL of the record which you simply created. This URL is the public DNS address of your example observed through a forward curb and the document call. For example:

http://my.public.dns.amazonaws.com/phpinfo.php

test your LAMP

You should see the PHP information page:

If you do not see this page, verify that the /var/www/html/phpinfo.Php document turned into created nicely within the preceding step. The package deal variations within the second column do now not want to match this example output.

[ec2-user ~]$ sudo yum list installed httpd24 php70 mysql56-server php70-mysqlnd
Loaded plugins: priorities, update-motd, upgrade-helper
Installed Packages
httpd24.x86_64                          2.4.25-1.68.amzn1                    @amzn-updates
mysql56-server.x86_64                   5.6.35-1.23.amzn1                    @amzn-updates
php70.x86_64                            7.0.14-1.20.amzn1                    @amzn-updates
php70-mysqlnd.x86_64                    7.0.14-1.20.amzn1                    @amzn-updates

If any of the required applications are not listed on your output, set up them the use of the sudo yum deploy package command.

3. Delete the phpinfo.Hypertext Preprocessor report.

[ec2-user ~]$ rm /var/www/html/phpinfo.php

Step 3: Secure the Database Server

TheThe mysql_secure_installation command walks you thru the technique of placing a root password and casting off the insecure features from your set up.

To secure the database server

  1. Start the MySQL server.
[ec2-user ~]$ sudo service mysqld start
Initializing MySQL database:  
...

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
...

Starting mysqld:                                           [  OK  ]

2. Run mysql_secure_installation.

[ec2-user ~]$ sudo mysql_secure_installation

Database Server

  • Type the current root password. By default, the root account does no longer have a password set. Press Enter.
  • Type Y to set a password, and sort a cozy password two times. For more facts approximately creating a cozy password, see https://identitysafe.Norton.Com/password-generator/. Make sure to keep this password in a secure place.

Server

Note: Setting a root password for MySQL is only the maximum fundamental degree for securing your database. When you build or deploy a database-driven software, you commonly create a database carrier person for that application and keep away from using the root account for anything however database management.

b. put off the nameless user bills.

c. disable the faraway root login.

d. dispose of the take a look at database.

e.reload the privilege tables and save your modifications.

Database

3. (Optional) If you do no longer plan to apply the MySQL server proper away, forestall it.

[ec2-user ~]$ sudo service mysqld stop
Stopping mysqld:                                           [  OK  ]

4. (Optional) If you want the MySQL server to begin at every boot, type the subsequent command.

[ec2-user ~]$ sudo chkconfig mysqld on

You have to now have a fully purposeful LAMP internet server. If you add content material to the Apache document root at /var/www/html, you need to be capable of view that content at the general public DNS address in your example.

Step 4: (Optional) Install phpMyAdmin

To install phpMyAdmin

PhpMyAdmin is a web-based totally database management tool that you could use to view and edit the MySQL databases to your EC2 example. Follow the steps beneath to put in and configure phpMyAdmin for your Amazon Linux example.

For safety guidelines from the developers, see Securing your phpMyAdmin set up.

Note: This educational describes the way to deploy phpMyAdmin manually.

  1. Log in to your EC2 example using SSH.
  2. Install the desired dependencies.
[ec2-user ~]$ sudo yum install php70-mbstring.x86_64 php70-zip.x86_64 -y

3. Restart Apache.

[ec2-user ~]$ sudo service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

4. Navigate to the Apache document root at /var/www/html.

Install phpMyAdmin

[ec2-user ~]$ cd /var/www/html
[ec2-user html]$ 

5. Select a supply package deal for the trendy phpMyAdmin launch from https://www.Phpmyadmin.Net/downloads.

[ec2-user html]$ wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
[ec2-user html]$ mkdir phpMyAdmin && tar -xvzf phpMyAdmin-latest-all-languages.tar.gz -C phpMyAdmin --strip-components 1

7. Delete the phpMyAdmin-latest-all-languages.tar.gz tarball.

[ec2-user html]$ rm phpMyAdmin-latest-all-languages.tar.gz

8. (Optional) If the MySQL server is not running, start it now.

[ec2-user ~]$ sudo service mysqld start
Starting mysqld:                                           [  OK  ]

phpMyAdmin

In a web browser, kind the URL of your phpMyAdmin set up. This URL is the public DNS address (or the public IP cope with) of your instance observed by way of a ahead reduce and the call of your set up listing.

http://my.public.dns.amazonaws.com/phpMyAdmin

You should see the phpMyAdmin login page:

To configure phpMyAdmin, you can manually create a configuration record, use the setup console, or combine both strategies.

For data approximately using phpMyAdmin, see the phpMyAdmin User Guide.

Troubleshooting

I can not hook up with my server the use of a web browser. Perform the following exams to peer in case your Apache internet server is jogging and handy.

  • Is the internet server jogging?

You can verify that httpd is on with the aid of jogging the subsequent command:

[ec2-user ~]$ chkconfig --list httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

Troubleshooting

Here, httpd is on in runlevels 2, three, four, and five (that’s what you want to see). If the httpd method is not walking, repeat the stairs described in Step 1: Prepare the LAMP Server.

  • Is the firewall successfully configured?

If you are unable to look the Apache test page, take a look at that the security institution you are the use of incorporates a rule to permit HTTP (port 80) traffic. For statistics approximately including an HTTP rule to your safety institution, see Adding Rules to a Security Group.

LAMP Web Server,Troubleshooting

The software software I want to run on my server is incompatible with the established PHP model or other software program

This tutorial recommends installing the maximum updated versions of Apache HTTP Server, PHP, and MySQL.

LAMP Web Server

If the present day version of PHP isn’t always supported, it is viable (and completely safe) to downgrade to an in advance supported configuration. You can also installation more than one version of PHP in parallel, which solves certain compatibility problems with no less than effort. For facts about configuring a preference amongst a couple of installed PHP versions, see Amazon Linux AMI 2016.09 Release Notes.

How to downgrade

The properly-examined preceding version of this educational called for the following core LAMP packages:

  • httpd24
  • php56
  • mysql55-server
  • php56-mysqlnd

If you’ve got already mounted the modern packages as advocated on the start of this academic, you ought to first uninstall those packages and different dependencies as follows:

[ec2-user ~]$ sudo yum remove -y httpd24 php70 mysql56-server php70-mysqlnd perl-DBD-MySQL56

Next, install the replacement environment:

[ec2-user ~]$ sudo yum install -y  httpd24 php56 mysql55-server php56-mysqlnd

downgrade

If you make a decision later to upgrade to the endorsed environment

[ec2-user ~]$ sudo yum remove -y  httpd24 php56 mysql55-server php56-mysqlnd perl-DBD-MySQL55