15
Feb
2019

Setup a Virtual Machine with CentOS 7 & LEMP Stack

Download iso file

1. Setting Up Virtual Machine

Open Virtual Box
Click on New
Select Guided Mode and adjust Settings as below for each screen

Name and Operating System Screen

  • Give Virtual Machine a Name – “CentOS7”
  • Type set to Linux
  • Version RedHat(64-bit)

Memory Size Screen

  • Set memory to 1024mb

Hard Disk Screen

  • Select Create a virtual hard disk now

Create Virtual Hard Disk Screen

  • Choose VDI(Virtual Box Image) and click next
  • Storage on physical hard disk – Fixed Size
  • Choose location to store .vdi
  • Set the size of the virtual hard drive to 8gb

2. Attaching .iso to CentOS7 VM

  • Select “CentOS7” VM and click on Settings
  • Click on Storage.
  • Under Storage Tree click on Empty
  • Click on the cd icon under Attributes > Optical Drive and select “Choose Virtual Optical Disk File”
  • Browse to “CentOS-7-x86_64-DVD-1511.iso” and select.
  • Click Okay

3. Installing CentOS7 on Virtual Machine

  • Select CentOS7 machine and click Start
  • Hit Enter to start install once VM has powered up
  • Once installed the Welcome Screen for CentOS7 should appear
  • Select language
  • Start Date and Time
  • Check installation Destination – Leave at Default
  • Select Begin Installation
  • Set Root password on the User settings screen (Click Done twice to confirm weak password)
  • Once Installed click on Reboot
  • Once reboot has completed login with root user and pass

4. Internet connection configuration

Identify ethernet card for the machine

nmcli d

Open configuration file for Device: enp0s3

vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
  • Check BOOTPROTO is set to “dhcp”
  • Change ONBOOT to “yes”
  • Hit escape and type :wq to save changes
  • Restart network
service network restart

Check there is an IP on the network

ip a

Should return an IP like 10.0.2.15

5. Set Static IP

Find Your IP address

ip address show

Find GATEWAY

ip route show

Open configuration file command:

vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
  • Change BOOTPROTO=dchp to BOOTPROTO=static
  • Add IPADDR=10.0.2.15
  • Add GATEWAY=10.0.2.2
  • Add NETMASK=255.255.255.0
  • Add DNS1=8.8.8.8

6. Changing Host Name

Open Hostname file vi /etc/hostname and remove localhost.localdomain. Give host a new name eg tutza

Open Hosts file vi /etc/hostname add a new line : 127.0.0.1 “HOSTNAME” eg tutza

7. Accessing SSH

Open port 22

firewall-cmd --zone=public --add-port=22/tcp --permanent

The status bar, on the bottom of the running vm, should have a bunch of icons. The one of interest to us is the network (double monitor) icon.
The dialog that opens shows the current configuration of the network adapter. Next press the Port Forwarding button to bring up the settings that will allow us to ssh into the VM. (Advanced Settings)
I use the following settings:

  • Host IP: 127.0.0.1 – this is the IP address (localhost) of the host operating system (not the VM). If you want to access the VM from anywhere on the internet you’ll need to use your computers real IP.
  • Host Port: 2222 – use a port that is open on your machine.
  • Guest IP – this is the IP address shown when ip addr is typed in the VM command-line. In my case it has been 10.0.2.15 every time I have only one VM running.
  • Guest Port – 22 is the default ssh port

8. Packages

wget

yum install wget

unzip

yum install unzip

epel

yum install epel-release

remi

wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
sudo rpm -Uvh remi-release-7*.rpm

enable repo

sudo yum --enablerepo=remi install php-tcpdf

permanently enable the Remi repository

vi /etc/yum.repos.d/remi.repo

Edit the [remi] portion of the file to set the enabled option to 1

nginx

yum install nginx

Start nginx

systemctl start nginx

Enable nginx on Boot

systemctl enable nginx

mariadb

yum install mariadb-server mariadb

Activate MySQL

systemctl start mariadb.service

Set MySQL to start on server boot

systemctl enable mariadb.service

Configure your MySQL installation

/usr/bin/mysql_secure_installation

Restart MySQL

systemctl restart mariadb.service

PHP7

yum install php php-mysql php-fpm
vi /etc/php.ini

Find the cgi.fix_pathinfo directive, uncomment it by removing the # and set it to 0

cgi.fix_pathinfo=0

Save and close file

vim /etc/php-fpm.d/www.conf

Find the listen directive (it should be the first), and verify that it is set to listen for PHP traffic using a Unix socket (instead of port 9000)

listen = /run/php-fpm/php-fpm.sock

Find the listen.owner and listen.group directives, uncomment them, and modify them as follows

listen.owner = nginx
listen.group = nginx

Find the Unix user/group of processes section, and change the user and group from apache to nginx

user = nginx
group = nginx

Save, close and restart php-fpm

systemctl restart php-fpm

Change permissions and ownership on php-fpm.sock

chmod 666 /run/php-fpm/php-fpm.sock
sudo chown nginx:nginx /run/php-fpm/php-fpm.sock

Configure NGINX

vi /etc/nginx/nginx.conf

Ensure the following line is in the server context:

include /etc/nginx/default.d/*.conf;

Save and close

Create a new default file in

/etc/nginx/default.d/default.conf
vi /etc/nginx/default.d/default.conf

Paste following code in

index index.php index.html index.htm;
    server_name your domain name or IP;

# pass the PHP scripts to FastCGI server listening on the php-fpm socket
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME document_root$fastcgi_script_name;
    include fastcgi_params;
}

Save, close and restart PHP

systemctl restart nginx

Verify PHP is working by creating a file

vim /usr/share/nginx/html/info.php

Add the following

<?php phpinfo(); ?>

Browse to http://your server’s IP address/info.php

Helpful Links

(i) centos 7 – virtualbox – install centos 7 on virtualbox

(ii) Internet connection configuration

(iii) Installing LEMP Stack

Share

You may also like...