Unified Power Flow Controller (UPFC) is one of the most effective Flexible AC Transmission Systems (FACTS) devices for enhancing power system security. However, to what extent the performance of UPFC can be brought out, it highly depends upon the location and the parameters setting of this device in the system. This paper presents an approach to find out the optimal placement and the optimal parameters setting of UPFC for enhancing power system security under single contingencies (N-1 Contingency). Firstly, we perform a contingency analysis and ranking process to determine the severest line tripping contingencies considering line overloads and bus voltage violations as a performance index. Secondly, we apply Genetic Algorithm (GA) and Particle Swarm Optimization (PSO) techniques to find out the optimal location and the optimal parameters setting of UPFC corresponding to the determined contingencies scenarios. To verify our proposed approach, we perform simulations on an IEEE 6-bus and an IEEE 14-bus power systems. The results we've obtained indicate that GA and PSO can successfully achieve the proper settings and placement of UPFC. Installing UPFC in such location can significantly enhance the security of power system by eliminating or minimizing the overloaded lines and the bus voltage violations.