Set Permanently ulimit -n / open files in ubuntu

Few days ago, my golang app in development server halted, then i check app’s log.

2018/03/26 10:06:56.373 [server.go:2921] [HTTP] http: Accept error: accept tcp [::]:80: accept4: too many open files; retrying in 1s

so, i check ulimit in my server, got open files configuration is default, 1024. Then i try set to 65535 which is that configuration is standart configuration in production server to avoid that error. Just typing as following :

# set limit
user@ubuntu:~$ ulimit -n 65535

When i relogin, i check again open files configuration, got 1024 or back to default. So, i want to set that new configuration (65535) permanently in my server. You can do it with following steps below :

# available limit
user@ubuntu:~$ ulimit -n
1024

# To increase the available limit to say 65535
user@ubuntu:~$ sudo vim /etc/sysctl.conf

# add the following line to it
fs.file-max = 65535

# run this to refresh with new config
user@ubuntu:~$ sudo sysctl -p

if you got error “sysctl: permission denied on key ‘fs.file-max’”, you can skip that step, then you doing steps as below :

# edit the following file
user@ubuntu:~$ sudo vim /etc/security/limits.conf

# add following lines to it
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
root soft nproc 65535
root hard nproc 65535
root soft nofile 65535
root hard nofile 65535

# edit the following file
user@ubuntu:~$ sudo vim /etc/pam.d/common-session

# add this line to it
session required pam_limits.so

# logout and login and try the following command
user@ubuntu:~$ ulimit -n
65535