So I need to setup a staging environment. I’m doing it on CentOS using the command line. This means I have a SSH connection to my server and have sudo privileges. But before that let’s begin by settings up a DNS entry in a control panel – if you don’t have that then you need to contact your web host.
This is what we want to create: a subdomain called “thestage” which will make the URL for access “thestage.yourlocation.com”. 300 is the TTL(time to live) and I’m not sure whether it’s right. Since it’s inaccessible to the public I’m going to experiment with that value for a little while.
If it’s not an IP address then you need a full stop at the end.
I created a directory on this server for the staging contents. This will make it a top level directory like /var /etc /usr etc. It felt appropriate.
I setup a vhosts (virtual hosts) conf file. This essentially means we can relay different addresses through the httpd (HTTP daemon) like Apache to different folders. This is how a lot of web hosting is conducted. So if you have access to vhosts then you can do this, if you don’t you’re probably on a hosting package that limits your ability to do things like this. Talk to your hosting provider and probably don’t continue reading the rest of this until you can do the aforementioned.
Because my httpd.conf (httpd configuration file) includes all ‘.conf’ (configuration) files in a directory of vhosts configuration files I can make a single instance specific/ bespoke version by creating a new virtual host configuration file (see below). If I didn’t/ couldn’t do this I would have to include it in the httpd.conf file which is possible but can get messy if you have a lot of information and a lot of virtual hosts. The httpd.conf file is probably somewhere like /etc/httpd/conf/httpd.conf. (if you can’t find it do this: “cd /” … “locate httpd.conf”. Instead I put one here /etc/httpd/vhosts.d/thestage.conf where “thestage.conf” is the name of the file and vhosts.d is a directory being included because of the following line in httpd.conf:
I put this inside it to just test everything works first off:
# Other directives here
Since we’ve made a change to httpd we need to restart it
If your user has it setup – not guaranteed (sorry, look elsewhere for that) – then use this:
service httpd restart
Otherwise (or if it doesn’t work) use the file path too:
now go to thestage.yourlocation.com and you should see the default Apache page.
So go to /thestage and create index.html (this is the command line way, you could just use a program like WinSCP to create it and edit it in a GUI).
Then open that file to add contents
sudo vi index.html
(assuming you can and do use sudo, not discussed here)
Press [i] to enter “insert” module
Write anything, e.g.
Press [Esc] to enter command mode
Type (they’re capitals btw):
and press enter, this saves and quits
Refresh your browser and you should then see:
So since that works we can get on with password protecting thestage.yourlocation.com. Since it’s a staging site it’s essentially still testing and we don’t want the world seeing it!
So the password advice in this Apache link didn’t work when I first tried it presumably because the path or something wasn’t set for me to use it so I had to do use the following command: locate htpasswd
# locate htpasswd
The one I wanted is /usr/bin/htpasswd
So being in the directory with the program:
we will almost be ready to use the command from the Apache link above. I have decided to not use /etc/.htpasswd so I will make a new file in my directory
If you want to put it somewhere else which doesn’t yet exist then you will need to use mkdir with the -p argument to make the parent directories. For example:
# mkdir -p /usr/local/apache/passwd
But, still being in the /usr/bin directory with the htpasswd program (enter the command “pwd” if you’re not sure if you’re in that dir) type the following to create the new .htpasswd file and add a password to be used with yourusername (your user name) as below. If you’ve already got a .htpasswd file as in /etc/.htpasswd then you don’t need the -c argument.
htpasswd -c /usr/local/apache/passwd/.htpasswd yourusername
Then you’ll need to enter and re-enter your password. This should work, if it doesn’t you might want to try another directory.
Then you’ll need to change the file ownership:
chown apache:apache /usr/local/apache/passwd/.htpasswd
Then create a .htaccess file in your your directory /thestage and add the following lines:
AuthName "Enter password"
Or wherever your file is in place of “/etc/httpd/.htpasswd” Save it and restart apache (httpd) again as before.
Refresh your browser and now you should have a login form.
Now, depending on your needs you have the ability to setup a new database (a staging version of your live one) and a new codebase (perhaps with Git, I’ll come to that later, if I go down that road…)