Solved! Go to Solution.
When publishing to server, certain variables are substituted for g_ESRI_variable_x and declared at the top of the script. Not sure why server needs to do this - some of the variables in my scripts are just field names, like g_ESRI_variable_4 = u'F_AREA'. You can see the amended script in the arcgissystem folder on the server, e.g. D:\arcgisserver\directories\arcgissystem\arcgisinput\LFP\lfp01Setup.GPServer\extracted\v101\_python\LFP_01_Setup.py
I think the problem in your script may be to do with the current path. The help doc says that the Layer constructor needs
A string that includes the full path and file name of an existing layer (.lyr) file.
Your script doesn't contain a path, so maybe the working folder is different when running on the server. Try specifying the full path for "layer" in lines 3 and 4.