Sunday, July 2, 2017

Install Apache

This post is one of a series

Create an environment for Web development
  1. Vagrant/Virtualbox
  2. Install Apache or Nginx for PHP or Nginx for Python - You are here
  3. Install MySQL OR Install MariaDB
  4. Install PHP / Install Python

Install Apache

If you choose to use Apache for your project, we will install Apache. Apache is a software used to serve files. Now we have a virtual machine (Cent OS 7) but we need to make it a server (that works only locally for development), so we will install Apache in the virtual machine.
You can see Apache (httpd) is already available from the default repository:
$ sudo yum info httpd

Install it:
$ sudo yum -y install httpd

Start Apache

We will start Apache (that is also called httpd in Linux) by the following command:
$ sudo systemctl start httpd.service
If this doesn't work, maybe the OS doesn't recognize "systemctl" as a command. Such commands are inside /sbin, so do the following command instead:
$ sudo /sbin/systemctl start httpd.service
To start Apache automatically when the virtual machine is started:
$ sudo systemctl enable httpd.service
To check the status of Apache (httpd):
$ sudo systemctl status httpd.service
You will be asked password to start httpd. The password is "vagrant". To stop Apache (httpd):
$ sudo systemctl stop httpd.service
To re-start Apache (httpd):
$ sudo systemctl restart httpd.service

FYI commands for CentOS6

(If you use CentOS6 or older, use these commands instead. We are not using CentOS6 here, so we don't need them now though. Just for your information.)
$ sudo service httpd start
$ sudo /sbin/service httpd start
$ sudo service httpd status
$ sudo service httpd stop
$ sudo service httpd restart
$ sudo chkconfig httpd on

SELinux

SELinux interrupts Apache for security reasons. You can authorize the Apache to work:
$ sudo yum -y install policycoreutils-python
$ sudo semanage permissive -a httpd_t
But I think it is better to disable SELinux permanently.
$ sudo setenforce 0
And open the "/etc/selinux/config" file and set the SELinux to disabled:
$ sudo vi /etc/selinux/config
And change it like this:
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.

SELINUX=disabled #Change HERE!!!!!

# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
How to use vi editor is here:
Press a on your keyboard to be insert mode and you can edit the file. Press escape key to stop the insert mode. After stopping the insert mode, press Shift + g to go to the lowest row. :wq or Shift + zz to save and close the file. You can go to command mode by pressing : on your keyboard. To exit the file without saving the file, inout :q! and enter. To search a word, press ? then write a word what you want to search. For example, if you write ?aaa and press enter, "aaa" is searched and highlighted in the file. Press n to jump to next match.)

Check if the server is working

We will access to the following link with your browser: http://192.168.33.10/ (Any browser is OK)
If your virtual machine and Apache are working correctly, the following should be displayed in the browser:


This can not be accessed from outside. This is a private network (as defined in Vagrantfile), so this can be accessed only from your PC.

(By the way, this can be seen only from your host computer. But if you choose "public network (sharing virtual machine in a local network)" in Vagrantfile, everybody in your LAN network can access to this link.

Symbolic link

We will make "Symbolic link" because it is useful. If we make symbolic link from /vagrant to /var/www/html, everything inside the share folder (/vagrant) is instantly copied to the Apache folder (/var/www/html) so that anything updated in the share folder is also instantly updated in the Apache folder too.
(You can also define another sync folder in the Vagrantfile to sync between the folder in the host and the httpd folder in the guest. I am lazy, so I just use this symlink.)
Do these commands:
$ sudo rm -rf /var/www/html
$ sudo ln -fs /vagrant /var/www/html
We will check if symbolic link is correctly created. Make a test.txt in the sync folder and run this in the virtual machine:
$ ls /var/www/html



If all the files are synced, you made it successfully. If not, something is wrong.

And we will install vim (an editor) which would be useful to edit files:
$ sudo yum install vim-enhanced