If you have a PHP script, perhaps purchased from a place like Codecanyon, and you want to install it, or if you’re a PHP developer looking to quickly learn how to publish a website or PHP script online, you’ve come to the right spot.
In this guide, I’ll walk you through the following topics:
Table of Contents
- Requirements to Publish PHP Website.
- Get a Domain Name.
- Get a VPS Server.
- Map Server IP to Domain.
- Create a MySQL Database For Your Application.
- Uploading Files to the Server using WinSCP.
- Installing PHP Modules.
- Configure Apache to Publish PHP Website
- Managing MySQL Databases.
- Setup cronjobs.
- Secure with a Free SSL
Requirements to Publish a PHP Script or Website.
What you need is simply:
- A VPS Server to install your PHP Script on.
- Domain Name to use with our setup.
1. Get a Domain Name To Publish a PHP Script or Website.
Getting a domain is as easy as grabbing a sweet treat. All you have to do is visit a domain registrar company like:
Purchase a domain name from any of the above domain registrars. You can even find domains for as low as $1 per year.
I recommend choosing a domain name that matches your business. If you already have one, you can skip this step and proceed to get a VPS server.
Take a quick look at this 3-minute video to see how to buy a domain name from Namecheap:
2. Get a VPS Server.
Now, it’s time to get our VPS server to set up our PHP script on.
What is a VPS Server?
When you want to make a website, put up a PHP script, or launch any web application, you need something called “web hosting“. This just means you’re putting your files and database on a public server so that they can be accessed online via the internet.
Hosting Options.
Now, when you want to host a web application, like WordPress in our case, you generally have two main options:
Shared Hosting:
This involves putting your website on hosting services like InMotion, Godaddy, Hostinger, and more. Your site will share a server with many other websites.
VPS Hosting:
In this setup, you’ll have your website hosted on your very own server, such as Cloudways or InMotion. This type of server is known as a VPS (Virtual Private Server), and it’s exclusively dedicated to your website.
Which one is better?
I won’t dive into the technical details now, but in simple terms, a VPS is definitely better. With it, you get your very own server that you can fully control, and it performs really well.
And if you think shared hosting is much cheaper, think again. With Contabo’s VPS services, you can start at just $8.49 per month, which is the same price as nearly 85% of shared hosting services.
The only potential downside is that it might require some extra technical know-how to set up and configure. But don’t worry, you’re in the right place for a step-by-step guide that makes things super easy!
Which company should you choose to publish your PHP script?
You have the freedom to pick any company you prefer to purchase a VPS server. In this guide, I’ll suggest two companies that offer great features at affordable prices, and I’ve personally used them to run over 6 servers for my business. These companies are Contabo and Cloudways.
However, the choice ultimately lies with you. If you feel more comfortable with another company, like Hostinger, which also offers competitive pricing, that’s entirely fine.
Note: In this tutorial, I’ll demonstrate the process using Contabo. So, let’s explore how to get a VPS server from Contabo.
Get a VPS Server From Contabo.
Step 1: Open Contabo Website.
Open your web browser and navigate to Contabo VPS Page.
You’ll encounter various VPS setup choices when you begin. To kick things off, you can go with the most budget-friendly option for installing WordPress, and it’ll work just fine.
As your business expands, you can upgrade to a more robust VPS with greater resources.
Step 2: Choosing VPS Settings
On the next page, you’ll need to make a few selections:
Under the “Operating System” option, pick Ubuntu 18.04.
Select Ubuntu 18.04
Then in the Administration Panel, Select LAMP
SELECT LAMP
For the final choice, go to Addons and make sure to tick the box for “Individual reverse DNS.”
Add PTR
If you’re asked to input the PTR record, just type in your domain name, and you’re all set.
Next, proceed to the billing page and confirm your order. I believe the remaining steps are straightforward and easy to understand.
Within about 24 hours, the Contabo Team will set up the server and send you an email with the details.
For Digital Ocean.
If you’d like to use Digital Ocean, just sign up using the coupon link below, and you’ll receive a free $200 credit to try everything out at no cost.
Once you’ve signed up for Digital Ocean, make a Droplet with these settings:
- Choose Ubuntu 16/18/20 x64 as your operating system.
- Begin with 1 CPU and 1 GB of RAM (you can change this later).
Awesome! You now have your server up and running.
3. Let’s connect your Domain Name to your VPS Server.
Now that we have a Domain and a VPS Server, they’re not linked yet!
Currently, you can access your VPS Server using the IP address Contabo’s Team sent you via email. However, we don’t want to use just an IP to access our WordPress website; we want to use our domain name.
So, what we’ll do is map the domain name to our VPS IP address. This way, we can access the VPS and the WordPress website using both the domain and the IP.
To make this happen, follow these steps to configure your DNS Records and publish your website or PHP script:
- Open the DNS management zone in your Domain Provider (like Godaddy, in my case).
- Edit the main A record like this:
- Host: @
- Points to: YOUR SERVER IP.
By doing this, you’ll connect your domain to your VPS Server, making it accessible via both the domain name and the IP address.
Done? Greate!
4. Set Up a MySQL Database for Your Application.
To launch a website or PHP script, most of them will need a MySQL Database. So, let’s make one.
Connect to your VPS server using an SSH client like Putty, and let’s get started!
Simply open Putty and enter the Server IP or Name, then click “Open.”
You’ll be prompted to log in; use “root” as the username, and then enter the password sent to you via email.
Note: When typing the password, Putty won’t display it for security, but it’s still being entered, so just type it as instructed.
Logging in with Putty:
Once you’re in, here’s the good news: MySQL is already installed as part of the LAMP Stack. However, we still need to create a database and a user for our application.
To begin, log in to the MySQL root (administrative) account by using this command:
mysql -u root -p
You’ll be asked for the root password; simply type or paste it and hit Enter.
Next, let’s make a separate database for your PHP application. You can choose any name you like, but for this guide, we’ll keep it simple and call it “TestDb.” Create this database by running the following command:
CREATE DATABASE TestDb DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Important: Remember to end every MySQL statement with a semi-colon (;).
Create MySQL User Account
Now, let’s make a distinct MySQL user account that we’ll only use for our new database. This approach is smart for both management and security. In this guide, we’ll call this user “DbUser,” but you can pick a different name if you prefer.
We’ll create this account, set a strong password, and provide access to the database we made. You can do this by typing the following command. Ensure you choose a robust password for your database user:
CREATE USER 'DbUser'@'localhost' IDENTIFIED BY 'enter_a_custom_password_here';
GRANT ALL ON TestDb.* TO 'DbUser'@'localhost';
You’ve successfully created a database and a user account, both designed exclusively for your PHP application. Now, we must refresh the privileges so that the current MySQL system is aware of the recent changes we’ve applied.
FLUSH PRIVILEGES;
Exit out of MySQL by typing:
EXIT;
It’s Done!
5. Upload The Script Files To The Server.
To upload your files, you’ll need a program called WinSCP. It’s a free tool, and you can download it here.
Once you have WinSCP, connect to your VPS Server. Then, update the PHP files to this path: “/var/www/html.”
Usually, the files come in a zipped format, so we’ll need to unzip them.
Now, go back to the Putty SSH client, and let’s install a utility to unzip files. Use this command:
sudo apt-get install unzip
Clear the Putty screen to make it clean with this command:
clear
Change the directory to /var/www/html using this command:
cd /var/www/html.
Unzip the “.zip” file using this command:
unzip YOURFILENAME*.zip
Remember to replace “YOURFILENAME” with the actual name of your file if it’s different.
Now, move the contents of the latest folder to the upper directory for direct access. You can do this using WinSCP. Just copy all the files inside the “YOURFILENAME” folder and paste them directly into the html folder.
Congratulations, your files are uploaded. You’re done!
6. Installing PHP Modules To Publish PHP Website (optional)
Certain PHP applications may need extra PHP modules on your server, as indicated in the app’s documentation.
To add these PHP modules:
Open Putty and use the following commands to install them:
sudo apt install php-curl
sudo apt install php-mbstring
sudo apt install php-example
Once the modules are installed, restart the Apache service with this command:
sudo service apache2 restart
That’s it! Your server is now ready with the necessary PHP modules for your application.
7. Configure Apache to Publish PHP Script or Website
Great job! Your files are now uploaded and located in the html folder. The next step is to configure Apache so that you can publish your PHP website and access your script using your domain name.
To do this, open WinSCP once more and go to this directory: /etc/apache2/sites-available/
Now, create a new empty file and give it a name like “yourdomain.conf.” You can replace “Scriptname” with any name you prefer (for instance, mautic.conf).
Open yourdomain.conf Then copy and paste the content below into the file and save.
<VirtualHost *:80>
ServerAdmin admin@YOUDOMAINNAME
DocumentRoot /var/www/html
ServerName YOUDOMAINNAME
ServerAlias YOUDOMAINNAME
<Directory /var/www/html/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Replace “YOURDOMAINNAME” with your actual domain name.
To enable your script’s configuration in Apache and apply the changes, follow these simple steps:
Now, just execute the following commands to complete the setup:
sudo a2ensite yourdomain.conf
sudo a2enmod rewrite
sudo systemctl restart apache2.service
GREAT! You are almost done
8. Connect to MySQL Database.
Now, open your web browser and go to your server’s address.
You should see a setup completion wizard like the one below:
Follow the setup by providing the database details and your admin login information. In our example, the database name is TestDb, and the user is DBUser with the set password.
It’s that easy! Take a look at the example below from a similar script.
Link to database. Done!
9. Setup cronjobs after Publishing PHP Script.
What are cronjobs?
Cron is a tool in Linux that allows you to schedule a command or script to run automatically on your server at a specific time and date. A cron job is the actual task that gets scheduled. Cron jobs are super handy for automating repetitive tasks.
For instance, you can create a cron job to delete temporary files every week, which helps save disk space. Some PHP web applications might need you to set up cron jobs to perform certain functions.
Setting up cronjobs:
To set up cron jobs, you must add all the required tasks to your server. The specific tasks depend on the script you’re using. If you purchased a script from a place like Code Canyon, the script’s owner should provide you with information about the necessary cron jobs.
Here’s how to do it:
- Open WinSCP and go to the /etc directory.
- Open the crontab file using an editor.
- Copy and paste all the cron jobs from your script’s documentation into this file. Each job should be on its own line, and there should be an empty line at the end.
That’s it! Your cron jobs are set up and ready to go. ✔️
10. Secure with a Free SSL.
SSL certificates play a crucial role in web servers by encrypting the data exchanged between the server and users, making your application more secure. Luckily, Let’s Encrypt offers a simple way to get trusted certificates for free.
Here’s how to set it up:
Step 1: Install the Let’s Encrypt Client
Let’s Encrypt certificates are obtained using software on your server, with the official client known as Certbot.
Start by adding the Certbot repository with this command:
sudo add-apt-repository ppa:certbot/certbot
Press ENTER to confirm.
Update your package list to include the new repository’s information:
sudo apt-get update
Now, install Certbot’s Apache package:
sudo apt install python-certbot-apache
Certbot is now installed and ready for use.
Step 2 — Set Up the SSL Certificate
Generating an SSL certificate for Apache using Certbot is quite easy. The client takes care of getting and installing a new SSL certificate that works for the provided domains.
Here’s what to do:
Run this command (but replace “YOUR_DOMAIN_NAME” with your actual domain):
sudo certbot --apache -d YOUR_DOMAIN_NAME
You’ll be asked for an email address for key recovery and notices. You can also choose between allowing both HTTP and HTTPS access or forcing all requests to use HTTPS. In most cases, it’s safer to require HTTPS, unless you have a specific reason for using unencrypted HTTP.
That’s it!
Now, test your website, and you should see that it’s SSL encrypted.
You’re all set!