Company dedicated to web development, graphic design, photography and web hosting.

How to install a web server
Published Sunday, 22nd of April 2012
Overview How to install your own web server. In this entry I show how to install and configure a full web server. I will install apache http server in combination with php; a mysql database server, vsftp as ftp server and dovecot and sendmail as mail servers. The installation was performed on CentOS 6.2 64 bits via command line.Alias table
VariableExampleDescription
${HTTP_PORT}80The port used for http server
${MYSQL_PORT}3306The port used for mysql database server
${FTP_PORT}21The port used for ftp server
${PASV_MIN_PORT}50000The minimum port used for passive ftp connections
${PASV_MAX_PORT}50100The maximum port used for passive ftp connections
${IMAP_PORT}143The port used for IMAP mail server
${POP3_PORT}110The port used for POP3 mail server
${SMTP_PORT}25The port used for SMTP mail server
Steps to follow Net configuration
  1. First at all, we need internet access, so configure the right parameters to your net in the file:
    • /etc/sysconfig/network-scripts/ifcfg-eth0
    If you have your net over other interface and not over eth0, replace the right file.
Http server installation
  1. To install and configure a http server, we will install the apache httpd server (I have installed 2.2.15 version):
    yum install httpd
    Maybe some dependencies are required to be installed also with this package.

  2. To configure the instance we need to modify the file:
    • /etc/httpd/conf/httpd.conf
    Be sure you configure at least your port (usually 80), the DocumentRoot (pointing to the folder where the server will be served) and the permissions for such folder. It usually have the group apache and user nobody to be served.

  3. And just to be sure that the daemon is launched every time that we restart the server, fix it into the level 3:
    chkconfig --levels 3 httpd on
  4. We need also to open this port from the firewall, we do that with the command:
    iptables -I INPUT -p tcp -m tcp --dport ${HTTP_PORT} -j ACCEPT
  5. After any changes in the iptables service we need to save the changes, use the next command:
    service iptables save
PHP installation
  1. Install php (5.3.3 version currently available via yum in CentOS 6.2):
    yum install php
    Maybe dependencies can be required to be installed with this package.

  2. To configure the php environment we need to modify the file:
    • /etc/php.ini
    Also notice that the httpd.conf file has been modified, new lines has been added, for example loading a module for php5. Please keep them to run php. Also add, if it's not added, some lines to tell httpd daemon how to manage files with extension php (handlers). Add some kind of php files as DirectoryIndex:
    1234567
    ...
    DirectoryIndex ... (other files) ... index.php index.php4 index.php5
    AddHandler application/x-httpd-php .php
    AddHandler application/x-httpd-php .php4
    AddHandler application/x-httpd-php .php5
    ...
MySQL installation
  1. Install MySQL as database server (version 5.1.61 currently available via yum in CentOS 6.2). Install also the server and the php-mysql connection:
    yum install mysql mysql-server php-mysql
    Note that dependencies can be required to be installed with these packages.

  2. Now we make mysqld daemon starting always that the server is started, adding it to some levels. Also open the listen port for external connections, and save the iptables changes:
    chkconfig --levels 235 mysqld on
    iptables -I INPUT -p tcp -m tcp --dport ${MYSQL_PORT} -j ACCEPT
    service iptables save
  3. To configure mysql you need to start the daemon:
    service mysqld start
  4. After that, you need to change the password for root (initially whitout password). Log into mysql command line:
    mysql -u root
    On the command line mysql we change the root password for all the local domains:
    mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new-password');
    mysql> SET PASSWORD FOR 'root'@'localhost.localdomain' = PASSWORD('new-password');
    mysql> SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('new-password');
    And we drop the Any user:
    mysql> DROP USER ''@'localhost';
    mysql> DROP USER ''@'localhost.localdomain';
    And exit from the command line mysql:
    mysql> exit
FTP installation
  1. Install vsftp as ftp server (version 2.2.2 currently available via yum in CentOS 6.2):
    yum install vsftpd
  2. Now we make vsftpd daemon starting always that the server is started. Also open the listen port for external connections, and save the iptables changes:
    chkconfig --levels 3 vsftpd on
    iptables -I INPUT -p tcp -m tcp --dport ${FTP_PORT} -j ACCEPT
    service iptables save
  3. Change all the configuration parameters in the next file:
    • /etc/vsftpd/vsftpd.conf
    Don't forget to disallow anonymous access, and keep users as root forbidden to access also. If needed, create some user to check if the server is working.

  4. To allow passive connection to the server, add these lines to the configuration file:
    1234
    pasv_enable=YES
    pasv_max_port=${PASV_MAX_PORT}
    pasv_min_port=${PASV_MIN_PORT}
    port_enable=YES
    I have used some free ports, from ${PASV_MIN_PORT} to ${PASV_MAX_PORT} but you can use any free port in your server. Remember that the firsts ports are reserved for known applications.

    You need to open the ports that you are planning to use for passive connections:
    iptables -I INPUT -p tcp -m tcp --dport ${PASV_MIN_PORT}:${PASV_MAX_PORT} -j ACCEPT
    service iptables save
  5. Finally, to allow users to read / write in their home dirs, change the boolean var ftp_home_dir in the firewall with the command:
    setsebool -P ftp_home_dir 1
Mail installation
  1. Install dovecot as mail server (version 2.0.9 currently available via yum in CentOS 6.2), working as IMAP and POP3 server:
    yum install dovecot
  2. You can configure all the parameters in the next file:
    • /etc/dovecot/dovecot.conf
    Also, all the files included in the folder /etc/dovecot/conf.d will be loaded as configuration. There you can change some properties, per file, for example, the mail_location property can be changed in the file 10-mail.conf. Properties about authentication can be changed in the file 10-auth.conf; about quotas in the file 90-quota.conf and so on. Plase, check the wiki in the dovecot page for detailed list of properties.

  3. Set the daemon to start always and open ports:
    chkconfig --levels 3 dovecot on
    iptables -I INPUT -p tcp -m tcp --dport ${IMAP_PORT} -j ACCEPT
    iptables -I INPUT -p tcp -m tcp --dport ${POP3_PORT} -j ACCEPT
    service iptables save
  4. Now install sendmail for SMTP server, as dovecot is only IMAP and POP3 server:
    yum install sendmail
  5. You can configure all the parameters for sendmail in the next file:
    • /etc/mail/sendmail.conf
    As the configuration is more complex for this server, please read about this software in the sendmail official page.

  6. Finally, set the daemon to start always and open ports:
    chkconfig --levels 3 sendmail on
    iptables -I INPUT -p tcp -m tcp --dport ${SMTP_PORT} -j ACCEPT
    service iptables save
References http://httpd.apache.org/
http://www.php.net/
http://www.mysql.com/
http://vsftpd.beasts.org/
http://www.dovecot.org/
http://www.sendmail.org/



Back to the list of entries