Introduction
A great way to put your PHP website online is to set it up on a web server, this guide will provide a step-by-step way on how you can get started with Amazon AWS by showing how to set up a Linux, Apache, MySQL, and PHP (LAMP) web server using the Amazon Lightsail service.
Amazon AWS
What is Amazon AWS?
Amazon Web Services (AWS) is a popular web hosting service that provides cloud servers rather than the standard physical web servers. The benefit of cloud web hosting is that you can create and configure the server hardware specification online via their dashboard and create a server in a matter of seconds, this is possible because the server is virtual among other virtual servers on dedicated hardware.
For PHP web development in particular Amazon AWS is a popular choice for PHP website hosting, lots of PHP developer roles reference Amazon AWS. If you haven't received the opportunity to work with Amazon AWS then this guide is a great way to get started and gain some experience.
Amazon Lightsail
Amazon Lightsail allows you to easily set up a cloud server which comes pre-configured with various software bundles allowing you to save time installing the software yourself.
Here are the following features that are included with Lightsail...
- Static IP Address
- DNS Management
- SSH Access
- SSD Storage
- Server monitoring
Amazon AWS Lightsail does come with a free trial for the first three months provided you are a new Amazon AWS user or an existing user who hasn't used the Lightsail service yet. Also, the free tier will only apply to the first three cheapest Lightsail options.
This table shows the pricing for the first four options for the Amazon AWS Lightsail service. These prices are specific to Linux servers as a lot of cases in web development Linux is used for web servers rather than Windows.
Price | Memory | CPU | HDD | Data transfer (Outbound) |
$3.50 | 512 MB | 1 Core | 20 GB | 1 TB Transfer |
$5.00 | 1 GB | 1 Core | 40 GB | 2 TB Transfer |
$10.00 | 2 GB | 1 Core | 60 GB | 3 TB Transfer |
$20.00 | 4 GB | 2 Core | 80 GB | 4 TB Transfer |
There are upper-tier packages priced at $40, $80, and $160 but if you're at a point when you require a high-specification server I would recommend using the other AWS services such as Amazon EC2.
Amazon Lightsail dashboard
You will require an Amazon AWS account, you can register an account with them however keep in mind you will need to submit credit/debit card details because you can be charged depending on the web services you use.
Once you login to your account you will be shown a dashboard page. If you can't see a link for Amazon Lightsail on the page then type Lightsail in the search bar at the top to go to the main Lightsail page.
Creating a web server
When you arrive at Lightsail for the first time you will be prompted with the following pop-up. Click the Let's get started button to begin setup for a server.
This page displays a list of options that can be selected.
Platform: Linux/Unix
Blueprint: Apps + OS - LAMP (PHP 8)
Scroll down the page to view and set the next options.
Networking Type: Dual-stack
Size: $5 US dollars per month
I recommend the $5 package for a server as it provides 1 GB RAM which I feel is the minimum required memory that you will need to host PHP websites and MySQL databases on the same server.
Scroll down further and you will see a text field to fill in details. Type in a name for the web server or use the default name.
You can also add some tags to the server if you want.
Once all the appropriate setting options are selected press the Create instance button to create the web server.
You will be returned to the dashboard and an entry for the new server will show up however it will be grayed out with the status of Pending. After a minute or so the server should be set up, if the block doesn't change try refreshing the page.
The block will show a new status of Running and the icons on the block will be colored orange to indicate that the server is active.
Setting up a website
Now that the web server is ready you can set up a website. Click on the three dots icon to show the menu and select the Manage option.
You will be taken to the page showing information on the server you selected. As you can see on the Connect page the IP address, username, and link to the server key are shown which is what you will need to access the server outside of the Amazon AWS website.
If you copy the IP address, paste it into the website URL bar in your web browser, and then press the Enter key then the default web page shows up that comes pre-setup with the web server.
For example, the IP address used in this guide is http://35.176.81.243.
You will want to use your domain for the website. Go to your domain register and select a domain to use, after choosing one set up a domain record that points to the IP address of the AWS Lightsail server.
AAA
This guide will use FileZilla, an FTP client to upload files to the Lightsail server.
Go to the GitHub devsites page and download the Zip file.
After downloading the Zip file, extract the files out of the Zip and then open up the Filezilla application or any other FTP Client you may have.
Move the devsites folder from your local PC to the htdocs folder.
If you go to the IP address again in the web browser but add /devsites after the URL you will see the website.
Go to the following path in the server.
/opt/bitnami/apache/conf/vhosts
Right-click in the panel that shows the file contents of the directory, then select the Create new file option.
A pop-up will request a name for the file, enter devsites-vhost.conf as shown, and press the OK button.
After creating the file right-click on it, and select the View/Edit option.
The domain must have a virtual host to connect to the devsites project.
Add the above text to the new file.
<VirtualHost 127.0.0.1:80 _default_:80>
ServerAlias lightsail-lamp.devpushprojects.com
DocumentRoot /opt/bitnami/apache/htdocs/devsites-laravel-main
<Directory "/opt/bitnami/apache/htdocs/devsites-laravel-main">
Options -Indexes +FollowSymLinks -MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
After adding the file contents save the file and the changes will be uploaded to the file through the Filezilla application.
The next step is to run some commands using a terminal, good thing that Amazon AWS Lightsail makes this easy. Go back to the Connect page for the server and click the Connect using SSH button.
A window will pop up as a terminal which connects you to the Lightsail server.
In the terminal run the following commands to set up the test website.
Change to the website directory with the below command.
cd htdocs/devsites-laravel-main
Use composer to install PHP packages.
composer install
Set the permissions to the user and group of daemon which is specific to bitnami, this command is required so Apache can read the website files.
sudo chown -R daemon:daemon ~/htdocs/devsites-laravel-mai
n
Copy the .env file which contains environment variables for the project.
sudo cp .env.example .env
Run the key:generate artisan command to generate an app key and add it to the .env file.
sudo php artisan key:generate
Restart Apache to apply the new virtual host file added.
sudo /opt/bitnami/ctlscript.sh restart apache
After following all the above commands in the terminal the website should load up without any issues. Go back to the web browser window and go to the domain URL assigned to the IP address to view the devsites project as shown below.
e.g. https://lightsail-lamp.devpushprojects.com
Conclusion
After going through this guide you will have successfully set up a PHP website in Amazon AWS Lightsail without too much hassle, the web server-related software comes pre-installed and configured when creating the Amazon AWS Lightsail server.
Use this as a quick and easy way to get your PHP websites online with ease, follow the same steps as above, and make alterations where necessary for your websites.