From wiki.mikejung.biz
Jump to: navigation, search

Liquidweb 728x90.jpg

How to Install and Optimize Apache Event 2.4.10 on Ubuntu 14.10

Install Apache Event and create vhost directory

The first thing you want to do is make sure that Ubuntu 14.10 is up to date. To do this run apt-get update to make sure all the repos are up to date. Once that is complete run apt-get upgrade which will check the newly updated repos and will attempt to upgrade any software that needs it. After Ubuntu 14.10 is up to date you can install apache2. I prefer to use the Event MPM because it's fast and awesome.

apt-get update
apt-get upgrade
apt-get install apache2 apache2-mpm-event apache2-utils

Once this is done you should be able to visit the IP of the server in your browser and you should see an Apache2 welcome page. Make sure apache is working before you try to do anything else!

Now that we know for sure that Apache is working we should make a new directory for our first website to use. If you are only planning on hosting a single site you could get away with skiping the vhost creation, but I don't really recommend this. It's best to create a new vhost instead of using the default one. To do this you first need to make a new directory for the site:

mkdir -p /var/www/$superawesomedomain.biz/public_html

Make sure you replace "$superawesomedomain.biz" with the real domain name you plan on using!

If you are doing all of this as the root user, you will have to change the directory ownership, otherwise nothing is going to work since Apache isn't running as root so it won't be able to properly handle files like PHP or things of that nature. By default apache runs as "www-data". If you only run a single site then you can use the command below, otherwise you will want to make a new user for each domain you plan on hosting because security.

chown -R www-data:www-data /var/www/$superawesomedomain.biz/public_html
chmod -R 755 /var/www

Go ahead and chmod the shit out of /var/www while you are at it. Once the ownership and file permissions are in place we can start to work on making a vhost config file and optimizing apache!

Create vhost file for a single domain

Let's copy the default vhost configuration, and name it after our awesome domain. You can do this by using the cp command. Make sure you rename the 000-default.conf to something different. You don't have to name the config file after the domain, but it makes sense to do this or else things might be confusing later on.

cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/$superawesomedomain.biz.conf

Use VIM or whatever text editor you want to open up the new vhost config file.

vim /etc/apache2/sites-available/$superawesomedomain.biz.conf

Initially, the file will contain this information. We will be changing it shortly

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Change "ServerAdmin" to an email address you plan on using. Replace "DocumentRoot" with the directory you just created. Also make sure you add "ServerName" and "ServerAlias" so that Apache knows what goes where. If you don't do any of this then how is Apache supposed to know if a domain is actually located on this server or not?

<VirtualHost *:80>
    ServerAdmin admin@$superawesomedomain.biz
    ServerName $superawesomedomain.biz
    ServerAlias www.$superawesomedomain.biz
    DocumentRoot /var/www/$superawesomedomain.biz/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Once you are done editing the file with vim, use ":wq" to write changes to the file and quit.

Apache 2.4 on Ubuntu allows you to enable, or disable vhosts with a simple command "a2endsite" or "a2dissite" followed by the name of the site. Once you have enabled the site you also need to reload apache2.

a2ensite $superawesomedomain.biz
service apache2 reload

Once apache has reloaded you should be able to visit the newly created site in your browser. You should get an index page that is blank if everything worked correctly.

Optimize Apache 2.4 Event MPM

Before we install wordpress, or whatever you plan on using for your website you will want to make sure that apache is configured with optimal settings. By default, Apache is already optimized for the most part. I always like to place in the default settings just so that someone doesn't come along and complain that there are no Apache settings in place and they copypasta some insane configuration from the internet. Just stick with the Apache defaults unless you know that you will exceed 400 requests at once, which is more than enough for most websites.

vim /etc/apache2/apache2.conf

Slap these suckers in the config file and save it. These are NOT OPTIMIZED settings, they are DEFAULT SETTINGS WHICH WORK JUST FINE 95% of the time. I'm using these settings to host this wiki, so I can confirm these settings work just fine for small to medium sized websites.

Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
<IfModule event.c>
ServerLimit           16
MaxClients           400
StartServers           3
ThreadsPerChild       25
ThreadLimit           64

I always like to stop and start apache instead of just restarting it.

service apache2 stop
service apache2 start

Congrats! You basically just optimized apache! If you are worried about performance, don't! 99% of performance gains come from using the latest versions of PHP and MySQL, which do most of the work when someone visits your site. Later on in this wiki I will show you how to configure HHVM, MySQL 5.6, Memcached, and Varnish. Caching stuff in RAM is what improves performance. Apache does so little work that attempting to make Apache faster is a massive waste of time.

How to install HHVM with Apache 2.4 Event Ubuntu 14.10

Installing HHVM is pretty simple. This is for Ubuntu 14.10, if you are using and older version you should be able to replace "utopic" with the codename for the version of Ubuntu you are running. Once the installation of HHVM is complete you can run the "install_fastcgi.sh" script which will configure Apache Event to pass along PHP files to HHVM for it to process. You will want to restart HHVM and Apache once the script is complete and I also recommend enabling HHVM to startup on boot unless you like downtime!

apt-get install software-properties-common
apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0x5a16e7281be7a449
add-apt-repository 'deb http://dl.hhvm.com/ubuntu utopic main'
apt-get update
apt-get install hhvm
/etc/init.d/hhvm restart
/etc/init.d/apache2 restart
update-rc.d hhvm defaults

By default, the script will configure hhvm_proxy_fcgi.conf to use fcgi://$1 as the location that Apache should watch for requests from, or something, and make HHVM do the PHP. I can't think of a better way to explain what is going on here, so hopefully you get what I mean. Anyway, this is great if you only use default vhosts, but no one does that so you need to modify the configuration file below and update the line if the location of your site. If you are running multiple domains and want to use HHVM for all of them you will want to add this line on a per vhost basis.

vim /etc/apache2/mods-enabled/hhvm_proxy_fcgi.conf

Comment out the original ProxyPassMatch that Apache uses to pass shit to HHVM and replace it with one that actually points to your doc root for your domain. Replace "$sweetdomain.biz" with your domain. Save the file and restart Apache

#ProxyPassMatch ^/(.+\.(hh|php)(/.*)?)$ fcgi://$1
##Change To
ProxyPassMatch ^/(.+\.(hh|php)(/.*)?)$ fcgi://$sweetdomain.biz/public_html/$1

Change directory to your domain's public_html/ and wget a HHVM phpinfo file, update ownership and then view it in your browser. If everything goes well you should see a basic PHP info file with some HHVM specific stuff. If that is the case then congrats!

cd /var/www/$sweetdomain.biz/public_html/
wget https://gist.githubusercontent.com/ck-on/67ca91f0310a695ceb65/raw/69d9c0e1366f20aa99327652cfef332d900bf7e6/hhvminfo.php
chown www-data:www-data hhvminfo.php

At this point you should be all set to get your webhosting on. You should be able to install WordPress or whatever and go to town. You will need MySQL installed at some point. If you need help setting up MySQL you can view my wiki page here.

Common Commands for Ubuntu Server

To search for available packages that you can install on Ubuntu

apt-cache search $thing_to_search_for

Find the release version of Ubuntu

lsb_release -a

To see the version of the installed package, when I’m considering an application upgrade

apt-cache policy xxxxxx (where xxxxxx is the name of the package).

Remove a .deb package (obviously not installed via aptitude)

dpkg -r xxxxxx (where xxxxxx is the name of the package).

Quickly remove downloaded package manager updates, when no longer needed:

aptitude autoclean

Ubuntu Networking Commands

ifconfig – View information about the network configuration of the machine
iwconfig – View information on the wireless network
iwlist scan – Scan for wireless networks
/etc/init.d/networking restart – Restarts the network setup
/etc/network/interfaces – The configuration file interfaces
ifup interface – Enable an interface
ifdown interface – Disable interface

List of Apt commands

apt-get update – Have updates available
apt-get upgrade – Apply updates
apt-get dist-upgrade – Upgrade to newer version of Ubuntu
apt-get install pkg – Install a package
apt-get purge pkg – Uninstall a package
apt-get autoremove – Remove obsolete packages
apt-get -f install – Forcing the installation of a package
dpkg -i pkg.deb – Install the file package.Deb
gedit /etc/apt/sources.list –  Edit the APT source file

Change MySQL DataDir Ubuntu 14.10

Stop MySQL before moving anything otherwise your dataz might get messed up.

service mysql stop

Open my.cnf and change the "datadir" location from /var/lib/mysql to the new location.

vim /etc/mysql/my.cnf

##Change this line to new location

#datadir                = /var/lib/mysql
datadir = /$path/$to/$new/mysql

Sync over the MySQL data to the new location, this command keeps permissions the same.

rsync -avh /var/lib/mysql /$path/$to/$new/

All that's left to do is update apparmor with the new location, replace /$path/$to/$new/ with the actual path.

echo "alias /var/lib/mysql/ -> /$path/$to/$new/," >> /etc/apparmor.d/tunables/alias
/etc/init.d/apparmor reload

At this point MySQL should start right up, using the new location for data.

service mysql start

How to upgrade to Kernel 3.19 on Ubuntu 14.10

If you want to run the latest 3.19 Kernel on Ubuntu 14.10 you can run the commands below, which should upgrade you to 3.19.

wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v3.19-vivid/linux-headers-3.19.0-031900-generic_3.19.0-031900.201502091451_amd64.deb

wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v3.19-vivid/linux-headers-3.19.0-031900_3.19.0-031900.201502091451_all.deb

wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v3.19-vivid/linux-image-3.19.0-031900-generic_3.19.0-031900.201502091451_amd64.deb

dpkg -i linux-headers-3.19.0-*.deb linux-image-3.19.0-*.deb 

If you want to view the latest script to handle the upgrade, check out this link! - https://gist.github.com/d1egoaz/1f64d382b5c92d07d2f2

To be honest, there is not a ton of new and shiny features in the latest 3.19 kernel. Not to say it's not worth upgrading to, but compared to some previous kernel releases that offered major performance boosts, the 3.19 release didn't have anything that got me too excited. Anyway, if you want to get an idea of all the new things in Kernel 3.19, check out this link! - http://kernelnewbies.org/Linux_3.19

Ubuntu Kernel Performance Tweaks

I've moved along to another page to cover Ubuntu IO Schedulers and Kernel Tweaks, check it out if you like fast things

Upgrading Apache to 2.4.12 on Ubuntu 14.10 or 14.04

If you don't have Apache installed, it's really easy to install the latest version of apache, which is 2.4.12. Ubuntu 14.04 and Ubuntu 14.10 both ship with older versions, even 14.10 comes with 2.4.10. So if you want the latest version of Apache on Ubuntu you will need to use a PPA, the one listed below works just fine so I recommend using it. After ppa:ondrej/apache2 has been added to the repo, make sure you update Ubuntu, then install apache2. If you already have apache 2 installed, I suggest removing it completely and installing it after the PPA has been added.

add-apt-repository ppa:ondrej/apache2/
apt-get update
apt-get install apache2 apache2-bin apache2-data apache2-mpm-event