Fresh Server Setup with Nginx, Passenger and Rails
Whenever you get a fresh server to setup it takes too much time to setup everything and sometimes you miss the essential task to perform on the server so for that this blog can help you.
Once you are done with the creating the EC2 instance you'll get a pem file using that file login to the server.
Login to server:
chmod 400 /path_to_pem/you_pem_file_name.pem
ssh -i /path_to_pem/you_pem_file_name.pem username@ec2-some_ip_seperated_by_(-)_(-).compute-1.amazonaws.com
Note: username is the name of root user for ubuntu machine it “ubuntu” and for AMI(Amazon Machine Instances) is “ec2”.
If you are successfully logged in to the server please do the followings:
Add user(deploy)
Now you need to add a user deploy in the server for deployment
sudo -i
export EDITOR=vim
echo "export EDITOR=vim" >/etc/profile.d/editor.sh
apt-get update
useradd -d /home/deploy -m deploy
passwd deploy
apt-get install vim
visudo
deploy ALL=(ALL:ALL) ALL
su deploy
Setup SSH
Once you are done with creating a new user deploy and successfully logged in with that user please setup the ssh login.
Generate ssh keys on the server:
ssh-keygen -t rsa -b 4096 -C "serverip@domain.com"
Copy your local machine ssh key and paste it in authorized_keys file
cat ~/.ssh/authorized_keys
or from your local machine do this
ssh-copy-id deploy@123.45.56.78
Change your ~/.ssh/config by adding following:
Host serverHostName <ip_address>User <user>IdentityFile ~/.ssh/id_rsa.pub
Now edit file /etc/ssh/sshd_config to proper configuration of ssh:
PermitRootLogin noPubkeyAuthentication yesAuthorizedKeysFile %h/.ssh/authorized_keysPasswordAuthentication noTCPKeepAlive yesClientAliveInterval 60ClientAliveCountMax 10UsePAM no
Restart the ssh service
/etc/init.d/ssh restartorservice ssh reload
/bin/bash --loginplease run this command to avoid using /bin/bash --login again and again after login to the server.
sudo chsh <username> -s /bin/bash
Useful stuff for your server
echo "gem: --no-rdoc --no-ri" > ~/.gemrc
echo "export RAILS_ENV=production" >> ~/.bashrc
Install Essential
sudo apt-get install build-essential openssl curl git-core zlibc zlib1g zlib1g-dev libssl-dev libcurl4-openssl-dev imagemagick libmagickwand-dev libyaml-devlibsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison libapr1-dev libaprutil1-dev libreadline6 libreadline6-dev openssh-serveropenssh-client libffi-dev libgdbm-dev nodejs libmysqlclient-dev
Install mysql
sudo apt-get install mysql-server
Install RVM with Ruby
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -sSL https://get.rvm.io | bash -s stable --rubysource /home/deploy/.rvm/scripts/rvmecho "source $HOME/.rvm/scripts/rvm" >> ~/.bash_profileecho "source $HOME/.rvm/scripts/rvm" >> ~/.bashrcrvm install ruby-2.3.1
Install ruby rails globally
sudo apt-get install ruby-bundlersudo apt-get install ruby-railties
Install Passenger + Nginx
sudo apt-get install -y dirmngr gnupgsudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7sudo apt-get install -y apt-transport-https ca-certificatessudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger xenial main > /etc/apt/sources.list.d/passenger.list'sudo apt-get updatesudo apt-get install -y nginx-extras passenger
Nginx Setup
Properly setting up and configuring the Web Server is totally depend upon your application requirement what I have shown you guys it the basic setup of Nginx
remove comment from # include /etc/nginx/passenger.conf; in file /etc/nginx/nginx.conf
Create /etc/nginx/sites-available/example_server.conf
put these lines of code in example_server.conf file
Then you need to create the symlink of example_server.conf file in the sites-enabled folderserver {listen 80 default_server;server_name onepermission.com www.onepermission.com;rails_env production;set $redirect_to_https 0;if ($http_x_forwarded_proto != 'https') {set $redirect_to_https 1;}if ($request_uri = '/health') {set $redirect_to_https 0;}if ($redirect_to_https = 1) {rewrite ^ https://$host$request_uri? permanent;}# Tell Nginx and Passenger where your app's 'public' directory isroot /home/deploy/one_permission/current/public;# Turn on Passengerpassenger_enabled on;passenger_ruby /home/deploy/.rvm/gems/ruby-2.3.1/wrappers/ruby;}
sudo ln -s /etc/nginx/sites-available/example_server.conf /etc/nginx/sites-enabled/example_server
Passenger Setup
run this command and it will give
passenger-config about ruby-command
copy what after command to passenger_ruby in /etc/nginx/sites-available/example_server.conf
example: /home/deploy/.rvm/gems/ruby-2.3.1/wrappers/ruby
Restart Nginx Server
sudo service nginx restart
Comments
Post a Comment