lemp server[Linux nginx mysql php]+wordpress+port forwarding + dynamic dns{2nd spirula task}

I would like to inform at the first that I wrote this after I finished my work and i couldn’t revise it well ,so fell free to ask,use ,or even correct whatever you want .Actually I mentioned the use/the reason for every single command here to give you the needed concept to search for the reason if it didn’t work/gave you un expecteded o/p .

I would like too to mention that this tutorial helped me aloot even in writing this http://interfacelab.com/nginx-php-fpm-apc-awesome/

ok now i will write a fast How-to for my second task

I was asked to do the foloowing :

-Install a LEMP server {Linux nginx mysql php }

-choose any open source bloging project and install it {choosed wordpress}

-configure my ADSL router to port forward any requests to my server {http , ssh}

-install dynamic dns  client on my server and make it update my free sub-domian reserved at dyndns.net continously

Some notes && background :

-nginx is a very powerfull web server which is well known with its great performance .although it is well known too that it dont have many modules and options like other web servers{apache …etc}.It is v v light though you can use it as a reversed proxy {This is actually one of the common uses for it }

-nginx dont handle php scripts from modules but the only way is to use fast cgi {http://en.wikipedia.org/wiki/FastCGI}as a concept .We can here use spawn-fgci or php-fpm .at the first i used spwn but later I moved to php-fpm .because it has less errors and better performance .

-php-fpm is actually a patching to the php source code no more .

-I choosed wordpress as I am using a P3 machine in my office,and wordpess is +performance than drupal{although drupal has many and many added options }

-I have a repotec ADSL router {will find the model and wirte it here later inshallah}.please note that there is some brands/models of ADSL modems+routers{including mine } don’t  provide the port forwarding in a stable way .and there is others that don’t work at all .

-As i don’t have a static IP in my ADSL line I have to setup a dynamic dns client that will update a sub dmoain registered for free on dyndns.net so that any time i want to access my server i will find that it has updated the real ip of my adsl line and binded it to my-sub-dmain.dyndns.net


-Installation was not smooth at all .There was many and may depencies I had to reolve ,many errors to handle and so .For sorry I couldn’t take snapshots of all of them but :-I will try to remeber and search 2-You are welcomed to leave a comment/send me asking {actually I prefer to comment to let others see the question and the answer }

Now lets see the work plan

-install some depencies

-download php {i used 5.2.13 }

-download php-fpm {note that every php version has its php-fpm }

-compile,make,make install php

-some modules

-edit some php configuration files

-installing mysql

-install php5-mysql

-downloading and installing nginx

-configuring a default site for nginx

-starting &&testing

-adding them to startup


apt-get install make bison flex gcc patch autoconf subversion loca

apt-get install libxml2-dev libbz2-dev libpcre3-dev libssl-dev zlib1g-dev libmcrypt-dev libmhash-dev libmhash2 libcurl4-openssl-dev libpq-dev libpq5 libsyck0-dev
#To me I found later many and many other depencies .
cd /usr/local/src/
#now you have to download php .choose a mirror from here ,copy its link location then wget it[http://www.php.net/get/php-5.2.13.tar.gz/from/a/mirror]..this is for php 5.2.13 which i used,feel free to choose any other version you want,just make sure you can find its php-fpm from here [http://php-fpm.org/downloads/]
tar zxvf  [php tarball name ]
#download its php-fpm from here [http://php-fpm.org/downloads/]
wget  [php-fpm source ]
gzip -cd php-5.2.8-fpm-0.5.10.diff.gz | patch -d php-5.2.8 -p1
#compile & install php
./configure --enable-fastcgi --enable-fpm --with-mcrypt --with-zlib --enable-mbstring --disable-pdo --with-pgsql --with-curl --disable-debug --enable-pic --disable-rpath --enable-inline-optimization --with-bz2 --with-xml --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-xslt --enable-memcache --enable-zip --with-pcre-regex --with-mysql
#at the last command you can have a look and remove any options you are sure you will not need it ..BUT ….please take care .To me I didn’t add –with-mysql at the first .this led me to many troubles ..{I will talk about this later inshallah}
#now installing
make all install
#I dunno why this command but it is found in all How-Tos I used
strip /usr/local/bin/php-cgi
#installing some modules
pecl install memcache

pecl install apc
pecl install syck-beta
cp /usr/local/src/php-5.2.8/php.ini-recommended /usr/local/lib/php.ini
mkdir /etc/php/

ln -s /usr/local/lib/php.ini /etc/php/php.ini
ln -s /usr/local/etc/php-fpm.conf /etc/php/php-fpm.conf
#now we have to edit /etc/php/php-fpm.conf to set the user and group options
<value name="owner">www-data</value>

<value name="group">www-data</value>
<value name="user">www-data</value>
<value name="group">www-data</value>
#now its time for nginx,feel free to choose the vesrion you want
cd ..

sudo tar zxvf nginx-0.6.35.tar.gz
sudo rm -f nginx-0.6.35.tar.gz
cd nginx-0.6.35
sudo ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module
sudo make && sudo make install
#lets make it easier to reach config file
ln -s /usr/local/nginx/conf /etc/nginx
#at /etc/nginx/fastcgi_params we have to add some parameters for fast cgi
fastcgi_connect_timeout 60;

fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
#now we have to make a startup file called /etc/init.d/nginx like this :  note that I have added a comment to inform you with the end of the file
#! /bin/sh

# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon


test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx

set -e

case “$1” in
echo -n “Starting $DESC: “
start-stop-daemon –start –quiet –pidfile /usr/local/nginx/logs/$NAME.pid \
echo “$NAME.”
echo -n “Stopping $DESC: “
start-stop-daemon –stop –quiet –pidfile /usr/local/nginx/logs/$NAME.pid \
–exec $DAEMON
echo “$NAME.”
echo -n “Restarting $DESC: “
start-stop-daemon –stop –quiet –pidfile \
/usr/local/nginx/logs/$NAME.pid –exec $DAEMON
sleep 1
start-stop-daemon –start –quiet –pidfile \
/usr/local/nginx/logs/$NAME.pid –exec $DAEMON — $DAEMON_OPTS
echo “$NAME.”
echo -n “Reloading $DESC configuration: “
start-stop-daemon –stop –signal HUP –quiet –pidfile /usr/local/nginx/logs/$NAME.pid \
–exec $DAEMON
echo “$NAME.”
#echo “Usage: $N {start|stop|restart|force-reload}” &gt;&2
exit 1

exit 0

#——————This is the end of the file ————————————-#

mkdir /usr/local/nginx/sites-enabled

sudo ln -s /usr/local/nginx/sites-enabled /etc/sites

#And now let’s add a conf file for our default site at /etc/sites/default.conf. The contents:,note that this some times this is in different file .

server {
listen *:80;
location / {
root   /var/www/default/pub;
index index.php;
# if file exists return it right away
if (-f $request_filename) {
# otherwise rewrite the fucker
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php$1 last;
# if the request starts with our frontcontroller, pass it on to fastcgi
location ~ ^/index.php
fastcgi_param SCRIPT_FILENAME /var/www/default/pub$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
include /usr/local/nginx/conf/fastcgi_params;
#now start
php-fm start

/etc/init.d/nginx start
#then  test
-create a html file called index.htm which just contain any sentense  like hi this is my server …
-open a browser then write the ip address of the server …you must see the sentense
-delete the first file and create another one called index.php and copy& paste these lines into it
echo “if i saw ONLY this sentense ,then webserver and php are running “;
-you must see this only “if i saw ONLY this sentense ,then webserver and php are running “
#ok ,now congratulations ,your server is running ,just we have to make it startup with  the system startup automaticlly
update-rc.d nginx defaults
#this will add the script we created before {/etc/init.d/nginx } to the directories that control the run levels so that it start with the system startup
#now we have to create a nother 1 for php-fpm
vi /etc/php-fpm
#then add this line
php-fpm $1
#then add it to startup
update-rc.d php-fpm
#note: this didn’t work with me so i edited /etc/init.d/nginx and added the line
php-fpm start
php-fpm stop at the two condition parts
Port forwarding
simply  configuring port forwarding on my ADSL modem+routr means that I am telling my router to redirect requests that come to defined ports to a local machine that is connected to the lan card .
Simply we will need to port-foward 2 ports :
– 80 which the web server use
– 22 which ssh use {you may prefer ot to add it if you dont ssh your server from any other network}
port forwarding is pretty easy ,you can find the instructions for your router model from [www.portforward.com]
it is some times called virtual server too
dynamic dns
#ok ,now we have a problem ,which is ……every time I restart my router it is granted a new ip address fromt he isp so if i found that this is myu ip today [x.x.x.x] then tried after the next reboot to use it  iw ill find that it has changed ….so as we are having a dynamic changing of ip addresses we will solve it with dynamic dns 😀
…simsply i will reserve a free sub domain then install a small program called dynamic dns client on my server ..this client will have to update my subdomain with the new ip address every time it is changed …so i can access my server through this sub domain
create a free account
choose to add a host
create a sub domain
#install inadyn
apt-get install inadyn
now use this command
inadyn –username [username u reserved at dyndns] –password [your password ] –update_period_sec 600 –alias [your sub domain name] &
#try to access your server from internet …PLEASE note that if you tried from the local network you MUST use any online proxy
#finally we have to create a /etc/init.d file for inadyn
#here is mine {note that i will tell you in a comment that this is the end if the file }

case “$1” in
“start”)inadyn –username zakishan –password master77 –update_period_sec 600 –alias zakishan.dnsalias.net &
x=`pgrep inadyn`
kill -9 $x
*)echo enter valid start/stop

#finally add it to the system startup using update-rc.d
update-rc.d inadyn defaults
#reboot your server ,check your log files ,test your server again and …..get ready to install the word press …..
Word press installation
#You can easily follow this [http://codex.wordpress.org/Installing_WordPress],I just quoted some of its instructions
#simply wordpress will use our mysql database so we have to
-create a username and password for it to use
-create a databse
-grant the user privileges on this DB
-edit the wordpress configuration files and tell him what user ,pass and db will it use
-run the installation script which will create the tables the application need  and set the configuration entries
ok .now lets begin with mysql
mysql -u root -p
#it will ask you about the password you entered when you installed it
create user [username] identfied by [password ];
create database [db-name];
grant [username] *.* on [db-name]
#now we have to download the wordpress project tarball
#extract it to the path you want inside the root directory of the site you created before#Rename the wp-config-sample.php file to wp-config.php
#edit it ,find these lines  and give it the username/password/db
#now from your browser access the install.php file inside the wp-admin directory which you can found in your extracted folder of wordpress inside the root /whatever where you placed it

And now let’s add a conf file for our default site at /etc/sites/default.conf. The contents:

01.server {
02. listen *:80;
04. location / {
05. root   /var/www/default/pub;
06. index index.php;
08. # if file exists return it right away
09. if (-f $request_filename) {
10. break;
11. }
13. # otherwise rewrite the fucker
14. if (!-e $request_filename) {
15. rewrite ^(.+)$ /index.php$1 last;
16. break;
17. }
19. }
21. # if the request starts with our frontcontroller, pass it on to fastcgi
22. location ~ ^/index.php
23. {
24. fastcgi_pass;
25. fastcgi_param SCRIPT_FILENAME /var/www/default/pub$fastcgi_script_name;
26. fastcgi_param PATH_INFO $fastcgi_script_name;
27. include /usr/local/nginx/conf/fastcgi_params;
28. }

2 thoughts on “lemp server[Linux nginx mysql php]+wordpress+port forwarding + dynamic dns{2nd spirula task}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s