Notes on setting up a server.

I’m setting up a new Ubuntu server and while most of the defaults are fine, there are some things that I need to adjust. I have a very shallow understanding of this stuff, so there could be better and more secure ways to do this, but this works for me.

Disallow access to PHP include files

There isn’t any reason that people need to see the include files that I use in my websites. You could name them .inc.php so that the raw code isn’t available, but that’s not very elegant, and outsiders can still access the file. There isn’t anything particularly sensitive in them, but by themselves, they don’t display correctly. So I added a few lines to my /etc/apache2/apache2.conf file. Just below the section that disallows viewing .htacess files.


#
# The following lines prevent .htaccess and .htpasswd files from being 
# viewed by Web clients. 
#
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy all
</Files>

# The following lines prevent .inc files from being
# viewed by Web clients.
#
<Files ~ "\.inc$">
    Order allow,deny
    Deny from all
</Files>
#

Prevent directory browsing

If you have a bunch of images in a directory, then anyone who wants can view all of them just by looking at the web page source and putting the directory name after your URL. I’d rather they not do that, so I restrict listing of the files by adding this line to my /etc/apache2/httpd.conf file. On my default Ubuntu install this file is empty.


Options Includes FollowSymLinks MultiViews

Restart Apache for the changes to take effect.

Alternate method to prevent directory browsing

If you want to prevent directory browsing in just one directory and either don’t want to change the whole site or don’t have access to the files named above, add this line to your .htaccess file.


Options -Indexes

Probably don’t have to restart Apache for changes to take effect.

Prevent Directory Browsing on a Per Site Basis

Changing the httpd.conf file will change the behavior of all sites on your server. If you want to change the behavior of just one site, edit its file in /etc/apache2/sites-avalilable. Find the line that has Options FollowSymLinks in it and if it has Indexes in it, delete it. This is what the default Ubuntu install has.


  <Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
  </Directory>

Probably do have to restart Apache for changes to take effect.

Prevent access to your include directory

Add this to your site’s file in /etc/apache2/sites-avalilable.


#<Directory /www/MySite/include.php>
#    Deny from all
#</Directory>

Show an error document instead of the default 404 error

Create a normal php document with your sites navigation and a message that says the file can’t be found and maybe you can find it with the nav menus. Add this to your site’s file in /etc/apache2/sites-avalilable. And while you are at it, there is no reason you need to tell anyone that they don’t have permission to see a particular file, just tell them it’s not found, so add the same line for a 403 error. I take them back to the main page and display the missing file in the main page.


ErrorDocument 404 /index.php?p=missing
ErrorDocument 403 /index.php?p=missing

Removing subviews from a view.

In my apps, I use swipe gestures to move to the next screen. The old stuff is animated off to the left and the new stuff animates in from the right. If the user rotates the device, I need to remove the elements of the view from the main view and redisplay them in the new orientation. I also need to remove the animations if they are occurring. I have lots of different layouts, but each layout has this method in it. (The contents vary a bit.)

I first remove the animations. Then I remove the views from the main view. Then I set the views to nil to make sure their resources are released.


- (void)removeAllObjectsFromParentView {
    [self.wordView.layer removeAllAnimations];
    [self.categoryView.layer removeAllAnimations];
    
    [self.wordView removeFromSuperview];
    [self.categoryView removeFromSuperview];
   
    self.wordView = nil;
    self.categoryView = nil;
}

All of the subviews of wordView and categoryView will be removed and released when the parent view is removed and released.

Pick a random color in iOS

In one of my methods I wanted to put randomly colored text on the screen. I put this class method in my Utilities.m class.


#define ARC4RANDOM_MAX      0x100000000
+ (UIColor *) randomColor {
    CGFloat red =  (CGFloat)arc4random()/ARC4RANDOM_MAX;
    CGFloat blue = (CGFloat)arc4random()/ARC4RANDOM_MAX;
    CGFloat green = (CGFloat)arc4random()/ARC4RANDOM_MAX;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

To set the color, just call


label.textColor = [Utilities randomColor];

Lazy Instantiation in iOS

I had a bunch of code like this in the init for my first view controller. Paul Hegerty had mentioned lazy instantiation in his Stanford CS193 courses. And the boilerplate code in the AppDelegate for creating Managed Object Contexts and Persistent Store Coordinator uses it a log. So I understood the concept, but I hadn’t used it for my own globals. After listening to his most recent class, I decided to convert all of my init code to lazy instantiation.

One benefit of lazy instantiation is that you don’t allocate resources until you need to use the object. In my case, a better reason is that I’m not cluttering up my view controller with code that initializes global variables. In the Model, View, Controller design pattern, initialization code really doesn’t belong in the controller. But even more important, since I check for initialization in the class that creates the variable, I can’t forget to initialize the variable.

Old Code


    if (![Globals sharedInstance].showmePict ) {
        [Globals sharedInstance].showmePict = @"Either";
    }
    
    if (![Globals sharedInstance].targetSoundDelay ) {
        [[Globals sharedInstance] resetTargetDelay:TARGET_SOUND_DELAY];
    }
    

    if ( !([Globals sharedInstance].targetSound) ){
#ifdef SHOWME_TARGET_SOUND
        [[Globals sharedInstance] resetTargetSound:SHOWME_TARGET_SOUND];
#endif
    
#ifndef SHOWME_TARGET_SOUND
        [[Globals sharedInstance] resetTargetSound:@""];
#endif
    }

New Code

In the singleton for globals.


- (NSString *)showmePict {
    
    if (!_showmePict ) _showmePict = @"Either";
    return _showmePict;
}

- (NSUInteger)targetSoundDelay {
    
    if ( !_targetSoundDelay ) [self resetTargetDelay:TARGET_SOUND_DELAY];
    return _targetSoundDelay;
}

- (NSString *)targetSound {
    
    if ( !_targetSound ){
    #ifdef SHOWME_TARGET_SOUND
        [self resetTargetSound:SHOWME_TARGET_SOUND];
    #endif
        
    #ifndef SHOWME_TARGET_SOUND
        [self resetTargetSound:@""];
    #endif
    }
    return _targetSound;
}

hdiutil

I recently started selling my software on Gumroad. For the Windows side, we just zipped up the .exe installer and uploaded it. For the Mac side, we first made a disk image where we laid out the files and then compressed them. You can do this with Disk Utility, but it’s a lot easier to do it on the command line.

The first thing you should do is put all the files you want to distribute into a folder. Then get info on the folder to see how much room you will need for your disk image. Round up a little. In the example below, I’m creating an image with 20MB. “ ProductName” is the name of the software, e.g. Match Ups!, or Train Time.


hdiutil create -megabytes 20 -fs HFS+ -volname ProductName ProductName.dmg

Once you have everything laid out how you want it, compress the image.


hdiutil convert -format UDZO ProductName.dmg -o ImageForDistribution.dmg

Where UDZO – UDIF zlib-compressed image. You can rename ImageForDistribution.dmg to anything you want.