Just another idiot with a blog
Currently Browsing: PHP

Configuring Fedora 13 PHP/ MySQL Server with Nginx and reverse proxy to Apache

Ok so I’m going to make this article quick, simple and to the point.  Fuck the bullshit, lets get ya’ll setup with nginx with a  reverse proxy to apache in less than an hour.   Now this article is made for VPS type system such as linode or slicehost.  We’re starting at the point in which you have Fedora 13 actually installed, base system.  So lets get started.

Install Nginx

Lets start by installing nginx, start the service and configure it to start at boot.

yum install nginx
service nginx start
chkconfig nginx on

Now lets install php, mysql and some dependencies.

Was going to use fast-cgi but it crashes so often, so I created a reverse proxy into apache.

yum install httpd httpd-devel php php-cli php-devel mysql php-mysql mysql-server php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy sendmail

Configuring nginx

nano /etc/nginx/nginx.conf

Once inside the file, change these two values

worker_processes  5;
keepalive_timeout  2;

Similar to apache you can set virtual hosts. First lets start by editing the example in the nginx.conf file.

You can define the vhost in server {} containers. Now lets modify the default vhost:

server_name _; makes this a default vhost ( so lets change this to specify a certain domain e.g. www.something.com).

In the location / part, add index.php to the index line. root /usr/share/nginx/html;

This is your DocumentRoot in an apache virtual host, so you can change it to whatever you want..I prefer the /var/www/ directories.

The important part for PHP is the location ~ \.php$ {} container. You need to uncomment it to enable it. Change the root line to the web site’s document root (e.g. root /usr/share/nginx/html; or whatever you directory you chose.). Be sure to pass .php to 127.0.0.1:8080 to proxy php files to apache (we’ll edit httpd.conf later).

Apache as proxy

nano /etc/httpd/conf/httpd.conf

Pass through to apache port 8080

Listen 127.0.0.1:8080

configure the virtualhost to reflect the same path as the nginx vhost.

Install and configure MySQL

Make sure its ready for startup and start it up

chkconfig mysqld on
/etc/init.d/mysqld start

Now lets install MySQL securely

mysql_secure_installation

>Enter Current Root Password : Hit Enter
>Set Root Password : Hit Enter
Now enter your password twice
The rest are your choice, I hit enter all the way through.

Install APC and other Dev Tools

Lets install some dependencies, most are just needed on a dev server anyway, so group install. Fail2ban is to keep users from attacking your site, they will be banned after 5 failed attemps to login for 10 mins. You can change the configuration in /etc/fail2ban/

yum groupinstall 'Development Tools'
yum groupinstall 'Development Libraries'
yum install php-pecl-apc
yum install fail2ban
yum install mlocate

chkconfig fail2ban on
chkconfig httpd on
chkconfig nginx on
chkconfig mysqld on
updatedb

In the fail2ban configuration file you can change additional settings, they are very self explanatory. To edit fail2ban configuration go to:

nano /etc/fail2ban/jail.conf

Now guess what, you’re done. If you have any questions, just leave a comment and I’ll be sure to answer it, enjoy the nginx server.

Feeling Generous??

Sign up for a linode VPS, I pay $20 a month and it fucking rocks

My Linode.com Referral Code 2bf9ae77ecd8e759a2b79bf1011e1c3147d93525

-Wes

Kohana 3 Name Spacing with Underscores in Controller Classes

So, I’ve been working with Kohana 2.3.4 lately and its absolutely beautiful. A much improved version of what Codeigniter should have been. Now they released Kohana 3 which is similar, yet very different. One of the “gotchas” I noticed in KO3 (Kohana 3) is the auto loading of controller classes and the name spacing with underscores. Here is an example of what I’m talking about.

All underscores are converted to slashes

In Kohana 2 you could name a class with underscores and there would be no problem

now the underscores are converted to slashes

notice the Controller_ is in the front now which translates to controller/

Within the classes folder in application you notice there is classes/controllers to keep for instance a file extending off template.

So a template in controllers would need to be named defaulttemplate.php which would become Controller_DefaultTemplate, BUT if you wanted it to be Controller_Default_Template it would have to be in a sub folder called Default in controllers then template.php

Its a bit confusing in the beginning but you’ll get used to it. The entire system is a helluva improvement. Look forward to a few articles i’ll be writing on using the ORM.

Cheers,
-Wes

Remove Non Ascii Characters using PHP and Regular Expressions

So there I was taking forever to fix content from a Word Perfect document converted to Word then pasted as HTML. I was spending a while removing all the bullshit non Ascii Characters and I thought to myself and said…Wes, you idiot, you are a programmer, do this the right way and filter it out. So I did and here is a simple way of doing it through regular expressions and php.


<?php
//Your nasty string of word and non ascii chars
$Contentz = "These are shitty chars „ and we dont like them nor want them.";

//Array of content I want to make a space
$badContent = array("&nbsp;");

//Replace the bad arrays with a space
$Contentz = trim(str_replace($badContent," ",$Contentz));

//Specific string replaces for ellipsis, etc that you dont want removed but replaced
$theBad = 	array("“","”","‘","’","…","—","–");
$theGood = array("\"","\"","'","'","...","-","-");
$Contentz = str_replace($theBad,$theGood,$Contentz);

//Whatever might be left over...
//Remove all non ascii chars (aka: bad Microsoft Word and Word Perfect Shit shit)
$Contentz = preg_replace('/[^(\x20-\x7F)\x0A]*/','', $Contentz);

echo $Contentz;
?>

$Contentz will show up removing the characters.

Cheers,
Wes S .Ray

Configuring PHP Web server on Fedora 11

This is going to be a quick and simple tutorial on how to setup a Fedora 11 Linux Server with PHP, MySQL, Apache and phpmyadmin. Now I’m assuming you already have the OS installed on your machine. Most of you will be using cloud servers, mine is from Linode.com. You will need to have shell access to your server. If you have very little experience with linux I suggest you brush up and learn a little syntax so you aren’t shooting in the dark.

Ok, so lets get started with the actual configuration of the server. At this point I AM ASSUMING you have the operating system installed and its just a bare canvas.

Here we go, hold on kids.

The first step that is needed to taken is open up your terminal, I personally use PuTTY, a free telnet/ssh client. One you are logged in make sure you have full access to the system, whether it be root or sudo.

Now lets install PHP and MySQL. These will be commands you type in the terminal.  We will be installing the apache, mysql and php and configuring them.

  • yum -y install httpd php mysql mysql-server php-mysql

We set chkconfig so that when rebooted the servers will start automatically, and we do this like so.

  • chkconfig httpd on
  • chkconfig mysqld on

Now that Apache and MySQL are starting with the server, we can configure MySQL

  • mysqladmin -u root ‘root_pw_of_your_choice’

Now lets install phpMyAdmin, a mysql database management tool.

  • yum install phpMyAdmin.noarch

Since that has been taken care of let, lets open up the phpmyadmin config.

  • nano /etc/httpd/conf.d/phpMyAdmin.conf

At this point what we need to do is comment out a certain section. Notice that I have a # which denotes comment out

#<Directory /usr/share/phpMyAdmin/>
#   order deny,allow
#   deny from all
#   allow from 127.0.0.1
#</Directory>

Next we are going to jump into the httpd.conf, otherwise known as the Apache Config File

  • nano /etc/httpd/conf/httpd.conf

Once in there you are going to want to change a section

<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>

You will want to set it to AllowOverride All, by default it should be set to none, but we dont want that.

Now at the end of the file add these to make your IP address directly or URL go directly to phpMyAdmin unless you set a virtualhost otherwise.  You need to put all of this at the bottom of your httpd.conf file

<VirtualHost your.ip:80>
ServerAdmin youremail@server.com
DocumentRoot /usr/share/phpMyAdmin/

ServerName yourIP
ServerAlias yourIP

ErrorLog logs/home-error_log
CustomLog logs/home-access_log common
</VirtualHost>

Include /etc/httpd/conf/sites-production/*
Include /etc/httpd/conf/sites-development/*

Save and close the conf file and finally create your folders than you included, e.g

  • mkdir /etc/httpd/conf/sites-production
  • mkdir /etc/httpd/conf/sites-development

Now that the config file directories are made, lets make the place holders for the actual website content

  • /var/www/sites-production
  • /var/www/sites-development

That about sums it up for the base server setup on fedora 11 for a linode VPS.  If you want more PHP Packages just run this command and you should have everything you need.

yum install php php-devel php-gd php-imap php-ldap php-mysql php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-magpierss php-mapserver php-mbstring php-mcrypt php-mhash php-mssql php-shout php-snmp php-soap php-tidy curl curl-devel perl-libwww-perl ImageMagick libxml2 libxml2-devel

If you need any MySQL help for setting up users, I reccomend this site

http://www.databasef1.com/tutorial/mysql-create-user.html

If any help is needed feel free to leave comments and I will respond.  Good luck!

Feeling Generous??

Sign up for a linode VPS, I pay $20 a month and it fucking rocks

My Linode.com Referral Code 2bf9ae77ecd8e759a2b79bf1011e1c3147d93525

-Wes

Copyright ©2012 wessray.com
Protected by Copyscape Unique Content Validation