Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Saturday, November 16, 2013

Automagic Mod_Vhost_Alias for Vagrant precise PHP

Just sharing my Apache vhost to enable automatic virtual hosting for my development environment.
This will enable anything you put to '/vagrant/www/*' a vhost with a public directory. Useful if you are working with frameworks that redirects everything to public/index.php (e.g. Zend Framework)
A directory like:
vagrant/
    www/
        mysite/
            public/
        myothersite/
            public/

Will automatically setup:
1. http://mysite.local
2. http://myothersite.local

And here is the configuration:
<Virtualhost *:80>
    VirtualDocumentRoot "/vagrant/www/%-2+/public"
    ServerName vhosts.local
    ServerAlias *.local
    UseCanonicalName Off
    LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon

    ErrorLog  /var/log/apache2/local-error_log
    CustomLog /var/log/apache2/local-access_log common

    <Directory ~ "/vagrant/www/[a-z0-9_]+/public">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order Allow,Deny
        Allow from all

        <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteBase /
            RewriteCond %{REQUEST_FILENAME} -s [OR]
            RewriteCond %{REQUEST_FILENAME} -l [OR]
            RewriteCond %{REQUEST_FILENAME} -d
            RewriteRule ^.*$ - [NC,L]
            RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
            RewriteRule ^(.*) - [E=BASE:%1]
            RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
        </IfModule>
    </Directory>
</Virtualhost>

Friday, November 1, 2013

Thursday, August 22, 2013

REPOST: Setting up PHP to connect to a MS SQL Server using PDO_ODBC in Ubuntu

For those who are not familiar of ODBC like I was, its basically an API that lets you connect to other databases. PDO has its ODBC driver which in turn ODBC has its own also. To connect to MS SQL Server, we are using FreeTDS which is a library that lets you accomplish this.

sudo apt-get install freetds-bin freetds-common tdsodbc odbcinst php5-odbc unixodbc
sudo cp /usr/share/tdsodbc/odbcinst.ini /etc/
sudo apache2ctl restart

Connect using PDO in PHP:
$c = new PDO('odbc:Driver=FreeTDS; Server=hostname_or_ip; Port=port; Database=database_name; UID=username; PWD=password;');
Note: You will not see any reference of FreeTDS (the odbc driver) in the ODBC section of your PHP Info so don't weird out.

Reference:
https://secure.kitserve.org.uk/content/accessing-microsoft-sql-server-php-ubuntu-using-pdo-odbc-and-freetds

Tuesday, August 13, 2013

REPOST: Show and Compare Committed Files Using LOG and DIFF in Subversion

Show commited files for revision 63.

Syntax:
svn log --verbose -r <rev>
Example:
svn log --verbose -r 63

Show difference by comparing actual files. For example, show line by line difference between revision 64 against 63.

Syntax:
svn diff -r<rev-of-commit>:<rev-of-commit - 1>
Example:
svn diff -r64:63

Show the actual file version at the specific revision number:

Syntax:
svn cat -r <rev> <file> | less
Example 
svn cat -r 64 ./some_file.php | less

Reference:
http://stackoverflow.com/questions/6296284/svn-list-files-committed-for-a-revision

Friday, July 5, 2013

Mac vagrant port forwarding

Just reposting this blog for reference.

http://www.dmuth.org/node/1404/web-development-port-80-and-443-vagrant

If you have a vagrant instance that is running on 8080 for web services and you want to forward 80 traffic to it so you will not be appending ports to your url like http://localhost:8080 but instead just use http://localhost do the following bellow:

sudo ipfw add 100 fwd 127.0.0.1,8080 tcp from any to me 80
sudo ipfw add 101 fwd 127.0.0.1,8443 tcp from any to me 443

Monday, April 15, 2013

NOTE: SCP syntax

scp -rp ./copy-me/* franz@192.168.1.1:/path/to/location/copy/

Friday, July 1, 2011

Installing Apache 2.2.19 with SSL from source in Ubuntu

Before installing Apache 2, we need to install OpenSSL.

1.) Download OpenSSL from http://www.openssl.org/source/openssl-1.0.0d.tar.gz:
shell> wget http://www.openssl.org/source/openssl-1.0.0d.tar.gz

2.) You want to at least verify the md5 checksum of the openssl tar source and compare the md5 hash string with the one provided in the Apache web site (for this version: http://www.openssl.org/source/openssl-1.0.0d.tar.gz.md5) OR better yet use GnuPG:
shell> md5sum openssl-1.0.0d.tar.gz
shell> 40b6ea380cc8a5bf9734c2f8bf7e701e  openssl-1.0.0d.tar.gz ;: now compare the md5

3.) If everything is well, untar and install:
shell> tar xzvf openssl-1.0.0d.tar.gz
shell> cd openssl-1.0.0d
shell> ./config --prefix=/usr/local/lib/openssl -fPIC

NOTES: I have to use -fPIC for some reason or the configure will keep generating an error but try first without it.

4.) Make a soft link of the bin/openssl
shell> sudo ln -s /usr/local/lib/openssl/bin/openssl /usr/local/bin/

Now we need to create a private key and a certificate.
5.) Lets create our server's private key:
shell> openssl genrsa -des3 -out server.key 1024

6.) Now lets create a Certificate Signing Request (CSR) and sign it using our private key (server.key):
shell> openssl req -new -key server.key -out server.csr

NOTE: You usually send the CSR to a CA or Certificate Authority such as Verisign (Im clueless, never done it) but for now we cannot wait and we want to test! We can act as our OWN Certificate Authority and sign our CSR. This will not be valid for browsers and will tell you the Certificate is not from a valid authority.

7.) Lets use our server's private key (server.key) to sign our Certificate Signing Request (CSR) or you can create another private key acting as a Certificate Authority (CA). From there we can sign and create our own CRT (Certificate):
shell> openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Now we have a Private key (server.key) and a signed certificate (server.crt)

8.) Lets add this public key and certificate to a location we can remember:
shell> mv server.key /usr/local/ssl/privatekeys/
shell> mv server.crt /usr/local/ssl/certificates/

We can start installing Apache 2.

9.) Download Apache 2.
shell> wget http://www.takeyellow.com/apachemirror//httpd/httpd-2.2.19.tar.gz
shell> tar xzvf httpd-2.2.19.tar.gz

10.) Before installing, lets compare the md5 checksum of our package to the ones from Apache.org so we can have a little peace of mind that the integrity of the source files has not been compromised:
shell> md5sum httpd-2.2.19.tar.gz
shell> e9f5453e1e4d7aeb0e7ec7184c6784b5 httpd-2.2.19.tar.gz
 *Compare that md5 with this: http://www.apache.org/dist/httpd/httpd-2.2.19.tar.gz.md5

If everything seems to look right, lets proceed with the installation

11.)  Install Apache 2 and enable SSL support:
shell> cd httpd-2.2.19
shell>sudo ./configure --prefix=/usr/local/lib/apache2 --enable-mods-shared=most --enable-ssl --with-ssl=/usr/local/lib/openssl
shell> sudo make
shell> sudo make install

12.) If everything went smooth, lets add our private key and certificate to Apache:
shell> sudo vim /usr/local/lib/apache2/conf/extra/httpd-ssl.conf

13.) Look for the line SSLCertificateFile "/usr/local/ssl/server.crt" and SSLCertificateKeyFile "/usr/local/ssl/server.key" and replace it with your proper certificate and key location

Mine will be:
SSLCertificateFile "/usr/local/ssl/certificates/server.crt"
SSLCertificateKeyFile "/usr/local/ssl/privatekey/server.key"

14.) Go to your Apache's httpd.conf and uncomment the include for httpd-ssl.conf
shell> sudo vim /usr/local/lib/apache2/conf/httpd.conf

15.) Make a soft link of apachectl then start Apache:
shell> sudo ln -s /usr/local/lib/apache2/bin/apachectl /usr/local/bin/
shell> sudo apachectl start

IMPORTANT: The above steps are just fast walk through and did not consider any security concerns in setting up Apache. Please research further on how to secure your web server efficiently.

Friday, June 3, 2011

TIP: Useful Linux Network Monitoring Commands

Ports that are t=tcp u=udp l=listening p=program n=numeric
$> netstat -tulpn

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN      2005/smbd
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2442/apache2
tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      2323/vsftpd


List of open files and program that opens them. This one looks at specific malicious port 667:
$> sudo lsof -i tcp:667

COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
darkstat 1629 nobody    8u  IPv4   6054      0t0  TCP *:667 (LISTEN)

Thursday, June 2, 2011

Accessing Remote Resources or LAN via PPTP VPN in Ubuntu

I have a problem where I can establish a connection to my VPN server but cant access remote resources or my LAN except for the VPN server.

To solve this issue assuming you can already connect to your VPN:

$> cat /proc/sys/net/ipv4/ip_forward
If the commmand returns 0 then:

$> sudo bash -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
$> cat /proc/sys/net/ipv4/ip_forward
Should return 1 now.

* To make it permanent edit:
/etc/sysctl.conf and uncomment the line: net.ipv4.ip_forward=1

Props to: http://bizante.com/2010/01/pptp-vpn-server-on-ubuntu-using-webmin/
for the fix but that alone didn't do for me, I need to configure the IP routing for PPTP.

Continuing...You need to configure your PPTP settings:
$> sudo vim /etc/pptpd.conf

Edit from the recommended:
localip 192.168.0.234-238,192.168.0.245 
remoteip 192.168.1.234-238,192.168.1.245

To:
localip 192.168.1.100
remoteip 192.168.1.234-238,192.168.1.245


NOTES:
My LAN's subnet mask is 255.255.255.0 from 192.168.1.1. The PPTP local server's (192.168.1.100) subnet will be 255.255.255.255 and the remote IPs will start from 192.168.1.234 which is also 255.255.255.255 according to my configurations. I do not know how to change the subnet for the VPN so it would be nice to learn that. I tried prepending the subnet as a CIDR notation like 192.169.1.100/24 but doesnt seem to work.

In any case, by changing from 192.168.0.100(default) to 192.168.1.100 (notice the "1") the local LAN being requested from the client VPN should forward now. I chose a high number 100 to avoid collision with my LAN so just be aware of that if you have tons of computer connecting to your network!


This may not work for others but it worked for me so just my 1 cent.

Friday, April 29, 2011

Notes: Rsync syntax SSH to another port

Rsync to another port (port:555) using SSH:
$> rsync -avh --progress --rsh='ssh -p555' /my/path/ username@192.168.1.1:/my/path/

Wednesday, April 27, 2011

TIP: ssh to another port

$ ssh -p 500 192.xxx.xx.xx

Tuesday, April 19, 2011

NOTES: soft linking

I tend to forget and get confused with $> ln syntax not being used for awhile.
To use it say you want to link /usr/local/lib/php cli to /usr/bin

$>    cd /usr/bin
                TARGET           LINK NAME
$>    ln -s /usr/local/lib/php php

Friday, April 15, 2011

Ubuntu PHP 5.3.6 installation from source in a system with existing PHP 5.2 and Apache 2

I have been contemplating on installing PHP 5.3 on my Ubuntu box since it takes awhile for Ubuntu to upgrade their APT packages but Im afraid of messing around with the install cause i got important live web files in my server and don't want to ruin the perfectly working PHP 5.2 version.

In either case, I finally summoned the guts to install PHP 5.3 from source and bit the bullet. I did it from source because I dont want to depend on the Ubuntu packages on PHP anymore and want the ability to upgrade easily in the next major PHP release.

1.) So I downloaded PHP 5.3.6 from php.net and did all the untarring.
$> wget http://www.php.net/get/php-5.3.6.tar.gz/from/a/mirror
$> tar -xzvf php-5.3.6.tar.gz

2.) Then I have a bunch of library dependencies that I need to install. Here apt-get works a charm.
$> apt-get install libxml2-dev
$> apt-get install libbz2-dev
$> apt-get install libcurl4-openssl-dev
$> apt-get install libjpeg-dev
$> apt-get install libpng12-dev
$> apt-get install libXpm-dev
$> apt-get install libfont-freetype-perl
$> apt-get install libfreetype6-dev
$> apt-get install libxslt-dev

I got a little bit more than the above library dependencies but their pretty easy to install with apt-get so I didn't bother to document them.

*For those who are new to compiling, note that the above packages are depending upon your build. I have to install the libraries above because if you look at my build configure below, I enabled this feature in PHP. If you do not know what libraries you are required to have then just run configure with your build options, then the script will halt if it cannot find the libraries. From there you can see your requirements although this takes time especially if you are enabling a bunch of feature. There also maybe much more efficient way in doing this though.

Another problem I cannot figure out was the --with-snmp, I keep getting the
error: SNMP sanity check failed.
My solution was to remove it from the build for now since I do not use. Feel free to suggest a solution though.

UPDATE: I solved this problem by installing package libsnmp-dev.
Look here in my blog for details: http://kelmadics.blogspot.com/2011/06/php-install-problems-with-snmp.html

TIP: One command I use if apt cannot find the package is search for it, for example:
apt-cache search freetype

Also, since I installed Apache differently before, the --with-apxs2=PATH will not work. I have to remove the threaded version and install Apache prefork:
$> sudo apt-get remove apache2-threaded-dev
$> sudo apt-get install apache2-prefork-dev

3.) Build time! Here is what I used to build:
$> cd php-5.3.6
$> ./configure \
--prefix=/usr/local/lib/php5.3.6 \
--with-apxs2=/usr/bin/apxs2 \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-pear=/usr/local/lib/php5.3.6/lib/php \
--with-tidy \
--with-curl \
--with-curlwrappers \
--with-openssl-dir \
--with-xpm-dir \
--with-pdo-mysql \
--with-xsl \
--with-ldap \
--with-xmlrpc \
--with-iconv-dir \
--with-bz2 \
--with-mcrypt \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--with-gd \
--with-snmp \
--enable-mbstring \
--enable-zip \
--enable-exif \
--enable-calendar


A thing to note here. I used --prefex=PATH to install it from a different location since I already have an existing PHP 5.2 and don't want to overwrite anything.

4.) Ok, finally done with the build, time for "make & make install".
$> make
$> make install

Hit a minor snag though. I got this error:
chmod 755 /usr/lib/apache2/modules/libphp5.so
apxs:Error: Activation failed for custom /etc/apache2/httpd.conf
file..
apxs:Error: At least one `LoadModule' directive already has to exist..
make: *** [install-sapi] Error 1 

Quick googling and the fix is just adding a dummy place holder on whichever file it is looking for. In the case above it is httpd.conf so just add or append in httpd.conf the following code:
# Dummy LoadModule directive to aid module installations
#LoadModule dummy_module /usr/lib/apache2/modules/mod_dummy.so


Take note of the "#". That is included!

Reference: Here's where I got the solution from for the above problem: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=231134

Please note that doing "make install" will overwrite your php5 module in /usr/lib/apache2/modules/libphp5.so so if you want to have a backup do it BEFORE you do "make install"

After adding the dummy text, do make install again.
$> make install

5.) Optional. After that I copied my existing php.ini from /etc/apache2/php5 to where ever I installed my php5.3 library. In my case this is /usr/local/lib/php5.3.6/lib
$> cp /etc/apache2/php5/php.ini /usr/local/lib/php5.3.6/php.ini

6.) Restart apache and you have php5.3 installed. I didn't even have down time!
$> sudo apache2ctl restart

IMPORTANT: One more final note since we placed a dummy module line in httpd.conf, this caused
the install to write a line: LoadModule php5_module /usr/lib/apache2/modules/libphp5.so
in httpd.conf but since you may already have this line in /etc/apache2/mods-available/php5.load if you  have Apache installed already through aptitude, restarting apache will warn that you have a duplicate module!

Solution is to simply remove the extra generated line from httpd.conf and everything should work!!!

Friday, March 4, 2011

Tip: search word in a file then print result

This will search for a whole word (-w) in a file, print the filename (-H) and print the line number (-n)
find . -exec grep -nwH 'searchString' {} \;

Wednesday, March 2, 2011

Tip: Linux terminal freeze when pressing Ctrl + S

Sometimes I accidentally press CTRL + S in a linux editor such as VI. This causes the terminal to freeze or lock.
To unfreeze you need to press CTRL + Q

Wednesday, February 16, 2011

Tip: Linux find remove logs

remove them logs:
find . -path "*.log.*.gz" -exec rm '{}' \;