This is a detailed guide to setup your own VPS server for hosting with PHP 7.3, Apache 2.4, Centos 7, PhpMyAdmin, MariaDB 10.3. This is a simple setup and does not modify the configuration of Apache and PHP. Also, there will be no control panel. Everything has to be done via the shell. For free and easy to use control panel, use VestaCp. But the PHP version, Mysql server version will be old in VestaCp because it has not been updated since last 1 year minimum. But you will get mail, FTP, antivirus with the vestaCp. So, choose what suits your needs.
Pr-Requisite: You should have bought a VPS and must have SSH access.
Update the system :
yum -y update
If asked for y/N press y. It will take some time to update the system.
Install Apache server
sudo yum -y install httpd
Enable Apache to start at boot, and restart the service for the above changes to take effect :
sudo systemctl enable httpd.service
You will have your root directory as “/var/www/html” . To change it you should go to /ect/httpd directory. Use the following command
cd /etc/httpd/conf
It will contain configuration files.Find the httpd.conf and change the document root if you want to change the path from where you want to serve your files. Use the following command to edit the file:
vi httpd.conf
Press i to edit the file. And after you have done with changes, press the ESC key and type 😡 to save and exit.
As we have changed the default document directory for Apache. It may cause 403 HTTP error at later stage. That may be because of Selinux . Use command
getenforce
to check if Selinux is enabled. This command will output ‘Enforcing’, if Selinux is enabled. So, to disable Selinux use following command:
setenforce 0
Install firewall, if not installed. Check if already installed by using the following command:
firewalld
If the firewall is already installed then it will not show anything and if it not installed then it will show ‘command not found’. So to install use command
yum -y install firewalldsystemctl enable firewalldsystemctl start firewalld
Allow web traffic through firewall
sudo firewall-cmd --add-service=http --permanent && sudo firewall-cmd --add-service=https --permanent
sudo systemctl restart firewalld
Install MariaDB server: Add the repository by using the following commands :
cd /etc/yum.repos.d
vi mariadb.repo
Above command will open the text editor. Now add the following to the file after changing to the insert mode by pressing ‘i’
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
For latest and other versions you can generate above-mentioned repo from here. Now close the vi editor by pressing Esc key and then typing ‘:x’.
Install the MariaDB
sudo yum install MariaDB-server MariaDB-client
Set MariaDB to start at boot
sudo systemctl enable mariadb.service
Start the MariaDB server :
sudo systemctl start mariadb.service
Run below to secure MariaDB. You will be given the option to change the MariaDB root password, remove anonymous user accounts, disable root logins outside of localhost, and remove test databases and reload privileges. It is recommended that you answer yes to these options.
mysql_secure_installation
Install PHP . Install EPEL release repo
yum -y install epel-release
Now use the following command to add appropriate repos and update :
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
Refresh the repository :
sudo yum update
And finally,
yum --enablerepo=remi-php73 -y install php
yum --enablerepo=remi-php73 -y install php-xml php-soap php-xmlrpc php-mbstring php-json php-gd php-mcrypt php-mysqli
To edit the php configuration globally edit the file ‘/etc/php.ini’ and reload the Apache service
systemctl reload httpd.service
Now you have apache, php and mysql installed. For adding domains you have to use virtualhost function of apache. You can search that on the internet. The configuration files of Apache are in ‘/etc/httpd’ folder. Example of a virtual host is given below. It was save in file ‘/etc/httpd/conf.d/vihost.conf’
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /home/public_html
ServerName domain.com
ServerAlias www.domain.com
ErrorLog "/var/log/httpd/domain_error_log"
<Directory "/home/public_html/domain">
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
Require all granted
DirectoryIndex index.php index.html index.htm
</Directory>
</VirtualHost>
Verify If you have swap enabled by using the command
swapon -s
If it does not return anything than swap file has not been created. To create 4Gb swap file with name swapfile use following commands:
cd /
dd if=/dev/zero of=swapfile count=4096 bs=1MiB
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo vi /etc/fstab
Add following to the file opened:
/swapfile swap swap sw 0 0
Install latest PhpMyAdmin : Go to https://www.phpmyadmin.net/. Download the latest release using the ‘wget’ command. Move it to appropriate directory e.g. /usr/share/phpmyadmin. Now add the alias to your httpd.conf or create new conf file in /etc/httpd/conf.d . And you are ready to go. To Protect the directory of PhpMyAdmin with password read this article. Backup of article is here
TIP: If you get errors when your php script try to change files e.g. I got error while updating the wordpress plugins. Then you have to add those files to apache group and user. You can add those files to apache user and group using command (On RHEL) :
sudo chown -R apache:apache /home/dom/public_html
Where /home/dom/public_html is the directory where your web files are located.