My boss had split the web server and DB. The error message "504 Gateway Timeout" no longer appears. The problem should be partially solved.
It is believed that the root cause of the problem is due to the successively uploading of image files while the upload of previous one had not yet completed. This leads to concurrent http POST request, causing the session lock issue https://moodle.org/mod/forum/discuss.php?d=419645, It is seems that data writing into the database is not fast enough. From Giuseppe Mandara's suggested solution is to switch to use SSD. It is not easy for us to switch to SSD at this moment. Therefore, we will not require students to upload photos of answer scripts in the future.
Another part of the issue is how to maximize the number of concurrent users to be supported. As the apache2buddy shows the apache process size is as large as 317MB, the actual MaxRequestWorkers value = database size/apache process size =about 120. Merely raising the MaxRequestWorkers value to 1024 can only raise the ceiling, would not help. The solution is to reduce the apache process size or adding more physical memory.