Linux Part 1
NOTE
As I have said above, we should be using an up-to-date version of our preferred OS
either x.04 or x.10. I have tested this out on Ubuntu 15.10
, PHP 7
no longer has a valid regex while installing its repo. This will no longer work for Ubuntu 15.10
and maybe Ubuntu 15.04
, Ubuntu 14.04
, and Ubuntu 14.10
. Again, please use Ubuntu 16.04
or Ubuntu 16.10
for this tutorial.
Linux
(Ubuntu 16.04
)
Prepping your system
1.) Update your system by typing in the command line
sudo apt-get -y update
The -y
flag simply just puts “yes” in the command so that we don’t get prompt with questions. Without the -y
flag, Linux
will sometimes ask you if you want to continue with the installation and it will also halt until you type in y
(yes) or n
(no) in the command line. Typing our letter beforehand will avoid the questions and it’ll get us to where we actually want to go (updating). Updating your system basically just updates whatever changes you have made to your system such as adding a repository or creating aliases in your .bashrc
file and such.
2.) Once you have updated your system, you can either do an optional upgrade or you can skip this step and go to step 3. For those who want to do the upgrade, follow this step and type in the command line
sudo apt-get -y upgrade
This one however, might actually prompt you a message saying that something failed and for you to use --force-yes
instead of -y
. If it does prompt you that message, just type in the command line
sudo apt-get --force-yes upgrade
Once it upgrades, you might also have to type in another command to remove all the packages that aren’t needed anymore. You can just type in the command line
sudo apt-get -y autoremove
If this one prompts you a same message saying something failed, just replace -y
with --force-yes
again.
All we’re basically doing is upgrading packages that are on older versions that might have newer versions available and remove old packages that aren’t needed anymore for your current version of Ubuntu
.
3.) Let’s configure our domain names first. If you wish to have a local domain name cut off from the outside world and something that you can only see, you can modify your hosts
file. I prefer using localhost.com
instead of localhost
. Sometimes, certain softwares would like for you to have an actual domain name, not a localhost one. So I just use localhost.com
. This domain name actually exists. At one point, it pointed towards one of Google
’s DNS
servers. No one really knows who the registrant is, but as of today, localhost.com
actually does not work anymore. It points to a blank DNS
server so we’re just going to use it for our personal use. We’re not really stealing the domain name since we aren’t actually owning that domain name. All we’re going to do is point that domain name towards our local IP Address
. This in turn turns a real domain name into a local domain name.
So open up a file explorer and browse to the folder /etc/
and browse way down. You’ll see the file hosts
. It’s not a folder, it’s a file. Open it with your favorite Text Editor
or gedit
. Once the hosts
file is opened, you’ll see 2 lines, one pointing to 127.0.0.1
and the other pointing to 127.0.1.1
. The 127.0.0.1 is basically our local
IP Address. Any domain name you want to use as a local domain name can be pointed here. Also, when opening the
hostsfile, make sure you have read, write, and execute permission on that file or when using your favorite
Text Editor. If you are using
gedit`, you might want to open a new terminal and typing in
sudo gedit
Reason why you need to do this is because when using gedit
, you will be using the read-only
version. You don’t have root permission to write to the hosts
file. So in order to get root permission to edit any file using gedit
, you’ll need to do it via the terminal. From there, a new gedit
will open up with the correct permissions. You’ll also need to re-browse back to the /etc/
folder and look for hosts
again.
4.) Add in your desired domain names into the hosts
file and save the file. For me, it’s going to be localhost.com
as the main domain; phpmyadmin.localhost.com
, sample.localhost.com
, and test.localhost.com
as the sub-domains. As their names say in the domain. phpmyadmin.localhost.com
will be used for phpMyAdmin
, sample.localhost.com
will be used for our samples or basically to make sure that the sub-domains work. And test.localhost.com
is basically for our test projects. Your hosts
file should look something similar to the below picture. Save the hosts
file to /etc/
and close it out.
Installing Apache
1.) Now, once we have our desired domain names you want to install Apache
. So let’s do that. Type in the command line
sudo apt-get -y install apache2
After you have successfully installed Apache
, let’s test it out to make sure it works. Well, just the main domain since all of the sub-domains will most likely point back to the main domain since we haven’t actually configured them to point to their own respective folders. So open up your favorite web browser and type in your local domain you added to the hosts
file. Mine is localhost.com
. If you did everything correctly, your web page should look something similar to the picture below.
2.) Before we start, I would like to also point out that we are re-opening our current terminal and then opening up another new one. Basically, we don’t want to lose our work so we open up a new terminal on the side and use that terminal as a side terminal so we can do 2 things at once (not recommended, but if you want to follow along, then do so, if not, ignore the first few pictures below). Next, we need to modify the 000-default.conf
file. This will allow us to point our domains to their respective folders. So using gedit
(with root permissions) again, browse to /etc/apache2/sites-available/
and open up 000-default.conf
. There should be an entry there already by default. Let’s modify it since that’ll be our main domain entry. The ServerName
line has been commented out. Let’s uncomment it and replace www.example.com
with www.localhost.com
(or the main domain you added to the hosts
file). For me though, I prefer not to use www.
as it is pretty annoying to me. So I’ll just leave that out, you don’t have to on your end. The lines will now read ServerName localhost.com
as shown below (or something different on your end depending on your local domain name).
You don’t have to add ServerAlias
to this file, but I like to anyways. Again, I don’t like adding www.
to my domains. So I’ll exempt that. Let’s continue. So next, let’s change webmaster@localhost
to webmaster@your-domain-here
. Mine will be webmaster@localhost.com
. These don’t really matter because these will only be shown to us if we get an error 500
for mis-configuring our .htaccess
file or our 000-default.conf
file. If you understand Apache
well, I honestly don’t think the email is really needed. The email won’t really exist since we aren’t actually going to be creating a local mail server. We’re just installing PHP
, Apache
, MySQL
, and optionally phpMyAdmin
. So even though you change the email, you’re still the only one who knows about your configurations unless you point that email to an existing email that is owned by an IT personal who will be willing to help you. Other than that, I see no real use for the email
.
3.) Now, this is important so it gets its own steps. You must point the directory to your desired location. For me personally, I like to separate my projects so the way I do it is creating directories based on their domains or sub-domains. If it’s the main domain, I create a directory called default
. The reason why we do this is so that we don’t have all our projects bunched into 1 single directory which will cause a lot of confusion and a messy workspace. So we basically separate those projects into their own directories so that we can work with 1 project without looking for the files in /var/www/html
while having a main domain pointing to /var/www/html
.
So our main domain is first, we will type in
/var/www/html/default
Now, you’ll have to do the same thing for all your sub-domains. This is simple. Just copy&paste and then replace or add a .localhost.com
in front of the domains like below. (phpmyadmin.localhost.com
will be pointed to /usr/share/phpMyAdmin
because that is where we will be installing it to. We may also need to modify this part later on to have phpmyadmin.localhost.com
working.)
4.) Now restart Apache
by typing into the command line
sudo systemctl restart apache2
5.) Create directories in /var/www/html
based on the domain names and test to see if the sub-domains point to their own respective directories. If the sub-domain points to a 404 error
page or you can’t create a new directory under /var/www/html
, then you’ll most likely have to change the permissions on /var/www/
. I like to change the permissions on /var/www/
and all of its sub-directories and files to 0777
. This is because it’ll give us read, write, and execute permission for the author, the server, and other groups. But since we are doing this locally, you don’t have to worry about it. If you were doing it on a live server, I’d suggest changing permissions to 0755
which means; read, write, and execute for the author. Read-execute for both server and other groups. But since we are doing this locally and no one can actually touch our files unless they are on our network, it should be fine using 0777
. You have to also grant this permission on all new directories that are created within /var/www/
after the 0777
permission has been put in place. So let’s do just that. Type this into the command line
sudo chmod -R 0777 /var/www/
The -R
flag means recursively which means repeat the same action for all sub-directories and files under /var/www/
. /var/www/
also gets 0777
as well since it’s the parent directory. If you couldn’t create new directories or files before, now is your chance. Let’s create the respective directories for our sub-domains. We will be excluding phpmyadmin
as it will reside in /usr/share/
.
6.) Once you have successfully set the permission for /var/www/
to 0777
, you can then create your default
directory and other directories as shown in the below pictures.
7.) Let’s move the old Apache
index file to the sample
folder. This in turn will give sample.localhost.com
the old Apache
index file and we will most likely create something new for the main domain.
OPTIONAL
1.) You really don’t have to do this, but I really like to hit that update button (in this case, update line). So I typically update every time I install or add something new to my system. Again, this is optional, but if you really want to follow a long, you can update your system if you want. Again, the command line is
sudo apt-get -y update
Installing PHP
1.) The first thing you’ll need to do is add a repository to your system. PHP 7
does NOT come default with your system or any new system. So you CANNOT just type in
sudo apt-get -y install php7
And having it work. That’s NOT how it works. You have to add the repository to your system, update your system and then you can use a command similar to the one above.
So let’s do that. Type into your command line
sudo add-apt-repository ppa:ondrej/php
This will grab the repository for PHP 7
. If it prompts you to hit the “enter” key, do so.
2.) Next, let’s update our system. Without updating our system, like I mentioned in the first few steps under Prepping your system. You need to update your system in order for the repository to be updated and added to your system list. Without it being in your system, you cannot run the installation command on it. So let’s update our system by typing in
sudo apt-get -y update
Again, you should know what the -y
flag stands for. We’ve used it multiple times above. So let’s not explain anymore. This one however is required if you want to successfully install PHP
.
3.) You can finally now install PHP 7
as it is now available (we will be installing PHP 7.0
instead of PHP 7.1
because I made the mistake and typed in .0
instead of .1
, but you can install PHP 7.1
if you want.). So in the command line, type in
For PHP 7.0
sudo apt-get -y install php7.0
For PHP 7.1
sudo apt-get -y install php7.1
4.) Next, we will install the mysqlnd
extension. So we’ll need to type in
For PHP 7.0
sudo apt-get -y install php7.0-mysql
For PHP 7.1
sudo apt-get -y install php7.1-mysql
5.) We will now install mbstring
as it is needed for PHPMyAdmin
to work. So type in the command line
For PHP 7.0
sudo apt-get -y install php7.0-mbstring
For PHP 7.1
sudo apt-get -y install php7.1-mbstring
6.) Let’s install cURL
now. If you don’t want this package, you can skip this step. This is for those who want cURL
. Type in the command line
For PHP 7.0
sudo apt-get -y install php7.0-curl
For PHP 7.1
sudo apt-get -y install php7.1-curl
Update Your System
1.) We have probably went through 4 or 5 updates, but we must update if we want our PHP
environment to work. Some updates are optional, but some aren’t.
sudo apt-get -y update