9
How do you set up a LAMP server completely from scratch on an unused desktop PC? The Linux distro should be CentOS 6.7.
9
How do you set up a LAMP server completely from scratch on an unused desktop PC? The Linux distro should be CentOS 6.7.
11
Dislaimer: This procedures are done keeping in mind that the server being set up will NEVER be used in production, just for testing. I am not a system/network administrator, just a novice programmer asked to set up a server so take the procedures with a grain of salt.
Download CentOS from here. Choosing to download the CentOS-6.7-i386-bin-DVD1.iso is enough.
Burn the downloaded ISO to a DVD using an ISO burner tool. In our case, we used WinISO. Doing this should be easy but in case you are lost, a tutorial is avaliable.
Insert the DVD to the computer and change the boot options to boot directly from the DVD.
Install CentOS using this guide as basis. Set the IP address and choose Basic Server as the default installation package as you go.
After CentOS installation is complete, we can check the IP address using the following command
ifconfig
In case the IP address you set was wrong, you can change it by issuing the following command
vi /etc/sysconfig/network-scripts/ifcfg-eth0
Edit the following lines. Use this as a guide.
IPADDR=your.ip.address
NETMASK=the.netmask
GATEWAY=the.default.gateway
DNS1=the.dns
Finally, restart the network
/etc/rc.d/init.d/network restart
chkconfig network on
We will use this tutorial as the basis in installing the rest of the LAMP stack.
Install Apache by issuing the following command (-y to respond yes to all the questions)
yum -y install httpd
Start Apache by doing the following
service httpd restart
To check if Apache is running correctly, open a browser and enter your IP address. Some issues might be encountered when starting Apache. This provided a solution to one of the issues we encountered. Another thing to do is bring down the firewall so that the server can be accessed by anyone in the same network. To bring down the firewall, issue this command
service iptables stop
Install MySQL using the following command
yum -y install mysql-server
Start MySQL by issuing the following
service mysqld start
It is important to secure MySQL. Start configuring this by issuing the following command
/usr/bin/mysql_secure_installation
At first, MySQL root password is blank, so just press Enter on the first question.
Next it will ask for you to set a root password so go ahead and set one.
Lastly, there will be a series of questions to secure MySQL, it is recommended to just answer yes to all the questions.
If you noticed, we disallowed the root to be able to login to MySQL remotely. However, if you need to configure MySQL remotely, you will need to create another user. In creating a new user, follow this example. Issue commands like so
mysql> CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
mysql> CREATE USER 'admin'@'%' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION;
To login to MySQL:
mysql -u root -p
# or
mysql -u admin -p
To exit MySQL:
mysql> EXIT
Install PHP using the following command
yum -y install php php-mysql
The previously linked tutorial also shows other PHP modules that can be installed. Go ahead and install the modules that you need. When we are done with with installing PHP, restart the server by issuing the following command
service httpd restart
To make Apache and MySQL run automatically when the server starts, issue the following commands (PHP starts at the same time as Apache)
chkconfig httpd on
chkconfig mysqld on
To test if our installation is correct, we can make a simple PHP file that we will put in our webroot. The webroot is commonly at /var/www/html
so create an info.php file there
vi /var/www/html/info.php
If the file does not exist, vi will create it for you. Inside the file, enter the following
<?php
phpinfo();
?>
Open your browser, type your IP address, a slash(/) and info.php like: http://your.ip.address/info.php If you are successful, you would see a webpage telling you the current status of your PHP installation
If you need to upload many files to your server, it is important to install FTP. We installed an FTP by following this tutorial. First, lets stop the firewalls. Issue the following commands
service iptables stop
service ip6tables stop
chkconfig iptables off
chkconfig ip6tables off
Install the FTP service by doing the following
yum -y install vsftpd
Start the service and run it automatically on server start
service vsftpd start
chkconfig vsftpd on
Edit the vsftpd.conf file like so
vi /etc/vsftpd/vsftpd.conf
Change the values/uncomment/add the following lines
anonymous_enable=NO
ascii_upload_enable=YES
ascii_download_enable=YES
use_localtime=YES
Restart the FTP service
service vsftpd restart
The root user is not allwed to connect to the FTP server for security purposes. We need to add a new user that we can use for the FTP service. To add a new user to CentOS, we do it like so
useradd admin
passwd admin
You will be asked for the password of the user you are creating, set the password as you see fit. You will be warned for bad passwords that you can ignore, just retype the password that you set. Next is to install FTP itself. Do it by issuing the following
yum -y install ftp
Then we can connect ot the FTP server like so
ftp your.ip.address
We will connect using our IP address. You will be asked for the user to use for connecting to the FTP. Use the user and password that we just created. You will probably receive an error while logging in. The tutorial linked previously explains briefly the cause of this error, a quick fix would be
setenforce 0
# or as the tutorial suggests
setsebool -P ftp_home_dir on
Now we can connect to the FTP using the command line or a client application. But first, we need to change the ownership and the permissions of our web root. We can do it like so
chown -R user html
chmod -R 777 html
There, you have finished setting up your web server!