{"id":38802,"date":"2026-07-08T12:46:32","date_gmt":"2026-07-08T15:46:32","guid":{"rendered":"https:\/\/www.locaweb.com.br\/ajuda\/?post_type=ht_kb&#038;p=38802"},"modified":"2026-07-08T12:46:32","modified_gmt":"2026-07-08T15:46:32","slug":"como-automatizar-o-deploy-de-filas-e-background-workers-do-laravel-no-locaweb-cloud-usando-scripts-userdata","status":"publish","type":"ht_kb","link":"https:\/\/www.locaweb.com.br\/ajuda\/wiki\/como-automatizar-o-deploy-de-filas-e-background-workers-do-laravel-no-locaweb-cloud-usando-scripts-userdata\/","title":{"rendered":"Como automatizar o deploy de filas e Background Workers do Laravel no Locaweb Cloud usando scripts Userdata"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">    \t\t<div class=\"hts-messages hts-messages--info  hts-messages--withtitle hts-messages--withicon \"   >\r\n    \t\t\t<span class=\"hts-messages__title\">Informa\u00e7\u00e3o!<\/span>    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\t<\/span><span style=\"font-weight: 400;\">Neste tutorial, voc\u00ea aprender\u00e1 a automatizar o provisionamento de inst\u00e2ncias focadas em processamento em segundo plano utilizando o recurso de Userdata do Apache CloudStack no Locaweb Cloud. Configuraremos a instala\u00e7\u00e3o automatizada do PHP, Redis e Supervisor para manter seus Background Workers rodando de forma persistente e resiliente.<\/span><span style=\"font-weight: 400;\">    \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t<\/span><\/p>\n<p><span style=\"font-weight: 400;\">O processamento ass\u00edncrono \u00e9 um pilar fundamental em arquiteturas de software modernas. Tarefas pesadas, como envio de emails em massa, gera\u00e7\u00e3o de relat\u00f3rios complexos ou processamento de imagens, n\u00e3o devem bloquear a requisi\u00e7\u00e3o HTTP do usu\u00e1rio final. No ecossistema Laravel, esse desafio \u00e9 resolvido atrav\u00e9s da funcionalidade de Queues (Filas) e Background Workers.<\/span><\/p>\n<h2><b>1. Pr\u00e9-requisitos<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Para que a sua estrutura seja implementada com sucesso, valide os seguintes pr\u00e9-requisitos antes de avan\u00e7ar para o painel:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Conta ativa no <\/span><a href=\"https:\/\/painel-cloud.locaweb.com.br\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">painel de controle<\/span><\/a><span style=\"font-weight: 400;\"> do Locaweb Cloud (Apache CloudStack).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Chave privada SSH (<\/span><span style=\"font-weight: 400;\">.key<\/span><span style=\"font-weight: 400;\"> ou <\/span><span style=\"font-weight: 400;\">.pem<\/span><span style=\"font-weight: 400;\">) salva na sua m\u00e1quina local com permiss\u00f5es restritas.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Par de chaves SSH cadastrado no painel da Locaweb para vincula\u00e7\u00e3o \u00e0 nova inst\u00e2ncia.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Reposit\u00f3rio Git da sua aplica\u00e7\u00e3o Laravel acess\u00edvel (p\u00fablico ou com chave de deploy configurada).<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">[Importante!]<\/span><span style=\"font-weight: 400;\"> Uma VM focada exclusivamente em Background Workers n\u00e3o serve tr\u00e1fego web p\u00fablico. O \u00fanico acesso externo necess\u00e1rio ser\u00e1 o SSH (porta 22). A porta do seu <\/span><b>banco de dados<\/b><span style=\"font-weight: 400;\"> (MySQL, porta 3306) nunca deve ser exposta na internet; ela ser\u00e1 acessada estritamente dentro da nossa rede privada.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Est\u00e1 com d\u00favidas sobre como come\u00e7ar no Locaweb Cloud? Acesse nosso <\/span><a href=\"https:\/\/www.locaweb.com.br\/ajuda\/wiki\/por-onde-comecar-no-locaweb-cloud\/\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">tutorial de apoio<\/span><\/a><span style=\"font-weight: 400;\">.\u00a0<\/span><\/p>\n<h2><b>2. Configura\u00e7\u00e3o de infraestrutura (painel)<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Antes de abrir o terminal, prepare a arquitetura de rede e registre o script de inicializa\u00e7\u00e3o diretamente no painel do Locaweb Cloud.<\/span><\/p>\n<h3><b>Passo 1: Registrar o script Userdata<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Como uma solu\u00e7\u00e3o IaaS pura, a Locaweb n\u00e3o realiza instala\u00e7\u00e3o autom\u00e1tica de frameworks por padr\u00e3o. No entanto, o recurso de <\/span><b>Userdata<\/b><span style=\"font-weight: 400;\"> resolve isso: o hipervisor injeta um script no motor do <\/span><i><span style=\"font-weight: 400;\">Cloud-Init<\/span><\/i><span style=\"font-weight: 400;\"> que roda como root na primeira inicializa\u00e7\u00e3o da m\u00e1quina, automatizando o ambiente.<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">No menu lateral esquerdo do painel, clique em <\/span><b>Computa\u00e7\u00e3o<\/b><span style=\"font-weight: 400;\"> &gt; <\/span><b>Userdata<\/b><span style=\"font-weight: 400;\"> &gt; <\/span><b>Registrar userdata<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Preencha os campos:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>Nome:<\/b> <span style=\"font-weight: 400;\">Setup-Laravel-Worker-Redis<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>Userdata:<\/b><span style=\"font-weight: 400;\"> Deixe o campo pronto para colar o script Shell que detalharemos na pr\u00f3xima se\u00e7\u00e3o.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>Codificado em Base64:<\/b><span style=\"font-weight: 400;\"> Mantenha desmarcado (utilizaremos texto simples).<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3><b>Passo 2: Alocar IP P\u00fablico e Configurar Port Forwarding<\/b><\/h3>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Navegue at\u00e9 <\/span><b>Rede<\/b><span style=\"font-weight: 400;\"> &gt; <\/span><b>IPs P\u00fablicos<\/b><span style=\"font-weight: 400;\"> e clique em <\/span><b>Adquirir novo IP<\/b><span style=\"font-weight: 400;\">. Anote o endere\u00e7o alocado.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Acesse a aba <\/span><b>Encaminhamento de porta<\/b><span style=\"font-weight: 400;\"> (<\/span><i><span style=\"font-weight: 400;\">Port Forwarding<\/span><\/i><span style=\"font-weight: 400;\">) deste IP e crie a regra de acesso administrativo:<\/span><\/li>\n<\/ol>\n<table>\n<tbody>\n<tr>\n<td><b>Protocolo<\/b><\/td>\n<td><b>Porta P\u00fablica<\/b><\/td>\n<td><b>Porta Privada<\/b><\/td>\n<td><b>VM de Destino<\/b><\/td>\n<td><b>Finalidade<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">TCP<\/span><\/td>\n<td><span style=\"font-weight: 400;\">22<\/span><\/td>\n<td><span style=\"font-weight: 400;\">22<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Sua VM de Workers<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Acesso SSH Administrativo<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">    \t\t<div class=\"hts-messages hts-messages--alert  hts-messages--withtitle hts-messages--withicon \"   >\r\n    \t\t\t<span class=\"hts-messages__title\">Aviso!<\/span>    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\t<span style=\"font-weight: 400;\">N\u00e3o crie regras de <\/span><i><span style=\"font-weight: 400;\">Port Forwarding<\/span><\/i><span style=\"font-weight: 400;\"> para a porta 3306 (MySQL). O tr\u00e1fego do banco deve permanecer isolado na rede privada interna para proteger suas informa\u00e7\u00f5es confidenciais.<\/span>    \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t<\/span><\/p>\n<h3><b>Passo 3: Configurar as Regras de Firewall<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Na aba <\/span><b>Firewall<\/b><span style=\"font-weight: 400;\"> do seu IP p\u00fablico, autorize a entrada do tr\u00e1fego correspondente:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>CIDR de Origem<\/b><\/td>\n<td><b>Protocolo<\/b><\/td>\n<td><b>Porta Inicial<\/b><\/td>\n<td><b>Porta Final<\/b><\/td>\n<td><b>Finalidade<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&lt;SEU_IP_LOCAL&gt;\/32<\/span><\/td>\n<td><span style=\"font-weight: 400;\">TCP<\/span><\/td>\n<td><span style=\"font-weight: 400;\">22<\/span><\/td>\n<td><span style=\"font-weight: 400;\">22<\/span><\/td>\n<td><span style=\"font-weight: 400;\">SSH restrito \u00e0 sua rede de TI<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">    \t\t<div class=\"hts-messages hts-messages--alert  hts-messages--withtitle hts-messages--withicon \"   >\r\n    \t\t\t<span class=\"hts-messages__title\">Importante!<\/span>    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\t<\/span><span style=\"font-weight: 400;\">Restringir o CIDR de origem do SSH ao IP fixo da sua empresa reduz drasticamente a superf\u00edcie de ataques por for\u00e7a bruta na porta 22.<\/span><span style=\"font-weight: 400;\">    \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t<\/span><\/p>\n<h2><b>3. Configura\u00e7\u00e3o de sistema (Userdata Script)<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">O script a seguir automatiza a prepara\u00e7\u00e3o do sistema operacional. Copie o c\u00f3digo abaixo, cole-o no campo <\/span><b>Userdata<\/b><span style=\"font-weight: 400;\"> do painel e salve:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Bash<\/p>\n<p><\/span><\/p>\n<pre><span style=\"font-weight: 400;\">#!\/bin\/bash<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># 1. Atualiza\u00e7\u00e3o do \u00edndice de pacotes e do sistema<\/span>\r\n\r\n<span style=\"font-weight: 400;\">apt-get update -y &amp;&amp; apt-get upgrade -y<\/span>\r\n\r\n\r\n\r\n\r\n<span style=\"font-weight: 400;\"># 2. Instala\u00e7\u00e3o de utilit\u00e1rios de sistema e reposit\u00f3rios necess\u00e1rios<\/span>\r\n\r\n<span style=\"font-weight: 400;\">apt-get install -y software-properties-common curl unzip git<\/span>\r\n\r\n\r\n\r\n\r\n<span style=\"font-weight: 400;\"># 3. Instala\u00e7\u00e3o do PHP 8.1 (CLI), Redis e Supervisor<\/span>\r\n\r\n<span style=\"font-weight: 400;\">apt-get install -y \\<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0php8.1-cli php8.1-mbstring php8.1-xml php8.1-bcmath php8.1-curl \\<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0php8.1-redis php8.1-mysql mysql-client redis-server supervisor<\/span>\r\n\r\n\r\n\r\n\r\n<span style=\"font-weight: 400;\"># 4. Instala\u00e7\u00e3o do Composer<\/span>\r\n\r\n<span style=\"font-weight: 400;\">curl -sS https:\/\/getcomposer.org\/installer | php -- \r\n--install-dir=\/usr\/local\/bin --filename=composer<\/span>\r\n\r\n\r\n\r\n\r\n<span style=\"font-weight: 400;\"># 5. Configura\u00e7\u00e3o autom\u00e1tica do Supervisor para gerenciar as filas<\/span>\r\n\r\n<span style=\"font-weight: 400;\">cat &lt;&lt;'EOF' &gt; \/etc\/supervisor\/conf.d\/laravel-worker.conf<\/span>\r\n\r\n<span style=\"font-weight: 400;\">[program:laravel-worker]<\/span>\r\n\r\n<span style=\"font-weight: 400;\">process_name=%(program_name)s_%(process_num)02d<\/span>\r\n\r\n<span style=\"font-weight: 400;\">command=php \/var\/www\/html\/app\/artisan queue:work redis --sleep=3 \r\n--tries=3 --max-time=3600<\/span>\r\n\r\n<span style=\"font-weight: 400;\">autostart=true<\/span>\r\n\r\n<span style=\"font-weight: 400;\">autorestart=true<\/span>\r\n\r\n<span style=\"font-weight: 400;\">stopasgroup=true<\/span>\r\n\r\n<span style=\"font-weight: 400;\">killasgroup=true<\/span>\r\n\r\n<span style=\"font-weight: 400;\">user=www-data<\/span>\r\n\r\n<span style=\"font-weight: 400;\">numprocs=4<\/span>\r\n\r\n<span style=\"font-weight: 400;\">redirect_stderr=true<\/span>\r\n\r\n<span style=\"font-weight: 400;\">stdout_logfile=\/var\/log\/laravel-worker.log<\/span>\r\n\r\n<span style=\"font-weight: 400;\">stopwaitsecs=3600<\/span>\r\n\r\n<span style=\"font-weight: 400;\">EOF<\/span>\r\n\r\n\r\n\r\n\r\n<span style=\"font-weight: 400;\"># 6. Inicializa\u00e7\u00e3o dos servi\u00e7os e recarga do Supervisor<\/span>\r\n\r\n<span style=\"font-weight: 400;\">systemctl enable redis-server &amp;&amp; systemctl start redis-server<\/span>\r\n\r\n<span style=\"font-weight: 400;\">systemctl enable supervisor &amp;&amp; systemctl start supervisor<\/span>\r\n\r\n<span style=\"font-weight: 400;\">supervisorctl reread &amp;&amp; supervisorctl update<\/span>\r\n\r\n\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">\u00a0    \t\t<div class=\"hts-messages hts-messages--alert  hts-messages--withtitle hts-messages--withicon \"   >\r\n    \t\t\t<span class=\"hts-messages__title\">Importante!<\/span>    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\t<span style=\"font-weight: 400;\">O Laravel depende do comando <\/span><span style=\"font-weight: 400;\">php artisan queue:work<\/span><span style=\"font-weight: 400;\"> para processar as filas. O <\/span><b>Supervisor<\/b><span style=\"font-weight: 400;\"> atua como um gerenciador de processos no Linux, garantindo que 4 processos concorrentes (<\/span><span style=\"font-weight: 400;\">numprocs=4<\/span><span style=\"font-weight: 400;\">) rodem continuamente em segundo plano sob o usu\u00e1rio <\/span><span style=\"font-weight: 400;\">www-data<\/span><span style=\"font-weight: 400;\">. Se um worker cair por falta de mem\u00f3ria, ele ser\u00e1 reiniciado automaticamente (<\/span><span style=\"font-weight: 400;\">autorestart=true<\/span><span style=\"font-weight: 400;\">).<\/span>    \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t<\/span><\/p>\n<h2><b>4. Lan\u00e7ando a m\u00e1quina virtual com o Userdata<\/b><\/h2>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">No painel, acesse <\/span><b>Computa\u00e7\u00e3o<\/b><span style=\"font-weight: 400;\"> &gt; <\/span><b>VMs<\/b><span style=\"font-weight: 400;\"> &gt; <\/span><b>Adicionar VM<\/b><span style=\"font-weight: 400;\"> e selecione o template Ubuntu 22.04 LTS.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ative a chave de altern\u00e2ncia <\/span><b>Modo Avan\u00e7ado<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Na se\u00e7\u00e3o <\/span><b>Userdata registrado<\/b><span style=\"font-weight: 400;\">, selecione o script que salvamos (<\/span><span style=\"font-weight: 400;\">Setup-Laravel-Worker-Redis<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Vincule seu par de chaves SSH e clique em <\/span><b>Executar VM<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">\u00a0    \t\t<div class=\"hts-messages hts-messages--info  hts-messages--withtitle hts-messages--withicon \"   >\r\n    \t\t\t<span class=\"hts-messages__title\">Informa\u00e7\u00e3o!<\/span>    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\t<span style=\"font-weight: 400;\">Durante os primeiros 2 a 3 minutos ap\u00f3s a inst\u00e2ncia constar como <\/span><i><span style=\"font-weight: 400;\">Running<\/span><\/i><span style=\"font-weight: 400;\"> (em execu\u00e7\u00e3o), o script de Userdata estar\u00e1 sendo processado de forma invis\u00edvel em segundo plano.<\/span>    \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t<\/span><\/p>\n<h2><b>5. Configura\u00e7\u00e3o do banco de dados: MySQL<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Escolha o cen\u00e1rio de banco de dados que melhor se adapta \u00e0 topologia do seu projeto:<\/span><\/p>\n<h3><b>Cen\u00e1rio A: MySQL na mesma VM (Ambientes Simples)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Conecte-se \u00e0 VM via SSH e instale o banco de dados:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Bash<br \/>\n<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">sudo apt-get install -y mysql-server<\/span>\r\n\r\n<span style=\"font-weight: 400;\">sudo systemctl enable --now mysql<\/span>\r\n\r\n<span style=\"font-weight: 400;\">sudo mysql_secure_installation<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Acesse o console administrativo e crie a base restrita ao <\/span><span style=\"font-weight: 400;\">localhost<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Bash<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">sudo mysql<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">SQL<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CREATE DATABASE laravel_app CHARACTER SET utf8mb4 COLLATE \r\nutf8mb4_unicode_ci;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY \r\n'TroqueEstaSenhaForte123!';<\/span>\r\n\r\n<span style=\"font-weight: 400;\">GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_user'@'localhost';<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FLUSH PRIVILEGES;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">EXIT;<\/span><\/pre>\n<h3><b>Cen\u00e1rio B: MySQL em VM Dedicada (Ambientes VPC)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Na inst\u00e2ncia isolada de banco, configure o arquivo de rede para escutar o IP privado da sub-rede:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Bash<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">sudo nano \/etc\/mysql\/mysql.conf.d\/mysqld.cnf<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># Ajuste o bind-address com o IP privado do banco, \r\nex: bind-address = 10.0.2.10<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Abra o console SQL e restrinja o acesso do usu\u00e1rio \u00e0 sub-rede privada da aplica\u00e7\u00e3o:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">SQL<br \/>\n<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CREATE DATABASE laravel_app CHARACTER SET utf8mb4 COLLATE \r\nutf8mb4_unicode_ci;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">CREATE USER 'laravel_user'@'10.0.1.%' IDENTIFIED BY \r\n'TroqueEstaSenhaForte123!';<\/span>\r\n\r\n<span style=\"font-weight: 400;\">GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_user'@'10.0.1.%';<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FLUSH PRIVILEGES;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">EXIT;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Bash<br \/>\n<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">sudo systemctl restart mysql<\/span><\/pre>\n<h2><b>6. Deploy da aplica\u00e7\u00e3o\u00a0<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">O arquivo <\/span><span style=\"font-weight: 400;\">.env<\/span><span style=\"font-weight: 400;\"> da aplica\u00e7\u00e3o clonada deve ser parametrizado com o driver apontando para o Redis:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ini, TOML<br \/>\n<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">DB_CONNECTION=mysql<\/span>\r\n\r\n<span style=\"font-weight: 400;\">DB_HOST=127.0.0.1\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # Use o IP 10.0.2.10 caso utilize o \r\nCen\u00e1rio B<\/span>\r\n\r\n<span style=\"font-weight: 400;\">DB_PORT=3306<\/span>\r\n\r\n<span style=\"font-weight: 400;\">DB_DATABASE=laravel_app<\/span>\r\n\r\n<span style=\"font-weight: 400;\">DB_USERNAME=laravel_user<\/span>\r\n\r\n<span style=\"font-weight: 400;\">DB_PASSWORD=TroqueEstaSenhaForte123!<\/span>\r\n\r\n\r\n\r\n\r\n<span style=\"font-weight: 400;\">QUEUE_CONNECTION=redis<\/span>\r\n\r\n<span style=\"font-weight: 400;\">REDIS_HOST=127.0.0.1<\/span>\r\n\r\n<span style=\"font-weight: 400;\">REDIS_PORT=6379<\/span><\/pre>\n<h3><b>M\u00e9todo 1: Deploy manual via Git<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Acesse a inst\u00e2ncia via SSH e prepare o diret\u00f3rio do c\u00f3digo-fonte:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Bash<\/p>\n<p><\/span><\/p>\n<pre><span style=\"font-weight: 400;\">sudo mkdir -p \/var\/www\/html<\/span>\r\n\r\n<span style=\"font-weight: 400;\">sudo git clone https:\/\/github.com\/&lt;SUA_ORG&gt;\/&lt;SEU_REPO&gt;.git \r\n\/var\/www\/html\/app<\/span>\r\n\r\n<span style=\"font-weight: 400;\">cd \/var\/www\/html\/app<\/span>\r\n\r\n\r\n\r\n\r\n<span style=\"font-weight: 400;\"># Instala depend\u00eancias de produ\u00e7\u00e3o otimizando o autoloader<\/span>\r\n\r\n<span style=\"font-weight: 400;\">sudo composer install --no-dev --optimize-autoloader<\/span>\r\n\r\n<span style=\"font-weight: 400;\">sudo cp .env.example .env<\/span>\r\n\r\n<span style=\"font-weight: 400;\">sudo php artisan key:generate<\/span>\r\n\r\n<span style=\"font-weight: 400;\">sudo php artisan migrate --force<\/span>\r\n\r\n\r\n\r\n\r\n<span style=\"font-weight: 400;\"># Ajusta a propriedade da pasta para os workers operarem<\/span>\r\n\r\n<span style=\"font-weight: 400;\">sudo chown -R www-data:www-data \/var\/www\/html\/app<\/span>\r\n\r\n\r\n<\/pre>\n<h3><b>M\u00e9todo 2: Deploy automatizado (GitHub Actions)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Voc\u00ea pode configurar uma esteira automatizada no Git utilizando os Secrets de conex\u00e3o SSH (<\/span><span style=\"font-weight: 400;\">SSH_HOST<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">SSH_USER<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">SSH_KEY<\/span><span style=\"font-weight: 400;\">). Salve o arquivo <\/span><span style=\"font-weight: 400;\">.github\/workflows\/deploy.yml<\/span><span style=\"font-weight: 400;\"> para disparar as atualiza\u00e7\u00f5es a cada push:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">YAML<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">name: Deploy Laravel Workers<\/span>\r\n\r\n\r\n\r\n\r\n<span style=\"font-weight: 400;\">on:<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0push:<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0branches: [ main ]<\/span>\r\n\r\n\r\n\r\n\r\n<span style=\"font-weight: 400;\">jobs:<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0deploy:<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0runs-on: ubuntu-latest<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0steps:<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0- name: Conectar via SSH e atualizar a aplica\u00e7\u00e3o<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0uses: appleboy\/ssh-action@v1.0.3<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0with:<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0host: ${{ secrets.SSH_HOST }}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0username: ${{ secrets.SSH_USER }}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0key: ${{ secrets.SSH_KEY }}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0script: |<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cd \/var\/www\/html\/app<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0git pull origin main<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0composer install --no-dev --optimize-autoloader<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0php artisan migrate --force<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0php artisan config:cache<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sudo supervisorctl restart laravel-worker:*<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">    \t\t<div class=\"hts-messages hts-messages--alert  hts-messages--withtitle hts-messages--withicon \"   >\r\n    \t\t\t<span class=\"hts-messages__title\">Aviso!<\/span>    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\t<\/span><span>O comando <\/span><span>queue:work<\/span><span> mant\u00e9m o c\u00f3digo do projeto salvo na mem\u00f3ria RAM. \u00c9 obrigat\u00f3rio executar <\/span><span>supervisorctl restart laravel-worker:*<\/span><span> ao final de cada deploy para que os workers carreguem a nova vers\u00e3o do c\u00f3digo.<\/span><span style=\"font-weight: 400;\">    \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t<\/span><\/p>\n<h2><b>7. Valida\u00e7\u00e3o<\/b><\/h2>\n<h3><b>Testes de comunica\u00e7\u00e3o e servi\u00e7os<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Conecte-se \u00e0 VM por SSH utilizando o terminal local para validar a sa\u00fade dos daemons:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Bash<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">chmod 400 ~\/.ssh\/sua-chave.key<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ssh -i ~\/.ssh\/sua-chave.key root@&lt;SEU_IP_PUBLICO&gt;<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Valide se o Message Broker (Redis) est\u00e1 respondendo perfeitamente na inst\u00e2ncia interna:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Bash<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">systemctl status redis-server<\/span>\r\n\r\n<span style=\"font-weight: 400;\">redis-cli ping<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># Deve retornar \"PONG\"<\/span><\/pre>\n<h3><b>Mapeamento do Supervisor<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Monitore o status operacional das filas gerenciadas pelo Supervisor:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Bash<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">supervisorctl status<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">    \t\t<div class=\"hts-messages hts-messages--alert  hts-messages--withtitle hts-messages--withicon \"   >\r\n    \t\t\t<span class=\"hts-messages__title\">Importante!<\/span>    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\t<span style=\"font-weight: 400;\">Antes de realizar o deploy do c\u00f3digo, o comando acima reportar\u00e1 o status <\/span><span style=\"font-weight: 400;\">FATAL<\/span><span style=\"font-weight: 400;\">, pois o arquivo <\/span><span style=\"font-weight: 400;\">artisan<\/span><span style=\"font-weight: 400;\"> ainda n\u00e3o existir\u00e1 na pasta. Ap\u00f3s clonar o projeto e configurar o <\/span><span style=\"font-weight: 400;\">.env<\/span><span style=\"font-weight: 400;\">, ative os processos executando:<\/span>    \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Bash<\/span><\/p>\n<pre><span style=\"font-weight: 400;\"># Inicializa o grupo de workers cadastrado<\/span>\r\n\r\n<span style=\"font-weight: 400;\">supervisorctl start laravel-worker:*<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Acompanhe o processamento de jobs e o fluxo de tarefas em tempo real consumindo os logs de sa\u00edda do sistema:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Bash<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">tail -f \/var\/log\/laravel-worker.log<\/span><\/pre>\n<h2><b>8. Solu\u00e7\u00e3o de problemas (Troubleshooting)<\/b><\/h2>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Workers permanecem em estado FATAL ap\u00f3s o deploy:<\/b><span style=\"font-weight: 400;\"> Certifique-se de que o arquivo <\/span><span style=\"font-weight: 400;\">\/var\/www\/html\/app\/artisan<\/span><span style=\"font-weight: 400;\"> possui permiss\u00f5es de execu\u00e7\u00e3o e que voc\u00ea rodou o comando <\/span><span style=\"font-weight: 400;\">composer install<\/span><span style=\"font-weight: 400;\"> para baixar os pacotes do framework.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Jobs acumulados na tabela (Tr\u00e1fego n\u00e3o processado):<\/b><span style=\"font-weight: 400;\"> Verifique se a vari\u00e1vel <\/span><span style=\"font-weight: 400;\">QUEUE_CONNECTION=redis<\/span><span style=\"font-weight: 400;\"> est\u00e1 configurada corretamente no seu arquivo <\/span><span style=\"font-weight: 400;\">.env<\/span><span style=\"font-weight: 400;\"> e limpe o cache de configura\u00e7\u00f5es rodando <\/span><span style=\"font-weight: 400;\">php artisan config:clear<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Acesso SSH administrativo recusado por timeout:<\/b><span style=\"font-weight: 400;\"> Acesse o painel CloudStack na aba de <\/span><b>IPs P\u00fablicos<\/b><span style=\"font-weight: 400;\"> e certifique-se de que a porta p\u00fablica 22 est\u00e1 mapeada via <\/span><i><span style=\"font-weight: 400;\">Port Forwarding<\/span><\/i><span style=\"font-weight: 400;\"> e liberada no <\/span><b>Firewall<\/b><span style=\"font-weight: 400;\"> para o seu CIDR local.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">    \t\t<div class=\"hts-messages hts-messages--info  hts-messages--withtitle hts-messages--withicon \"   >\r\n    \t\t\t<span class=\"hts-messages__title\">Pronto!<\/span>    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\tO provisionamento automatizado dos seus Background Workers via Userdata foi conclu\u00eddo com sucesso. Suas filas est\u00e3o rodando de forma est\u00e1vel, segura e resiliente na nuvem.    \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0    \t\t<div class=\"hts-messages hts-messages--info  hts-messages--withtitle hts-messages--withicon \"   >\r\n    \t\t\t<span class=\"hts-messages__title\">Conhe\u00e7a!<\/span>    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\t<span style=\"font-weight: 400;\">A VM de workers opera de forma isolada nos bastidores, mas a sua aplica\u00e7\u00e3o web Laravel que enfileira as tarefas precisa de prote\u00e7\u00e3o na borda. Conhe\u00e7a as solu\u00e7\u00f5es de <\/span><a href=\"https:\/\/www.locaweb.com.br\/ssl-locaweb\/\" target=\"_blank\" rel=\"noopener\"><b>Certificado SSL Locaweb<\/b><\/a><span style=\"font-weight: 400;\"> e ative o protocolo HTTPS via Let\u2019s Encrypt no servidor web principal para blindar a navega\u00e7\u00e3o dos seus usu\u00e1rios com criptografia!<\/span>    \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t<\/span><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>O processamento ass\u00edncrono \u00e9 um pilar fundamental em arquiteturas de software modernas. Tarefas pesadas, como envio de emails em massa, gera\u00e7\u00e3o de relat\u00f3rios complexos ou processamento de imagens, n\u00e3o devem bloquear a requisi\u00e7\u00e3o HTTP do usu\u00e1rio final. No ecossistema Laravel, esse desafio \u00e9 resolvido atrav\u00e9s da funcionalidade de Queues (Filas)&#8230;<\/p>\n","protected":false},"author":55,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"ht-kb-category":[666],"ht-kb-tag":[],"class_list":["post-38802","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-locaweb-cloud"],"_links":{"self":[{"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/ht-kb\/38802","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/users\/55"}],"replies":[{"embeddable":true,"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/comments?post=38802"}],"version-history":[{"count":1,"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/ht-kb\/38802\/revisions"}],"predecessor-version":[{"id":38803,"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/ht-kb\/38802\/revisions\/38803"}],"wp:attachment":[{"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/media?parent=38802"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/ht-kb-category?post=38802"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/ht-kb-tag?post=38802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}