Webserver for OSX Tutorial

Description

This tutorial is will show how to set up an apache server for web development which allows for sites to be written in any language and framework. This is tailored for Mac OS X and is meant for developers who write websites in multiple languages and frameworks. This tutorial starts by setting up apache, languages and framework followed by the site setup. The site setup is a website that allows browsing of web directories for viewing.

Preview for web development platform

Minimum Requirements

  • php 5.0
  • apache 2.0
  • gems 1.3

Setup

OSX and Apache Configuration

First go to System Preferences -> Sharing and click on Web Sharing which allows for apache to be turned on. The “Computer Name” will be the name entered into a web browser in order to all developing sites.

Sharing Window

Next open /etc/apache2/http.conf and edit the following lines:

DocumentRoot "/Library/WebServer/Documents"
<IfModule dir_module>
  DirectoryIndex index.html
</IfModule>

and change them to:

DocumentRoot "/Users/<username>/websites"
<IfModule dir_module>
  DirectoryIndex index.html index.cgi index.pl index.php index.xhtml
</IfModule>

My “Computer Name” is argonath, but this should be the “Computer Name” chosen earlier.

255.255.255.255	broadcasthost
::1             localhost 
fe80::1%lo0	localhost
127.0.0.1 argonath

Site Configuration

cd /Users/
git clone http://www.zaphinath.com/git/webdevosx.git websites

Then open the browser of your choice and go to the address you chose in the setting up of apache. In my example I go to argonath/ and it will load the web development platform.

Ruby on Rails Configuration

Macport and XCode Tools should both be installed and configured in order to install ruby on rails. I found I had to add a symbolic link to the mysql5 mysqld.sock file because MacPorts puts it in /opt/local and therefore it couldn’t be found at /tmp/mysql.sock.

sudo port install mysql5 +server
sudo port install ruby
sudo port install rb-rubygems
sudo port install rb-termios
sudo port install rb-mysql
sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock

sudo gem update --system
sudo gem install -y rake
sudo gem install rails --include-dependencies
sudo gem install passenger
sudo passenger-install-apache2-module

sudo bundle install

Symfony Configuration

Go to http://symfony.com/download and download the latest version of the Symfony Framework. After the file is unpacked move it to the website directory. This directory is a blank template of the Symfony Project. For every Symfony-php project made just simply copy the Symfony directory into the directory of the new project.

mv ~/Downloads/Symfony ~/websites/Symfony/Symfony
cp ~/websites/Symfony/Symfony ~/websites/Symfony/

Coldfusion Configuration

Python Ctypes for Advanced Fortran and C++

Problem

The cytpes library in python 2.6 and greater allows python to call fortran, c, and c++ functions from a specially compiled library. However the python cytpes library is limited in the datatypes that can be called from those libraries. This tutorial will show how to call derived data types from a fortran library and structs in a c++ in python.

Ctypes Fundamental Data Types

Here is a list of data types that can be passed through the ctypes library. For more information on ctypes please visit the python cytpes tutorial.

ctypes type C type Python type
c_char char 1-character
string
c_wchar wchar_t 1-character
unicode string
c_byte char int/long
c_ubyte unsigned char int/long
c_short short int/long
c_ushort unsigned short int/long
c_int int int/long
c_uint unsigned int int/long
c_long long int/long
c_ulong unsigned long int/long
c_longlong __int64 or
long long
int/long
c_ulonglong unsigned __int64 or
unsigned long long
int/long
c_float float float
c_double double float
c_char_p char *
(NUL terminated)
string or
None
c_wchar_p wchar_t *
(NUL terminated)
unicode or
None
c_void_p void * int/long
or None

Ctypes in python does not support booleans, derived data types, objects, structs, and other commonly used data types.

Solutions

 

Derived Data Type

ddt.f90

module ddt
implicit none

type rect !Name of data type
  real :: height
  real :: length
end type rect

contains

subroutine calc_area(rc)
type(rect) :: rc
real area, h, l

h = rc%height
l = rc%length

area = h*l
print *, "The area is", area

end subroutine

end module

[bash]gfortran -fpic -c ddt.f90[/bash]

Because there is no ctype for derived data types in fortran we get around this by making an extra module in fortran that will have subroutines that we will call in python.

modtest.f90

module modtest
use ddt

contains

subroutine pcall_area
  type(rect) :: rc
  rc%height = 3.5
  rc%length = 2.5
  call calc_area(rc)
end subroutine

end module modtest

[bash]gfortran -fpic -c modtest.f90[/bash]

Now we compile a library lib.so from which our python code will call.

[bash]gfortran -fpic -shared modtest.o ddt.o -o lib.so[/bash]

Now we want to see a list of functions and subroutines we can call in our python script from the compiled library lib.so

[bash]nm lib.so | less[/bash]
[text]
0000000000000f0e T ___ddt_MOD_calc_area
0000000000000f3a T ___modtest_MOD_pcall_area
0000000000000f08 t __dyld_func_lookup
0000000000000000 t __mh_dylib_header
U dyld_stub_binder
0000000000000ef4 t dyld_stub_binding_helper
[/text]

We want to call ___modtest_MOD_pcall_area in our python script. In order to do this we need to remove one of the underscores in the front as shown:

[python firstline=”1″]
#!/usr/bin/python
#Filename: foobar.py
#Author: Derek Carr
#Website: http://www.zaphinath.com

from ctypes import *

libtest = cdll.LoadLibrary("./lib.so")

method = libtest.__modtest_MOD_pcall_area

try:
method()
print "Passed"
except:
print "Failed"
traceback.print_exc()
sys.exit()
[/python]

[bash]
chmod 777 foobar.py
./foobar.py
The area is 8.7500000
Passed
[/bash]

Now we have successfully called a subroutine from fortran that uses a derived data type or defined data type and used that subroutine in python.

Struct in C++

Line Wrap With SyntaxHighlight

Problem:

Syntax Highlighter has a problem where long lines of code use a horizontal scroll bar instead of using a line break or word wrap.

Example Before:

[bash wraplines=”false”]sql_query = SELECT page_id, page_title, page_namespace, page_is_redirect, old_id, old_text FROM vn_page, vn_revision, vn_text WHERE rev_id=page_latest AND old_id=rev_text_id[/bash]

Solution:

If using wordpress install a plugin called ‘SyntaxHighlighter Evolved’ and after the plugin is activated go to the settings toolbar and select “SyntaxHighlighter.” Note: You must be using version 2.x of SyntaxHighlighter in order to have line wrapping work.

SyntaxHighlighter Options

Example After:

[bash]sql_query = SELECT page_id, page_title, page_namespace, page_is_redirect, old_id, old_text FROM vn_page, vn_revision, vn_text WHERE rev_id=page_latest AND old_id=rev_text_id[/bash]

Spinx.conf for mediawiki

Problem:

[bash]ERROR: index ‘wiki_main’: sql_query: Table ‘db.page’ doesn’t exist (DSN=mysql://:***@localhost:3306/db)’[/bash]
[bash]ERROR: index ‘wiki_incremental’: sql_query: Table ‘db.page’ doesn’t exist (DSN=mysql://:***@localhost:3306/db)[/bash]

Solution:

This comes because in the database the tables being queried have a different name. The name was changed at the creation of the database. An example is the normal table for wiki is ‘page’ but now its ‘your_defined_prefix_page’. You will need to edit the sphinx.conf to call the right database tables. This should be fairly easy.

Before

[bash]# main document fetch query – change the table names if you are using a prefix
sql_query = SELECT page_id, page_title, page_namespace, page_is_redirect, old_id, old_text FROM page, revision, text WHERE rev_id=page_latest AND old_id=rev_text_id[/bash]

After

[bash]# main document fetch query – change the table names if you are using a prefix
sql_query = SELECT page_id, page_title, page_namespace, page_is_redirect, old_id, old_text FROM vn_page, vn_revision, vn_text WHERE rev_id=page_latest AND old_id=rev_text_id[/bash]