PHPBuilder - Top 4 Web Server Administration Tips

RSS Twitter

Top 4 Web Server Administration Tips

by: Voja Janjic
January 31, 2016

As a server owner, I can say that server administration is a very complicated job. It involves a Linux operating system, web server, database server, FTP and e-mail servers along with other software that can sometimes cause very unusual bugs and problems. Also, it is crucial to fix the problems as soon as possible, as clients expect high performance and high availability. I will share a few PHP-related server problems that have occurred and that I had to fix myself.


Cannot write to the disk from PHP

You have tried to create a folder or a file trough PHP, but you get the permission denied error. You try to install a WordPress plugin or upload an image and you also get the same error. Basically, you cannot write to the disk using PHP.


Why does this happen? In the Linux operating system, each file is run by a Linux user and each file has its own permissions. This applies to PHP files as well — so, when a user visits your website, the system will run the PHP file as a Linux user with certain permissions. In CentOS, that user is called nobody and has the least permissions on the system. The file permissions contain three numbers — the first one is the permission for file owner, the second is for owner's group and the third one applies to everyone. Each of the number is a combination of the following numbers — 4 (read), 2 (write) and 1 (execute). If we take a directory with permissions 755 as an example, that means that the file owner can read, write and execute files in the directory (4+2+1=7), the users in the same group as the owner can read and execute files (4+1=5) and everyone else can also read and execute the files in the directory.


So, the user nobody would have only read and execute permissions and would not be able to create (write) a file. Many people solve this problem manually by changing permissions to 777 wherever needed. However, that is not good practice. It takes a lot of time and can become a security risk. Instead of that, suPHP or FastCGI should be enabled. It would automatically determine under which user the PHP script should be run, thus fixing all permission denied errors in a proper way.


To enable suPHP, go to Configure PHP and suEXEC in WHM/Cpanel, change the PHP 5 Handler to suphp and save the new configuration. In case that suPHP is not installed on your system, check this (for CentOS 6.x operating system).


Due to differences in running the files, you might need to manually fix some permissions after switching to suphp. Read this for more information.


APC cache limit

A PHP fatal error "Cannot redeclare class" can sometimes appear out of nowhere. If APC cache is installed on your server, you will need to increase its maximum file size. To do that, edit the php.ini and increase the apc.max_file_size value. The default is 2M — increase it to 6 or 8M.


Server load is small, but CPU and memory usage is high

Imagine the following situation — you have only a thousand visitors at the moment on your website, but it takes a long time to open any page, CPU usage is 100% on all six 2.4GHz cores and the RAM usage is also very high.


Most probably, Apache web server is installed on your web server. The problem with Apache is that its processes take up a lot of CPU and memory. One of the causes is that it always runs PHP, even for static files, which takes more CPU resources, more memory and more time. My suggestion is to install Nginx web server instead of Apache even before the website launch. But, if you already have live websites and need to change web server software, read about it here.


Also, you can further reduce the server load by using a CDN (content delivery network) for all static files. The static files would be cached and loaded from their servers. In the situation above, I have moved all CSS and JS files, as well as images to CDN and reduced the CPU load from 6 to only 2 cores. Check MaxCDN or Cloudflare for more information.


php.ini tweaks

By default, php.ini is not configured to cover some specific use-cases. If you need to run a PHP script in the background, it is a good idea to increase max_execution_time. If you are working with images, pdf files or anything that requires a substantial amount of memory, increase the upload_max_filesize and the memory_limit. Set these values to e.g. 8M and 256M. When working with APIs or fetching content from an external source, allow_url_fopen must be enabled.



Comment and Contribute

Your comment has been submitted and is pending approval.

Voja Janjic



(Maximum characters: 1200). You have characters left.