Skip to content

Putting Apache on a diet - how to get a lean configuration, Part 1

In this article I'm going to go over some of the details of how Apache works, and uses memory. As you will see, the apache processes that serve requests will grow to include the amount of memory they need for a particular request, and they will still be using that memory, even when they are serving something simple like a .css file or an image.

For this reason, the first limitation you are likely to encounter running Apache is a limitation on memory. I will explore this topic and show you how you can do the same.

The first thing need to determine about Apache is what mode it is running in. If your plan is to use Apache with php via mod_apache, then the prevailing wisdom for many years has been that there may be library extensions in PHP that are not thread safe. For this reason, people have tended to avoid running Apache as a threaded server, for fear that scripts may randomly die producing the dreaded 500 Internal server error pages we all know and love.

This fear may be overstated depending on the nature of your php code, but the defaults for apache packages I've seen, seem almost always to be using the prefork worker.

Prefork vs Worker?

So the first question you might be asking yourself is, which mode is my current apache server running in? There is a simple safe way to determine this -- run the httpd(apache) program with a specific command line switch. There are programs like which and whereis that might help you locate it, if you don't know where it is.


whereis httpd
 


CODE:
httpd: /usr/sbin/httpd.worker /usr/sbin/httpd /usr/sbin/httpd.event /etc/httpd /usr/share/man/man8/httpd.8.gz

So let's run the /usr/sbin/httpd with the -h for help and see what happens:




Continue reading "Putting Apache on a diet - how to get a lean configuration, Part 1"

PHP Mysql support: mysql or mysqlnd?

So you are installing a modern version of php using one of the alternative repositories, and suddenly you are confronted with a confusing choice. You want support for mysql (mysqli or PDO-mysql) in your php program. But which one to choose?

First off, you should probably be using PDO. It's just a cleaner database interface when compared to mysqli, and also tends to be the supported option if you're using an ORM like Doctrine2.

But you probably have found that installing the PDO package doesn't get you support for MySQL.

So what are these 2 packages? Well let's see what yum under Centos shows us, once we've setup webtatic as a repo:


 * webtatic: us-east.repo.webtatic.com
==============================================
php56w-mysql.x86_64 : A module for PHP applications that use MySQL databases
php56w-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases
 


In short, the mysql extension aka the mysql library is to quote Oracle/mysql
... a general-purpose client library
named libmysql.

This was the original php approach to supporting mysql. MySQL provided a client api library, and using that c library, a php extension was created that depends upon libmysql implementing the famous mysql_ functions that allowed php to talk to mysql.

The mysqlnd package (where nd stands for "native driver") is the fruit of a project to make mysql work optimally in the php language. Again to quote the mysql site:

The mysqlnd library is highly optimized for and tightly integrated into PHP. The MySQL Client Library cannot offer the same optimizations because it is a general-purpose client library.

The mysqlnd library is using PHP internal C infrastructure for seamless integration into PHP. In addition, it is using PHP memory management, PHP Streams (I/O abstraction) and PHP string handling routines. The use of PHP memory management by mysqlnd allows, for example, memory savings by using read-only variables (copy on write) and makes mysqlnd apply to PHP memory limits.


On top of these benefits are a number of interesting enhancements and support for plugins that might be of specific interest to you as a developer or sysadmin.

In general nothing should break in your code as the api should work the same under mysqlnd as it did with the old mysql library.

Conclusion

In summary, you want to use mysqlnd now and in the future.



Defined tags for this entry: , , , , ,

More git prompt - Does it work on a Mac?

So a few years ago I wrote this article about setting a custom shell prompt that is "Git aware" and shows you your current branch.

The question came up as to whether or not this works on a Mac under OS/X.

I have always advocated avoiding things like WAMP or MAMP because I don't like a bunch of services running on my workstation. I prefer using virtualization to run a *nix distro matching whatever target deployment server I'm going to run under. VMWare, Virtualbox etc. along with the popularity of Vagrant and Docker have tremendous advantages over something like MAMP in my experience. You start the environment when you need it, and stop it when you don't, and there's no problem having 5 different VM's with different stacks and php versions.

For this reason, I have never been all that concerned with setting a git aware shell prompt up on my macbook. But as it's a *nix-like operating system, it has the basics you need to make the shell prompt code work, albeit with 2 required tweaks.

First you have to edit the /etc/profile script so that it will look for and read scripts in an /etc/profile.d directory. sudo vi, nano or whatever you want to edit the /etc/profile script and add this at the bottom:


for sh in /etc/profile.d/*.sh ; do
        [ -r "$sh" ] && . "$sh"
done
unset sh
 


This is simple bourne shell code to read in scripts in the /etc/profile.d directory when you login to a shell. It is a system-wide script, so when you change this, you change it for all users on the system.

Now you just have to create the /etc/profile.d directory.


sudo mkdir /etc/profile.d
 


Once this is done, you can use the same simple method described in the original article.



Defined tags for this entry: , ,

Fixing PHP short open tags

Find and replace in Eclipse PDTA PHP block is started with the tag <?php. However, there is also an alternative known as a "short open tag" which is to only use <?

The problem with using short open tags is that they conflict with xml parsers and for that reason, support for short open tags has to be enabled. By default, it's disabled and enabling deprecated features may be impossible if hosts don't allow it.

Every so often you may come upon a code base that was written using short open tags.

Often people are first confronted with this problem when they have a legacy code base, and either move it to a new server or upgrade php only to find that the site is spewing errors and no longer functional. In a worse case scenario portions of your php code will be plainly visible to end users due to the fact that the php parser is no longer parsing those blocks and simply returning them as html text.

There are a number of different approaches you can take to solve this problem. First you can turn on the support for short open tags, but as I mentioned previously, this is not recommended.

Continue reading "Fixing PHP short open tags"

How I found symbolic links for gitignore with find and cut

I was cleaning up a directory of old files for addition to git, and I came across a bunch of symlinks to a completely different codebase. In this particular case it was a wordpress blog that had a series of its files linked into the webroot of the site, that existed in an entirely different directory. I didn't want these files in the git repo.

Finding all the symlink files is easy enough.


cd /path/to/root
 


The find command gives us an easy way to get all symlink files:


find . -type l
 


But that's just a start -- we have to get these into the .gitignore file. Continue reading "How I found symbolic links for gitignore with find and cut"

Showing your git branch in your shell prompt

At some point I found some code to get the git branch name and insert it into the shell prompt. As it was some years ago, sadly I've forgotten the original source, but it wasn't exactly the type of prompt I'm used to, so I tweaked it until I got it the way I prefer. In the first set of brackets, I display the user@host:directory.

Once I cd into a git initialized directory a colored prompt appears with the branch name displayed in gold. For my purposes, this is simple, functional and doesn't result in overly long prompts. Here's what you can expect from any terminal that supports ansi color codes.



To implement the prompt on your server or workstation, simply cut this snippet and paste it into the end of the /etc/bashrc file.

The current way to do this is to create a new file named "gitprompt.sh" in the /etc/profile.d directory, and paste the code below into it. Using vim is a good way to accomplish this task. You need to be root or sudo for this to work of course.

Once the file exists, all the system users will benefit from the prompt in their shell sessions.

Using this prompt, whenever you're in your bash shell, there will be no question of whether or not you are in a git initialized directory, or what your currently checked out branch name is.


# Custom aliases and functions
function parse_git_branch_and_add_brackets {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\ \[\1\]/'
}
PS1="[\u@\h:\W]\[\033[0;33m\]\$(parse_git_branch_and_add_brackets)\[\033[0m\]\$ "
 


Does this work on a Mac? Yes of course!




Defined tags for this entry: , , ,