Tag Archives: Apache

Ubuntu Server: How To Enable OPcache for PHP 7 on Apache Tips, Tricks and Tutorials 23 OCT 2017

PHP is what is known as an interpreted coding language, meaning that unlike compiled apps that have already been compiled down into bytecode (i.e. you have to compile your application first so that the OS can run it), PHP scripts are only compiled and interpreted by the runtime engine on script request.

So, if you have scripts that are relatively static (i.e. you aren’t changing them very often), one good way to accelerate your PHP project is to use some sort of caching system that essentially pre-compiles your scripts down to bytecode, meaning less work come request time and thus theoretically, faster code execution.

Enter opcode caching. OPcache, originally a closed source part of the Zend Server project (under the moniker of Zend Optimizer), is an open source module that stores (caches) precompiled PHP script bytecode in shared memory, thus removing the need for PHP to first load and parse scripts on each request.

Since PHP 5.5.0, the module has come bundled with PHP (though disabled by default), meaning that for all intents and purposes, OPcache is now pretty much the standard when it comes to PHP accelerating.

To set up OPcache on an Ubuntu 16.04 server running PHP 7 over Apache, you first need to enable OPcache via the standard php.ini file. To edit the file:

sudo nano /etc/php/7.0/apache2/php.ini

To enable the module once you have the php.ini file is open, find the line reading ;opcache.enable=0, uncomment it by removing the ; and then enable by changing the 0 to 1:

opcache.enable=1

There are quite a few OPcache related settings that you can change while doing the setup, with the php.ini doing a good job of explaining the role of each one. The important ones to keep track of are: opcache.memory_consumption, opcache.max_accelerated_files, and opcache_revalidate_freq.

Save your changes and then enable the module via:

sudo phpenmod opcache

Finally, restart PHP:

sudo service apache2 restart

Monitoring OPCache is a whole other story (worth a google), but the easiest way just to make sure that it is in fact running is to call phpinfo() on one of your webpages. If enabled, you’ll see a bold section under the title of Zend OPcache, with the first column “Opcode Caching” listing a value of “Up and Running”.

Related Link: PHP OPcache | Wikipedia

Windows 10 and XAMPP: Port 80 in use by “Unable to open process” Tips, Tricks and Tutorials 01 AUG 2015

Originally the plan was to wait for Windows 10 to be a little more established before I took the jump and upgraded my work laptop (Windows 7) to Microsoft’s latest wunderkind (Windows 10). However, I got a little too excited (plus, free is always so attractive, isn’t it?), that I ended up jumping in and doing the upgrade as soon as my notification arrived! (So much for self control in other words).

(For interest’s sake, the upgrade went smoothly, though I had to uninstall a few applications and switch off a few startup apps for the best result. First impression? I really, really like it.)

Anyway, I did immediately hit my first snag when firing up XAMPP – the web server Apache failed to load, with the resulting message appearing in the console: Port 80 in use by “Unable to open process”.

netstat wasn’t much better at identifying the process locking up port 80 (also reporting “unable to identify process owner”), though the PID of 4 it returned did give a clue that it was probably a service currently using the port.

So I fired up the Services control panel (search “Services” using the search button next to the Windows button) and narrowed my search to all automated start services seeing as port 80 seemed to be locked at Windows start up already.

Knowing that port 80 is usually an internet related port, the services listing quickly threw up a likely suspect: W3SVC, otherwise known as World Wide Web Publishing Service.

It’s the service that provides the connectivity for Microsoft’s built in Internet Information Services Manager, but seeing as I want to use Apache for my web server, it’s quite safe to disable it.

Or as I did in my case, simply stop it and switch it’s startup type to manual.

Firing up XAMPP’s control panel confirmed that all was now well as Apache happily and greedily gobbled up port 80 as it started up!

disable World Wide Web Publishing Service Properties service

Related Link: XAMPP

How to Solve: a2ensite “Error, Site does not exist!” Tips, Tricks and Tutorials 04 JUL 2015

To enable a virtual vhost website on your Ubuntu Server running Apache, you need to first create a vhost configuration file (just copy 000-defaul.conf as a quick way to start), and then run the a2ensite command to enable it so that Apache knows about it.

However, nowadays attempting to run a2ensite might just pop up with this particular error message:

ERROR: Site [YOURSITE] does not exist!

This is not a problem that used to occur, but in newer versions of Ubuntu it does – the reason for this is simple – your configuration file is probably missing a .conf extension, and the a2ensite perl script now only works with filenames ending in .conf!

So to fix, simply add the .conf your site vhost configuration file and run a2ensite again.

sudo a2ensite YOURSITE.conf

This time around you should be all good, and can jump straight into the next step of reloading Apache’s configuration with:

sudo service apache2 reload

Nifty.

Apache attack helicopter

Error: Call to undefined function mcrypt_get_iv_size() Tips, Tricks and Tutorials 03 JUL 2015

I installed my web application onto a new version of Ubuntu Server, and noted almost immediately that the web application crashed on each login attempt. Consulting the Apache error log, the issue quickly became clear:

Call to undefined function mcrypt_get_iv_size()

It would seem that php_mcrypt.dll is no longer loaded on a default install, meaning that you need to enable the mcrypt module under apache/php. To do this is pretty easy:

sudo apt-get install php5-mcrypt
sudo php5enmod mcrypt
sudo service apache2 restart

Ran through those and everything was now up and running again – so worth noting for anyone else out there busy bashing their heads against a wall! :)

winchester-cathedral-crypt

WordPress: .htaccess Permalinks not Working (Ubuntu) Tips, Tricks and Tutorials 27 MAY 2014

If your freshly installed WordPress site is throwing standard 404 errors (the Apache default look and feel 404 pages mind you) instead of showing your content when accessing via permalinks, then chances are pretty pretty high that either a) WordPress can’t write to your .htaccess file or b) the Apache rewrite (mod_rewrite) module isn’t installed.

To solve this you’ll need to have SSH access into your Ubuntu server. Once you have logged in, head over to your WordPress install directory and see if there is a .htaccess file there:

ls -a .htaccess

If it isn’t there then that means that WordPress was unable to automatically create it, probably because of file rights. To resolve this, create a .htaccess file:

 touch .htaccess

And populate it with the following WordPress default .htaccess content:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Next, give it appropriate file rights:

chmod 755 .htaccess

Finally, let us go make sure that the Apache mod_rewrite module is installed by running the following:

sudo a2enmod rewrite
sudo service apache2 restart

With the mod_rewrite now enabled under Apache, next is to ensure that the AllowOverride directive is set appropriately for your Apache vhost directory declaration – you can use either AllowOverride FileInfo or AllowOverride All to achieve this. For example, for the default website, edit /etc/apache2/sites-available/default to reflect:

<Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            # changed from None to FileInfo
            AllowOverride FileInfo
            Order allow,deny
            allow from all
    </Directory>

With both of these now in place, your permalink problem should be solved.

horizontal wordpress logo landscape

SVN Commit Error: Repository Moved Temporarily; Please Relocate Tips, Tricks and Tutorials 17 FEB 2014

svn-logoI encountered a rather strange (and frustrating!) error today which was completely blocking me from committing any files into my SVN repo. After a lot of fiddling and tweaking (all unsuccessful) and finally just destroying the whole damn thing to start afresh, I did stumble across the cause and solution via Google, which means that obviously I now need to save it here for my own record.

The error Subversion was returning to me after every commit attempt read something along the lines of:

svn: Repository moved temporarily to 'xxxyyyzzz'; please relocate

The problem as it turns out is the fact that both the project webserver (in this case Apache) and Subversion are installed on the same server and thus share an Apache configuration (because you are probably using the dav_svn module to access your SVN repo).

Now if you are being clever with your 404 handling for your web project and maybe doing some redirects, and this errorDocument handler is specified on your default site configuration, then obviously your SVN module is affected by this as well. So for the one or two occasions where your SVN repo perhaps references a file that no longer exists in itself, your custom 404 redirect handler screws with the necessary SVN return error, thus resulting in the rather cryptic error message outlined above.

Luckily, it turns out that solving this issue is in fact very simple. All you need to do is edit your dav_svn.conf file:

sudo nano /etc/apache2/mods-enabled/dav_svn.conf

and add the default Apache 404 error document directive to it! A typical SVN configuration might then look like this:

<Location /svn> 
DAV svn 
SVNPath /var/svn-repos/svn 
AuthType Basic 
AuthName "Subversion Repository" 
AuthUserFile /etc/apache2/dav_svn.passwd 
Require valid-user 
SSLRequireSSL 
ErrorDocument 404 default 
</Location>

So yeah, I just wasted a lot of time by not finding this nugget via Google first.

Ubuntu Server: Apache: Turn off SSL Tips, Tricks and Tutorials 21 JAN 2014

ubuntu-torso-in-a-white-topIn the event that you’ve traditionally had your Apache webserver serving up SSL-encrypted web traffic on your Ubuntu server and you’ve now gone and changed your mind (or simply don’t want to pay for the privilege of someone else saying you’re perfectly safe and okay, here, have this certificate, now where’s the money any more), turning off SSL is pretty simple.

To do this we make use of the special scripts available to us by default when using Apache on an Ubuntu install, first disabling the SSL module, then disabling the default-ssl site, and finally restarting the Apache service itself.

The commands are as follows:

sudo a2dismod ssl
sudo a2dissite default-ssl
sudo service apache2 restart

Obviously if you have any other SSL-enabled site configurations in your /etc/apache2/sites-enabled folder, you’ll want to run the a2dissite against them as well!

Also, if you don’t want to risk restarting your webserver,’service apache reload’ might also be sufficient for reloading the webserver configuration.

How to quickly install the LAMP Stack on an Ubuntu Server Tips, Tricks and Tutorials 24 AUG 2013

ubuntu-10-logoIf you are running an Ubuntu server then chances are pretty good that you are using it as a web server. That in turn means you probably want to run it as a LAMP stack, i.e. Linux, Apache, MySQL, and PHP, or at least that’s what I need 90% of the time.

Of course the fact that you are running Ubuntu means that the Linux part of the stack is already taken care of meaning that the next thing to sort out is the installation of the Apache webserver, MySQL database server, and of course the web scripting workhorse that is PHP.

Now while you can install these things one by one, but if defaults are fine for you and time is of the essence, then thanks to the nifty tasksel Ubuntu command installing all three at once is a snap.

Basically tasksel groups software packages by tasks and offers an easy way to install all packages needed for that particular task. In other words, basically it does the same as a conventional meta-package.

If it isn’t installed by default, run:

sudo apt-get install tasksel

Once installed, you can run the application like so:

sudo tasksel

From there it is a matter of scrolling through the menu to locate the LAMP server option, press space to select it (an asterisk will appear next to the menu option) and then a final click on OK to begin the installation process (you can tab to the OK button if you don’t have a mouse available). Answer the MySQL root password prompt accordingly and once done, you should have a nice and shiny new installation of Apache, MySQL and PHP at your fingertips, verified by the standard “It Works!” message returned when hitting http://localhost

Note: I’m quite fond of following this up by installing the web-based database management tool PHPMyAdmin, which can be done via a call to:

sudo apt-get install phpmyadmin

Again, you’ll be asked for the root MySQL password to complete this installation, so it is probably worth having jotted this down somewhere! :)

Ubuntu Server: What version of Apache am I running? Tips, Tricks and Tutorials 12 AUG 2013

ubuntu-10-logoThe quickest way to determine what build or version of the Apache webserver you are running on your Ubuntu Server is simply to call up a terminal and run the -v switch on the apache2 command.

This will output both the version as well as a date of when this server version was first built.

In practice:

sudo apache2 -v

Pleasingly enough, this will also work if you want to find out what version of PHP you are running on your Ubuntu Server, as well as what version of MySQL (both client and server) you are running.

Again:

#PHP version your Ubuntu Server is running:
sudo php -v

#MySQL Server version your Ubuntu Server is running:
sudo mysqld -V

#MySQL Client version your Ubuntu Server is running:
sudo mysql -V

Nifty.