Nginx / Apache Servidores Web
Diferenças de versão 26.10
26.10 usa OpenSSL 4. Migre a configuração legada do ENGINE para provedores, especialmente para integração PKCS#11/HSM. Valide a configuração e as cadeias de certificados antes de recarregar os serviços; a instalação do pacote por si só não estabelece compatibilidade. Notas de lançamento oficiais
Este artigo aborda a instalação e configuração dos dois servidores web mais populares em Ubuntu 26.04: Nginx e Apache.
Nginx
Instalando Nginx
sudo apt update
sudo apt install nginx -y
# Start and enable on boot
sudo systemctl enable --now nginx
# Check status
sudo systemctl status nginx
# Check version
nginx -vConfiguração de firewall
# Allow HTTP and HTTPS
sudo ufw allow 'Nginx Full'
# Or allow individually
sudo ufw allow 80/tcp
sudo ufw allow 443/tcpEstrutura de diretório
/etc/nginx/
├── nginx.conf # Main configuration file
├── sites-available/ # Available site configurations
├── sites-enabled/ # Enabled sites (symlinks)
├── conf.d/ # Additional configuration snippets
├── snippets/ # Reusable configuration snippets
└── modules-enabled/ # Enabled modules
/var/www/ # Website root directory
/var/log/nginx/ # Log directoryConfiguração de host virtual
# Create website directory
sudo mkdir -p /var/www/example.com/html
sudo chown -R www-data:www-data /var/www/example.com
echo "<h1>Welcome to example.com</h1>" | sudo tee /var/www/example.com/html/index.html
# Create virtual host configuration
sudo tee /etc/nginx/sites-available/example.com << 'EOF'
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
# Logs
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
# Static asset caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
# Deny access to hidden files
location ~ /\. {
deny all;
}
}
EOF
# Enable the site
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
# Remove default site (optional)
sudo rm /etc/nginx/sites-enabled/default
# Test configuration
sudo nginx -t
# Reload
sudo systemctl reload nginxConfiguração de proxy reverso
sudo tee /etc/nginx/sites-available/app-proxy << 'EOF'
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
EOF
sudo ln -s /etc/nginx/sites-available/app-proxy /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginxAjuste de desempenho Nginx
# /etc/nginx/nginx.conf key settings
sudo tee /etc/nginx/conf.d/performance.conf << 'EOF'
# Gzip compression
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_min_length 256;
gzip_types
text/plain
text/css
text/xml
text/javascript
application/json
application/javascript
application/xml
application/rss+xml
image/svg+xml;
# File caching
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
# Connection optimization
keepalive_timeout 65;
keepalive_requests 100;
EOF
sudo nginx -t && sudo systemctl reload nginxApache
Instalando Apache
sudo apt update
sudo apt install apache2 -y
# Start and enable on boot
sudo systemctl enable --now apache2
# Check status
sudo systemctl status apache2
# Check version
apache2 -vConfiguração de firewall
sudo ufw allow 'Apache Full'Estrutura de diretório
/etc/apache2/
├── apache2.conf # Main configuration file
├── ports.conf # Listening ports
├── sites-available/ # Available site configurations
├── sites-enabled/ # Enabled sites
├── mods-available/ # Available modules
├── mods-enabled/ # Enabled modules
└── conf-available/ # Additional configuration
/var/www/ # Website root directory
/var/log/apache2/ # Log directoryGerenciamento de módulo comum
# Enable modules
sudo a2enmod rewrite # URL rewriting
sudo a2enmod ssl # SSL/TLS
sudo a2enmod headers # HTTP header control
sudo a2enmod proxy # Reverse proxy
sudo a2enmod proxy_http # HTTP proxy
sudo a2enmod proxy_wstunnel # WebSocket proxy
# Disable a module
sudo a2dismod autoindex
# Restart to apply changes
sudo systemctl restart apache2Configuração de host virtual
# Create website directory
sudo mkdir -p /var/www/example.com/html
sudo chown -R www-data:www-data /var/www/example.com
echo "<h1>Welcome to example.com</h1>" | sudo tee /var/www/example.com/html/index.html
# Create virtual host configuration
sudo tee /etc/apache2/sites-available/example.com.conf << 'EOF'
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin admin@example.com
DocumentRoot /var/www/example.com/html
<Directory /var/www/example.com/html>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# Logs
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
EOF
# Enable the site
sudo a2ensite example.com.conf
# Disable the default site (optional)
sudo a2dissite 000-default.conf
# Test configuration
sudo apache2ctl configtest
# Reload
sudo systemctl reload apache2Configuração de proxy reverso
sudo a2enmod proxy proxy_http
sudo tee /etc/apache2/sites-available/app-proxy.conf << 'EOF'
<VirtualHost *:80>
ServerName app.example.com
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
# WebSocket proxy
RewriteEngine On
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) ws://127.0.0.1:3000/$1 [P,L]
ErrorLog ${APACHE_LOG_DIR}/app-proxy-error.log
CustomLog ${APACHE_LOG_DIR}/app-proxy-access.log combined
</VirtualHost>
EOF
sudo a2ensite app-proxy.conf
sudo apache2ctl configtest && sudo systemctl reload apache2Configuração do certificado SSL
Usando Let’s Encrypt (Certbot)
# Install Certbot
sudo apt install certbot -y
# Nginx plugin
sudo apt install python3-certbot-nginx -y
# Apache plugin
sudo apt install python3-certbot-apache -yConfiguração SSL Nginx
# Automatically obtain and configure certificates
sudo certbot --nginx -d example.com -d www.example.com
# Test automatic renewal
sudo certbot renew --dry-run
# View certificate information
sudo certbot certificatesO Certbot modificará automaticamente a configuração Nginx para:
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
# ... other configuration
}
# HTTP to HTTPS redirect
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}Configuração SSL Apache
# Enable the SSL module
sudo a2enmod ssl
# Automatically obtain and configure certificates
sudo certbot --apache -d example.com -d www.example.comConfiguração SSL manual (Nginx)
sudo tee /etc/nginx/snippets/ssl-params.conf << 'EOF'
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
# HSTS
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
# OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 223.5.5.5 8.8.8.8 valid=300s;
EOFRenovação Automática
# Certbot automatically configures a systemd timer during installation
sudo systemctl status certbot.timer
# Manual renewal
sudo certbot renew
# View the renewal schedule
sudo systemctl list-timers | grep certbotNginx vs Apache: Escolhendo o caminho certo
| Cenário | Recomendação | Razão |
|---|---|---|
| Arquivos estáticos de alta simultaneidade | Nginx | Baixo uso de memória orientado a eventos |
| Requisitos .htaccess | Apache | Suporte nativo para configuração em nível de diretório |
| Proxy reverso/balanceamento de carga | Nginx | Configuração limpa, excelente desempenho |
| PHP (PHP-FPM) | Qualquer | Ambos suportam FastCGI; recomendado em 26.04 |
| Gateway de microsserviços | Nginx | Mais adequado para cenários de gateway de API |
Warning
Em Ubuntu 26.04, o serviço Apache 2.4.66 ativa MemoryDenyWriteExecute=yes por padrão, o que faz com que libapache2-mod-php trave. A recomendação oficial é usar PHP-FPM (mod_proxy_fcgi + php-fpm) em vez do mod_php incorporado.
Comandos de gerenciamento comuns
# Nginx
sudo nginx -t # Test configuration
sudo systemctl reload nginx # Graceful reload
sudo systemctl restart nginx # Restart
sudo tail -f /var/log/nginx/access.log # View access log
# Apache
sudo apache2ctl configtest # Test configuration
sudo systemctl reload apache2 # Graceful reload
sudo systemctl restart apache2 # Restart
sudo a2ensite site.conf # Enable site
sudo a2dissite site.conf # Disable site
sudo a2enmod module # Enable module
sudo a2dismod module # Disable module
sudo tail -f /var/log/apache2/access.log # View access logArtigos relacionados
- Docker Orquestração de serviço do Compose — Implante serviços da Web usando contêineres do Compose Docker
- SSH Noções básicas — SSH conexões remotas e gerenciamento de chaves, habilidades essenciais para administração de servidores
- Firewall UFW — Configure regras de firewall para servidores web, abra portas HTTP/HTTPS