configurando servidor ubuntu 13.04 + nginx + rails + mysql do zero
Autor: Pedro Fausto | Publicado: 2013-06-13T19:53:00-07:00



1- Criando usuário.
Digite o comando '&nbsp;adduser ubuntu ' &nbsp;isso criara um usuário chamado ubuntu.



2-&nbsp;Dando permissões de root para o usuário ubuntu.

Digite o comando '&nbsp;visudo&nbsp;'.


=====&gt; Colar no fim do arquivo:&nbsp;

ubuntu &nbsp; &nbsp; ALL=NOPASSWD: ALL

=====&gt; Ctrl+X

=====&gt; Y + Enter


Pronto, o usuario ubuntu esta com permissoes de root. Saia do root e entre com o novo usuario.


3- Criando ssh-key publica/privada


Digite '&nbsp;ssh-keygen -t rsa&nbsp;'. agora adicione sua ssh-key digitando '&nbsp;nano .ssh/authorized_keys '.

Depois disso você pode acessar o servidor utilizando ssh-key sem necessidade da senha.




4- Melhorando a segurança do SSH

digite &nbsp;'&nbsp;sudo nano /etc/ssh/sshd_config&nbsp;'



&nbsp;Alterar as linhas abaixo de acordo:

=====&gt; PermitRootLogin yes &nbsp; &nbsp; &nbsp; &nbsp; ===&gt; PermitRootLogin no &nbsp; &nbsp; &nbsp; &nbsp;(linha 26)



=====&gt; #PasswordAuthentication yes ===&gt; PasswordAuthentication no (linha 50)



=====&gt; X11Forwarding yes &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ===&gt; X11Forwarding no &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(linha 62)



=====&gt; AcceptEnv LANG LC_* &nbsp; &nbsp; &nbsp; &nbsp; ===&gt; # AcceptEnv LANG LC_* &nbsp; &nbsp; (linha 73)



=====&gt; UsePAM yes &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;===&gt; UsePAM no &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (linha 77)



=====&gt; Adicionar linha: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;===&gt; AllowUsers ubuntu



=====&gt; Ctrl+X

=====&gt; Y + Enter

logo apos digite &nbsp;' sudo /etc/init.d/ssh restart '

&nbsp;* Restarting OpenBSD Secure Shell server sshd [ OK ]



5- Definir o RAILS_ENV padrão.

digite ' nano .bashrc '



=====&gt; Colar no fim do arquivo

export RAILS_ENV="production"


6- Instalando pacotes necessários.

sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core \
zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf \
libc6-dev ncurses-dev automake libtool bison subversion nodejs





7- Instalando nginx.


Digite ' sudo apt-get install nginx -y ', logo apos digite ' sudo service nginx start '.
Agora vamos otimizar as configurações do nginx. Edite o arquivo '&nbsp;sudo nano /etc/nginx/nginx.conf&nbsp; '

Coloque estas configurações:


user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

worker_rlimit_nofile 8192;

events {
&nbsp; &nbsp; &nbsp; &nbsp; worker_connections 8000;
&nbsp; &nbsp; &nbsp; &nbsp; # multi_accept on;
}

http {

&nbsp; &nbsp; &nbsp; &nbsp; ##
&nbsp; &nbsp; &nbsp; &nbsp; # Basic Settings
&nbsp; &nbsp; &nbsp; &nbsp; ##

&nbsp; &nbsp; &nbsp; &nbsp; charset utf-8;

&nbsp; &nbsp; &nbsp; &nbsp; sendfile on;
&nbsp; &nbsp; &nbsp; &nbsp; tcp_nopush on;
&nbsp; &nbsp; &nbsp; &nbsp; tcp_nodelay on;
&nbsp; &nbsp; &nbsp; &nbsp; keepalive_timeout 65;
&nbsp; &nbsp; &nbsp; &nbsp; types_hash_max_size 2048;
&nbsp; &nbsp; &nbsp; &nbsp; # server_tokens off;
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;client_max_body_size 100M;
&nbsp; &nbsp; &nbsp; &nbsp; server_names_hash_bucket_size 64;
&nbsp; &nbsp; &nbsp; &nbsp; # server_name_in_redirect off;

&nbsp; &nbsp; &nbsp; &nbsp; include /etc/nginx/mime.types;
&nbsp; &nbsp; &nbsp; &nbsp; default_type application/octet-stream;

&nbsp; &nbsp; &nbsp; &nbsp; ##
&nbsp; &nbsp; &nbsp; &nbsp; # Logging Settings
&nbsp; &nbsp; &nbsp; &nbsp; ##

&nbsp; &nbsp; &nbsp; &nbsp; access_log /var/log/nginx/access.log;
&nbsp; &nbsp; &nbsp; &nbsp; error_log /var/log/nginx/error.log;

&nbsp; &nbsp; &nbsp; &nbsp; ##
&nbsp; &nbsp; &nbsp; &nbsp; # Gzip Settings
&nbsp; &nbsp; &nbsp; &nbsp; ##

&nbsp; &nbsp; &nbsp; &nbsp; gzip on;
&nbsp; &nbsp; &nbsp; &nbsp; gzip_disable "msie6";
&nbsp; &nbsp; &nbsp; &nbsp; gzip_vary on;
&nbsp; &nbsp; &nbsp; &nbsp; gzip_proxied any;
&nbsp; &nbsp; &nbsp; &nbsp; gzip_comp_level 6;
&nbsp; &nbsp; &nbsp; &nbsp; gzip_buffers 16 8k;
&nbsp; &nbsp; &nbsp; &nbsp; gzip_http_version 1.1;
&nbsp; &nbsp; &nbsp; &nbsp; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

&nbsp; &nbsp; &nbsp; &nbsp; ##
&nbsp; &nbsp; &nbsp; &nbsp; # nginx-naxsi config
&nbsp; &nbsp; &nbsp; &nbsp; ##
&nbsp; &nbsp; &nbsp; &nbsp; # Uncomment it if you installed nginx-naxsi
&nbsp; &nbsp; &nbsp; &nbsp; ##

&nbsp; &nbsp; &nbsp; &nbsp; #include /etc/nginx/naxsi_core.rules;

&nbsp; &nbsp; &nbsp; &nbsp; ##
&nbsp; &nbsp; &nbsp; &nbsp; # nginx-passenger config
&nbsp; &nbsp; &nbsp; &nbsp; ##
&nbsp; &nbsp; &nbsp; &nbsp; # Uncomment it if you installed nginx-passenger
&nbsp; &nbsp; &nbsp; &nbsp; ##

&nbsp; &nbsp; &nbsp; &nbsp; #passenger_root /usr;
&nbsp; &nbsp; &nbsp; &nbsp; #passenger_ruby /usr/bin/ruby;

&nbsp; &nbsp; &nbsp; &nbsp; ##
&nbsp; &nbsp; &nbsp; &nbsp; # Virtual Host Configs
&nbsp; &nbsp; &nbsp; &nbsp; ##

&nbsp; &nbsp; &nbsp; &nbsp; include /etc/nginx/conf.d/*.conf;
&nbsp; &nbsp; &nbsp; &nbsp; include /etc/nginx/sites-enabled/*;
}


Então digite o comando ' sudo service nginx restart ', para reiniciar o serviço do nginx.

8- instalando ruby

Digite '&nbsp;sudo apt-get install ruby ruby-dev -y '



9- Instalando mysql-server

Digite '&nbsp;sudo apt-get install mysql-server mysql-client libmysqlclient-dev&nbsp;-y&nbsp;'. Durante a instalação ele ira pedir a senha do usuário root, eu sempre uso " root ", mais fica a sua escolha.



10- Instalando phpmyadmin

Digite '&nbsp;sudo apt-get install php5-fpm php5-cli php5-mysql -y ', logo apos digite

sudo add-apt-repository ppa:nijel/phpmyadmin
sudo apt-get update
sudo apt-get install phpmyadmin&nbsp;
, durante a instalação ele vai pergunta qual servidor você vai usar basta aperta 'Enter', e novamente enter para proxima pergunta, e logo em seguida ele perguntara qual a senha do admin do banco de dados, e a senha de acesso, digite root em todas elas.



Para configurar o phpmyadmin no nginx basta colar isso em '&nbsp;sudo nano /etc/nginx/sites-enabled/default ' :
OBS: Se vc for usar o phpmyadmin no mesmo servidor das aplicações altere a porta adicionando essa linha, &nbsp;listen 81;,&nbsp;abaixo do "server { ",&nbsp;você&nbsp;pode escolher qualquer porta no&nbsp;meu caso uso 81.





server {

&nbsp; &nbsp; &nbsp; &nbsp; # Make site accessible from http://localhost/
&nbsp; &nbsp; &nbsp; &nbsp; # listen 81;
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; server_name localhost;



&nbsp; &nbsp; &nbsp; &nbsp; location / {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #### Esta linha voce pode alterar para onde esta seu site ou pagina padrao, no meu caso coloquei para redirecionar para o google quando acessar a url root&nbsp;####

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rewrite &nbsp;^/(.*)$ &nbsp;http://www.google.com.br/ &nbsp;permanent;

&nbsp; &nbsp; &nbsp; &nbsp; }

######## phpmyadmin #######



&nbsp; &nbsp; &nbsp; &nbsp; location /phpmyadmin {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#### Essas 2 linhas de&nbsp;código&nbsp;e para pedir&nbsp;usuário&nbsp;e senha antes de entrar no&nbsp;domínio veja mais em htpasswd+nginx ( instale&nbsp;apt-get install apache2-utils, apos instalado digite o comando para gerar o usuario e a senha&nbsp;htpasswd -c&nbsp;&nbsp;/home/ubuntu/.htpasswd&nbsp;nomedousuario) &nbsp;####

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;auth_basic "Acesso Restrito";

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;auth_basic_user_file /home/ubuntu/.htpasswd;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #################################################

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;root /usr/share/;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;index index.php index.html index.htm;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;location ~ ^/phpmyadmin/(.+\.php)$ {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try_files $uri =404;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;root /usr/share/;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fastcgi_pass 127.0.0.1:9000;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fastcgi_index index.php;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;include /etc/nginx/fastcgi_params;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;root /usr/share/;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; location /phpMyAdmin {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rewrite ^/* /phpmyadmin last;

&nbsp; &nbsp; &nbsp; &nbsp; }


}




Então digite o comando '&nbsp;sudo service nginx restart&nbsp;', para reiniciar o serviço do nginx.



Depois faça a seguinte alteração no arquivo '&nbsp;sudo nano /etc/php5/fpm/pool.d/www.conf &nbsp;'


listen = 127.0.0.1:9000
;listen = /tmp/php-fpm.sock

Então digite o comando '&nbsp;sudo service&nbsp;php5-fpm&nbsp;restart&nbsp;', para reiniciar o serviço do php.


Pronto, se tudo estiver certo seu eservidor esta pronto para seus projetos em rails ou para php. &nbsp;=)