{"id":1692,"date":"2013-09-01T13:55:26","date_gmt":"2013-09-01T13:55:26","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=1692"},"modified":"2017-12-08T19:42:50","modified_gmt":"2017-12-08T19:42:50","slug":"provisioning-server","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/provisioning-server\/","title":{"rendered":"Setting up a new linux web server"},"content":{"rendered":"<p><strong>Time: 13 minutes<\/strong> &#8211; okay, that&#8217;s after doing this half a dozen times \ud83d\ude09<\/p>\n<p>Setting up a server, also called <em>provisioning<\/em> by the gurus, is an exciting first step after purchasing a new VPS or dedicated machine. It&#8217;s a time when we can experiment, learn and explore all of the latest offerings by the open-source world. It&#8217;s our opportunity to configure the server to our needs without needing to cross our fingers as we make changes, hoping not to disrupt the services currently running on the box.<\/p>\n<p>Depending on the hosting provider you chose, your server will be pre-provisioned to an extent. Some providers will configure a server to your exact specifications, some install the LAMP stack only, while others provide you with nothing but a root password and the ability to SSH in to the machine.<\/p>\n<p>If you are in the last bucket, essentially you have been given keys to a car that has an engine, but no gas pedal, steering wheel or radio. Depending on your point of view, this could been seen as a disadvantage. I encourage you to be more excited about your new skeleton of a server.<\/p>\n<p>Having a server fully provisioned for you is a time-saver, no doubt. But for the most part, you will be getting a server that has a cookie cutter configuration that is not only out of date, but consumes more disk space and memory that you&#8217;ll need.<\/p>\n<p>So let&#8217;s see what it takes to put some meat on that skeleton. It should be noted that the instructions below are geared towards a CentOs distribution. Although, many apply to all distributions.<\/p>\n<h3>Choosing a Server Template<\/h3>\n<p>If you&#8217;re provisioning a VPS or Cloud instance, you might be given the option to apply a &#8220;template&#8221; to the server. This template indicates which distribution to install, but also can offer installation of other software, such as the LAMP stack.<\/p>\n<p>I&#8217;d encourage you to avoid installing anything except for the bare-bones distribution itself. I&#8217;ve struggled to &#8220;clean up&#8221; after these templates have installed packages far too many times.<\/p>\n<h3>Creating a non-root superuser account.<\/h3>\n<p>The very first thing you should do is say goodbye to that root login. Don&#8217;t ever use it again. It&#8217;s not worth ever putting the root password at risk. Okay, but how will I gain admin privileges to the server?<\/p>\n<p><code>useradd <em>myusername<\/em><br \/>\npasswd <em>myusername<\/em>\u00a0\u00a0\u00a0\u00a0\u00a0 (then choose a password)<\/code><\/p>\n<p>Great, you have a new user account. Now let&#8217;s give it superpowers:<\/p>\n<p><code>visudo<\/code><\/p>\n<p>Locate the line that looks like this:<\/p>\n<p>root\u00a0\u00a0\u00a0 ALL=(ALL)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ALL<\/p>\n<p>Then, create another similar looking line directly under it:<\/p>\n<p><code> <em>myusername<\/em> \u00a0\u00a0 ALL=(ALL)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ALL<\/code><\/p>\n<p>What is this program, I can&#8217;t type anything! You&#8217;re using an age-old tool called <em><strong>vi<\/strong><\/em>, it&#8217;s not user-friendly at all, but there are die-hard linux fans that would dive in front of a bullet for it (yes, I have tried to physically harm the software program <strong><em>vi<\/em><\/strong> from time to time).<\/p>\n<p>Press the <kbd>i<\/kbd> key on your keyboard to allow editing.<\/p>\n<p>When you&#8217;re done, press the <kbd>Esc <\/kbd>key, then <kbd>colon<\/kbd>, then the letters <kbd>w<\/kbd>, then q.<\/p>\n<h3>Indexing the filesystem search database<\/h3>\n<p>While you&#8217;re provisioning the server, you&#8217;ll probably find yourself using the <em>locate<\/em> command to find utilities. Depending on your host, they may or may not have setup a cron job to auto-index the search database daily. If not, you&#8217;ll need to do this before issuing your first locate command.<\/p>\n<p><code>updatedb<br \/>\n<\/code><\/p>\n<p>Yup, that&#8217;s it!<\/p>\n<p>Well, unless you don&#8217;t have the mlocate tool installed. Not to worry:<\/p>\n<p><kbd>yum install mlocate<\/kbd><\/p>\n<p><kbd><br \/>\n<\/kbd><\/p>\n<h3>Install the Remi Repo for CentOS 7<\/h3>\n<pre><code>\r\nsudo yum install epel-release\r\nwget http:\/\/rpms.famillecollet.com\/enterprise\/remi-release-7.rpm\r\nrpm -Uvh remi-release-7.rpm\r\nEdit\u00a0\/etc\/yum.repos.d\/remi.repo\u00a0 \u00a0and set enabled = 1\u00a0 \u00a0under the PHP56 repo block.\r\n<\/code><\/pre>\n<h3>Installing your web server, development tools and database<\/h3>\n<p>Otherwise known as the LAMP stack, Apache, MySQL and PHP (or Python\/Perl) are your most common installs for a web server. Being so common, it&#8217;s not a real heavy lift to get these installed.<\/p>\n<p>There are a few ways to install packages, the two most common are:<\/p>\n<ul>\n<li>From source (requires us to compile the source code on our server)<\/li>\n<li>RPM (software comes packaged and installation is managed by the package)<\/li>\n<\/ul>\n<p>I typically choose the RPM method as it&#8217;s works for my needs 90% of the time. Occasionally, you&#8217;ll find yourself compiling from source (it&#8217;s easier than it sounds), but I encourage you to stick with RPM until it&#8217;s no longer sufficient.<\/p>\n<p><em><strong>yum <\/strong><\/em>is an excellent front-end tool that leverages the RPM packaging system. With <em><strong>yum<\/strong><\/em>, you are able to use common names for packages and install them with a single command. Let&#8217;s start with <strong>Apache<\/strong>.<\/p>\n<h3>Installing Apache<\/h3>\n<p><code>yum install httpd httpd-devel<br \/>\nyum install mod_ssl<\/code><\/p>\n<p>It&#8217;s a good ideal to install Apache&#8217;s development tools (httpd-devel), as you&#8217;ll find them necessary when you add modules such as OpenSSL and others. The same goes for other services such as PHP and MySQL; if a development tools library is available, it usually doesn&#8217;t hurt to install it.<\/p>\n<p>You&#8217;ll also notice that we&#8217;ve install mod_ssl. Might as well while we&#8217;re at it.<\/p>\n<p>Now, here&#8217;s the kicker. Depending on your version of CentOS, you might be greeted by a server error when you enter your server&#8217;s IP address in a web browser. The reason is that the default firewall rules are blocking port 80.<\/p>\n<p>No problem, drop in these commands and you should be good to go:<\/p>\n<pre><kbd>iptables -I INPUT -p tcp --dport 80 -j ACCEPT\r\n<\/kbd><kbd>iptables -I INPUT -p tcp --dport 443 -j ACCEPT\r\n<\/kbd><kbd>service iptables save<\/kbd><\/pre>\n<p>If that doesn&#8217;t work, try adding these lines to \u00a0\/etc\/sysconfig\/iptables:<\/p>\n<pre>-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT\r\n-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT<\/pre>\n<p>Directly after<\/p>\n<pre>:OUTPUT ACCEPT [3:440]<\/pre>\n<p>Then running<em> service iptables save<\/em>.<\/p>\n<h3>Installing Development Tools<\/h3>\n<p>At some point, you will need to compile from source, so it makes sense to have the development tools &#8220;suite&#8221; on your machine:<\/p>\n<pre>yum groupinstall \"Development Tools\"<\/pre>\n<h3>Installing PHP<\/h3>\n<p>I like to grab the most recent PHP version. The problem is that most servers, when first provisioned, don&#8217;t know about it. They typically use out-of-date repositiories that identify where these RPM packages are on the web. You may need to Google a bit to find a suitable repository for your linux distribution that has more recent packages. I personally use this one:<\/p>\n<p><code>rpm -Uvh http:\/\/download.fedoraproject.org\/pub\/epel\/6\/x86_64\/epel-release-6-8.noarch.rpm<br \/>\n<\/code><\/p>\n<p><code>yum --enablerepo=remi install php php-common php-devel<br \/>\n<\/code><\/p>\n<p>You may discover that you don&#8217;t have the remi repos on your system. In that case, for CentOs 6:<\/p>\n<pre>wget http:\/\/rpms.famillecollet.com\/enterprise\/remi-release-6.rpm\r\nsudo rpm -Uvh remi-release-6*.rpm<\/pre>\n<p>&nbsp;<\/p>\n<h3>Installing MySQL<\/h3>\n<p><code>yum --enablerepo=remi,remi-test install\u00a0 mysql mysql-server mysql-devel<br \/>\n<\/code><\/p>\n<p><strong>Update for CentOS 7<\/strong><\/p>\n<p>CentOs 7 will try to install something called MariaDB, which is apparently a drop-in replacement for MySQL. Avoid this by installing the MySQL Community repo first.<\/p>\n<pre><code>rpm -Uvh http:\/\/dev.mysql.com\/get\/mysql-community-release-el7-5.noarch.rpm\r\nyum install mysql mysql-server mysql-devel<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>Installing PHP Modules<\/h3>\n<p>Now that MySQL is installed, let&#8217;s install the PHP modules necessary to connect to MySQL. While we&#8217;re at it, we should snag as many other useful modules as possible<\/p>\n<pre><code>yum --enablerepo=remi install php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache \r\nphp-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-pecl-apc php-cli php-pear php-pdo<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Installing nano<\/h3>\n<p>As I mentioned before, unless you&#8217;re a die-hard, you won&#8217;t be happy with the vi text editor that comes installed on the server. nano is a step up, something you might more closely equate with Windows Notepad.<\/p>\n<p><code>yum install nano<br \/>\n<\/code><\/p>\n<p>&nbsp;<\/p>\n<h3>Verifying your mail sender<\/h3>\n<p>It&#8217;s likely that you distribution will include <em>sendmail<\/em> by default, but it&#8217;s possible that it won&#8217;t. In some cases, by doing:<\/p>\n<p><code>locate sendmail<\/code><\/p>\n<p>It appeared to me that sendmail was actually installed. But when I tried to test it (as shown below), it returned errors. Sendmail wasn&#8217;t actually installed.<\/p>\n<p>If this happens, you can easily install it with <em>yum<\/em>.<\/p>\n<p><code>yum install sendmail<\/code><\/p>\n<p>Then, verify it&#8217;s working using this <a href=\"http:\/\/www.sitekickr.com\/snippets\/linux\/sendmail-test-command-line\">sendmail test snippet<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<h3>Fire it up!<\/h3>\n<p>Now that you have everything installed, it&#8217;s time to start Apache and MySQL.<\/p>\n<p>You can call these programs directly:<\/p>\n<pre><code>\/etc\/init.d\/httpd start\r\n\/etc\/init.d\/mysqld start<\/code><\/pre>\n<p>Or, you can use the service command to start them. service is basically a convenience utility to start, stop or restart any daemon located in the \/etc\/init.d directory.<\/p>\n<pre><code>service httpd start\r\nservice mysqld start\r\n<\/code><\/pre>\n<h3>Make &#8217;em last forever<\/h3>\n<p>Once you start your services, you&#8217;re good to go. For now. But what happens when you need to restart your server (or it restarts automatically for some unforeseen reason, like a power outage)?<\/p>\n<p>Thankfully, the <em>chkconfig<\/em> command has been provided to use to manage our starting lineup. To ensure that both Apache and MySQL start automatically on boot, issue the following two commands:<\/p>\n<pre>chkconfig httpd on\r\nchkconfig mysqld on<\/pre>\n<p><b>On CentOS 7<\/b><\/p>\n<pre>systemctl enable httpd\r\nsystemctl enable\u00a0mysqld<\/pre>\n<h3>Configuring Apache<\/h3>\n<p><code>nano \/etc\/httpd\/conf\/httpd.conf<br \/>\n<\/code><\/p>\n<p>Uncomment this line:<\/p>\n<p><code>#NameVirtualHost *:80<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Then, add any VirtualHost stanzas required, either directly in httpd.conf, or preferably within a separate file. Many use the convention of having a separate file for virtual host directives, called vhosts.conf<\/p>\n<p><code>\/etc\/httpd\/conf.d\/vhosts.conf<br \/>\n<\/code><\/p>\n<p>After you&#8217;re done setting up your virtual hosts, restart apache.<\/p>\n<p><code>service httpd graceful<br \/>\n<\/code><\/p>\n<p>By using the <em>graceful <\/em>argument, you&#8217;re telling Apache not restart in the middle of a request. Of course, when first provisioning your server, it&#8217;s not likely that you&#8217;re getting production requests from your web site. But, it&#8217;s a good idea to get in the habit of gracefully restarting your web server.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>UPDATE 12\/28\/2015<\/strong><\/p>\n<p>I&#8217;ve found that some of my CentOs servers tend to fall out of sync with time. The NTP service can be used to keep the server&#8217;s time in sync against a remote time service.<\/p>\n<pre><code>#install ntp\r\nsudo yum install ntp\r\n\r\n# make sure it's starts up after reboot\r\nsudo \/sbin\/chkconfig ntpd on\r\n\r\n# start the ntp service now\r\nsudo \/etc\/init.d\/ntpd start<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Setting up a new server isn&#8217;t something we do often enough to recall every step in detail. This reference should help serve as a guide when it comes time to provision a brand new linux server.<\/p>\n","protected":false},"author":1,"featured_media":1699,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[41],"tags":[261,141],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1692"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=1692"}],"version-history":[{"count":25,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1692\/revisions"}],"predecessor-version":[{"id":2552,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1692\/revisions\/2552"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media\/1699"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=1692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=1692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=1692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}