• Installing Apache on CentOS/Ubuntu

An article on various Web servers.


Installing Apache on CentOS

Setting up Apache is pretty quick and straightforward, but before we begin you should update your installation. Now go ahead and install Apache with the following command:

sudo yum install httpd

Start the apache service:

sudo service httpd start

To start apache on boot enter the following:

/sbin/chkconfig --levels 235 httpd on 
Configuring Name-Based Virtual Hosts

If you plan on having multiple domains or sites you will need to set up Virtual hosts.

Edit the file /etc/httpd/conf.d/vhost.conf and modify it suiting your needs

NameVirtualHost *:80  
<VirtualHost *:80>       
ServerAdmin webmaster@example.com      
ServerName example.com      
ServerAlias www.example.com      
DocumentRoot /srv/www/example.com/public_html/      
ErrorLog /srv/www/example.com/logs/error.log       
CustomLog /srv/www/example.com/logs/access.log combined   
<VirtualHost *:80>       
ServerAdmin webmaster@example.org           
ServerName example.org      
ServerAlias www.example.org      
DocumentRoot /srv/www/example.org/public_html/      
ErrorLog /srv/www/example.org/logs/error.log       
CustomLog /srv/www/example.org/logs/access.log combined </VirtualHost>

Important lines here are:

ServerAlias www.example.com 
DocumentRoot /srv/www/example.com/public_html/

Server alias is the domain name you want to use. DocumentRoot is the location where your site files are stored.

Once you have made your changes you will need to restart your server to apply them.

service httpd restart

Installing Apache on Ubuntu

At a terminal prompt enter the following command:

sudo apt-get install apache2

Apache2 is configured by placing directives in plain text configuration files. These directives are separated between the following files and directories:

httpd.conf: historically the main Apache2 configuration file, named after the httpd daemon. Now the file does not exist. In older versions of Ubuntu the file might be present, but empty, as all configuration options have been moved to the below referenced directories.

conf-available: this directory contains available configuration files. All files that were previously in /etc/apache2/conf.d should be moved to /etc/apache2/conf-available.

conf-enabled: holds symlinks to the files in /etc/apache2/conf-available.When a configuration file is symlinked, it will be enabled the next time apache2 is restarted.

envvars: file where Apache2 environment variables are set. mods-available: this directory contains configuration files to both load modules and configure them. Not all modules will have specific configuration files, however.

mods-enabled: holds symlinks to the files in /etc/apache2/mods-available.When a module configuration file is symlinked it will be enabled the next time apache2 is restarted.

ports.conf: houses the directives that determine which TCP ports Apache2 is listening on. sites-available: this directory has configuration files for Apache2 Virtual Hosts. Virtual Hosts allow Apache2 to be configured for multiple sites that have separate configurations.

sites-enabled: like mods-enabled, sites-enabled contains symlinks to the /etc/apache2/sites-available directory. Similarly when a configuration file in sites-available is symlinked, the site configured by it will be active once Apache2 is restarted. magic: instructions for determining MIME type based on the first few bytes of a file.


Installing Nginx on CentOS

Step One - Enable EPEL repository

sudo yum install epel-release

Step Two - Install Nginx

sudo yum install nginx

Step Three - Start Nginx

sudo /etc/init.d/nginx start

Installing Nginx on Ubuntu

Step One - Installing Dependencies

The packages that you will need to install are python-software-properties and software-properties-common (which is only necessary if you are running Ubuntu 12.10).

To install the first package dependency, python-software-properties, you will need to run the following command:

 sudo apt-get install python-software-properties

If you are on Ubuntu 12.10, you should run the following command to install software-properties-common, which is another package that is necessary (without it, the add-apt-repository command used in Step Two will not be found).

sudo apt-get install software-properties-common

Step Two - Adding the Stable Nginx Repository

To ensure that our web server software is secure to run on a production server, we will be using the latest 'stable' release.

If you are developing a nginx module, or if you need to use the "bleeding edge" version, you can replace the 'stable' version with the 'development' version. However I would not recommend doing this on a production server, as there may be bugs.

Now that we have the latest stable package installed, we can now add the repository to install the latest version of nginx:

sudo add-apt-repository ppa:nginx/stable

Note: If this command still does not work (normally on 12.10), run the following command:

sudo apt-get install software-properties-common

This will add the repository to Ubuntu and fetches the repository's key. This is to verify that the packages have not been interfered with since they have been built.

Step Three - Updating the Repositories

After adding a new repository, you will need to update the list:

sudo apt-get update

Step Four - Install nginx

To install nginx or update the version you already have installed, run the following command:

sudo apt-get install nginx

Step Five - Check That Nginx is Running

You can check to see that nginx is running by either going to your cloud server's IP address/domain, or typing in:

service nginx status

This will tell you whether nginx is currently running. (Step Six - if Nginx is Not Running)

If nginx is not running correctly, and/or prints out an error e.g. nginx: [emerg] bind() to [::]:80 failed (98: Address already in use), you can run:

netstat -tulpn

This will list all processes listening on ports. The PID, is the number that you will use to kill the process. In this case, you would need to run kill -9 734. However the general code to copy into your terminal would be:

kill -9 xxxx

The phrase, "xxxx", is the PID of the process you want to kill. After killing the process, you can try restarting nginx again by running:

service nginx start

Alternatively, the issue may be caused by the configuration accepting connections from both ipv4 and ipv6. In order to resolve this, edit out "listen [::]:80" in your default config file (/etc/nginx/sites-available/default) and any other server block config files that are in use.

sudo nano /etc/nginx/sites-available/default

The lines should look like this:

server {
        listen 80;
        #listen [::]:80 default_server;


Congratulations! You have now installed nginx!

This article was last modified: March 11, 2016, 8:12 p.m.


Please log in to leave a comment.

Add or change tags.

A comma-separated list of tags.


Hacker News