Ricerca nel sito web

Come installare Magento con Apache e Crittografiamo SSL su Ubuntu 22.04


Magento è una piattaforma open source e una delle più popolari che aiuta i programmatori a creare siti Web di e-commerce. È scritto in PHP, consentendoti di creare un sistema di carrello della spesa flessibile. È completamente personalizzabile e ti consentirà di sviluppare e lanciare un negozio online completamente funzionale in pochi minuti. Magento offre una community gratuita ed una versione commerciale, la versione community è gratuita ed è pensata per le piccole imprese, mentre la versione commerciale è pensata per le aziende medio-grandi.

Questo tutorial ti mostrerà come installare Magento su Ubuntu 22.04.

Prerequisiti

  • Un server che esegue Ubuntu 22.04.
  • Un nome di dominio valido indicato con l'IP del tuo server.
  • Sul server è configurata una password di root.

Installa Apache, MySQL e PHP

Innanzitutto, installa il server web Apache e il server database MySQL utilizzando il seguente comando:

apt install apache2 mysql-server -y

Magento supporta solo le versioni PHP dalla 7.3 alla 7.4. Tuttavia, la versione 7.4 di PHP non è inclusa nel repository predefinito di Ubuntu 22.04 poiché fornisce PHP 8.1 come versione predefinita. Quindi dovrai installare PHP 7.4 dal repository PHP Ondrej.

Innanzitutto, installa le dipendenze richieste utilizzando il seguente comando:

apt install software-properties-common -y

Successivamente, aggiungi il repository PHP Ondrej utilizzando il seguente comando:

add-apt-repository ppa:ondrej/php

Una volta aggiunto il repository, aggiorna il repository e installa PHP con le altre dipendenze richieste utilizzando il seguente comando:

apt update -y
apt install php7.4 libapache2-mod-php7.4 php7.4-common php7.4-gmp php7.4-curl php7.4-soap php7.4-bcmath php7.4-intl php7.4-mbstring php7.4-xmlrpc php7.4-mysql php7.4-gd php7.4-xml php7.4-cli php7.4-zip -y

Successivamente, modifica il file di configurazione PHP e modifica le impostazioni predefinite:

nano /etc/php/7.4/apache2/php.ini

Modificare i seguenti valori:

file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 512M
upload_max_filesize = 128M
max_execution_time = 3600

Salva e chiudi il file, quindi riavvia il servizio Apache per applicare le modifiche alla configurazione:

systemctl restart apache2

Una volta terminato, puoi procedere al passaggio successivo.

Crea un database per Magento

Magento utilizza MySQL come backend del database, quindi dovrai creare un database e un utente per Magento.

Innanzitutto, accedi alla shell MySQL con il seguente comando:

mysql

Una volta connesso a MySQL, crea un utente e un database per Magento utilizzando il seguente comando:

mysql> CREATE DATABASE magento2;
mysql> CREATE USER 'magento2'@'localhost' IDENTIFIED BY 'password';

Successivamente, concedi tutti i privilegi al database Magento2:

mysql> GRANT ALL PRIVILEGES ON magento2.* TO 'magento2'@'localhost';

Successivamente, svuota i privilegi ed esci da MySQL con il seguente comando:

mysql> FLUSH PRIVILEGES;
mysql> EXIT;

Installa Compositore

Composer è un gestore delle dipendenze per PHP utilizzato per installare le dipendenze PHP richieste per il tuo progetto PHP.

Innanzitutto, installa lo strumento da riga di comando Curl utilizzando il seguente comando:

apt-get install curl -y

Successivamente, installa il compositore utilizzando il seguente comando:

curl -sS https://getcomposer.org/installer -o composer-setup.php
php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Una volta installato il compositore, otterrai il seguente output:

All settings correct for using Composer
Downloading...

Composer (version 2.3.5) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

Puoi controllare la versione del Composer usando il seguente comando:

composer --version

Otterrai il seguente output:

Composer version 2.3.5 2022-04-13 16:43:00

Scarica e installa Magento su Ubuntu 22.04

In questa sezione scaricheremo la versione 2.4.3 di Magento utilizzando il Composer. Per fare ciò, dovrai creare una chiave di accesso dopo aver effettuato l'accesso al sito web di Magento. https://marketplace.magento.com/customer/accessKeys/. Una volta creata la chiave di accesso, esegui il seguente comando per scaricare Magento sul tuo server:

cd /var/www/html
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.3 magento2

Ti verrà chiesto di fornire la tua chiave di accesso come mostrato di seguito per scaricare Magento:

Creating a "magento/project-community-edition=2.4.3" project at "./magento2"
Warning from repo.magento.com: You haven't provided your Magento authentication keys. For instructions, visit https://devdocs.magento.com/guides/v2.3/install-gde/prereq/connect-auth.html
    Authentication required (repo.magento.com):
      Username: a6b333ad41629bac913eaabb9b8e053c
      Password: 
Do you want to store credentials for repo.magento.com in /root/.config/composer/auth.json ? [Yn] Y
Installing magento/project-community-edition (2.4.3)
  - Downloading magento/project-community-edition (2.4.3)
  - Installing magento/project-community-edition (2.4.3): Extracting archive
Created project in /var/www/html/magento2
Loading composer repositories with package information
Info from https://repo.packagist.org: #StandWithUkraine

Una volta scaricato Magento, imposta la proprietà e l'autorizzazione corrette per la directory Magento:

chown -R www-data:www-data /var/www/html/magento2/
cd /var/www/html/magento2
find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
chown -R :www-data . 
chmod u+x bin/magento

Successivamente, esegui il comando seguente per installare Magento sul tuo server:

bin/magento setup:install --base-url=http://magento.example.com --db-host=localhost --db-name=magento2 --db-user=magento2 --db-password=password --admin-firstname=Hitesh --admin-lastname=Jethva [email  --admin-user=admin --admin-password=Magento@Secure1Password --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1

Otterrai il seguente errore:

  Could not validate a connection to Elasticsearch. No alive nodes found in your cluster  

È possibile risolvere questo errore utilizzando il comando seguente:

php bin/magento module:disable {Magento_Elasticsearch,Magento_InventoryElasticsearch,Magento_Elasticsearch6,Magento_Elasticsearch7}

Dovresti vedere il seguente output:

The following modules have been disabled:
- Magento_Elasticsearch
- Magento_Elasticsearch6
- Magento_InventoryElasticsearch
- Magento_Elasticsearch7

Cache cleared successfully.
Generated classes cleared successfully. Please run the 'setup:di:compile' command to generate classes.
Info: Some modules might require static view files to be cleared. To do this, run 'module:disable' with the --clear-static-content option to clear them.

Dovrai inoltre eseguire il seguente comando per disabilitare l'autenticazione a due fattori:

sudo -u www-data bin/magento module:disable Magento_TwoFactorAuth

Otterrai il seguente output:

The following modules have been disabled:
- Magento_TwoFactorAuth

Cache cleared successfully.
Generated classes cleared successfully. Please run the 'setup:di:compile' command to generate classes.
Info: Some modules might require static view files to be cleared. To do this, run 'module:disable' with the --clear-static-content option to clear them.

Ora esegui nuovamente il comando di installazione di Magento per avviare l'installazione:

bin/magento setup:install --base-url=http://magento.example.com --db-host=localhost --db-name=magento2 --db-user=magento2 --db-password=password --admin-firstname=Hitesh --admin-lastname=Jethva [email  --admin-user=admin --admin-password=Magento@Secure1Password --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1

Una volta completata l'installazione, otterrai il seguente output:

[Progress: 1359 / 1366]
Module 'Yotpo_Yotpo':
[Progress: 1360 / 1366]
Enabling caches:
Current status:
layout: 1
block_html: 1
full_page: 1
[Progress: 1361 / 1366]
Installing admin user...
[Progress: 1362 / 1366]
Caches clearing:
Cache cleared successfully
[Progress: 1363 / 1366]
Disabling Maintenance Mode:
[Progress: 1364 / 1366]
Post installation file permissions check...
For security, remove write permissions from these directories: '/var/www/html/magento2/app/etc'
[Progress: 1365 / 1366]
Write installation date...
[Progress: 1366 / 1366]
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin_1u9o0y
Nothing to import.

Ora, svuota tutta la cache e crea un cron job Magento utilizzando i seguenti comandi:

sudo -u www-data bin/magento cache:flush
sudo -u www-data bin/magento cron:install

Una volta terminato, puoi procedere al passaggio successivo.

Configura Apache per Magento

Successivamente, dovrai creare un file di configurazione dell'host virtuale Apache per servire Magento sul web.

nano /etc/apache2/sites-available/magento2.conf

Aggiungi le seguenti righe:

<VirtualHost *:80>
     ServerAdmin [email 
      DocumentRoot /var/www/html/magento2/
     ServerName magento.example.com

     <Directory /var/www/html/magento2/>
          Options FollowSymlinks
          AllowOverride All
          Require all granted
     </Directory>

     ErrorLog ${APACHE_LOG_DIR}/magento2_error.log
     CustomLog ${APACHE_LOG_DIR}/magento2_access.log combined

</VirtualHost>

Salva e chiudi il file quindi attiva l'host virtuale Magento e il modulo di riscrittura Apache utilizzando il seguente comando:

a2ensite magento2.conf
a2enmod rewrite

Successivamente, riavvia il servizio Apache per applicare le modifiche:

systemctl restart apache2

Puoi anche controllare lo stato di Apache con il seguente comando:

systemctl status apache2

Dovresti vedere il seguente output:

? apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-04-29 09:03:19 UTC; 2s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 39790 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 39794 (apache2)
      Tasks: 6 (limit: 4630)
     Memory: 14.1M
        CPU: 132ms
     CGroup: /system.slice/apache2.service
             ??39794 /usr/sbin/apache2 -k start
             ??39795 /usr/sbin/apache2 -k start
             ??39796 /usr/sbin/apache2 -k start
             ??39797 /usr/sbin/apache2 -k start
             ??39798 /usr/sbin/apache2 -k start
             ??39799 /usr/sbin/apache2 -k start

Apr 29 09:03:19 ubuntu systemd[1]: Starting The Apache HTTP Server...

Accedi all'interfaccia web di Magento

Ora apri il tuo browser web e accedi alla dashboard di Magento utilizzando l'URL http://magento.example.com/admin_1u9o0y. Verrai reindirizzato alla pagina di accesso di Magento:

Fornisci il nome utente e la password dell'amministratore e fai clic sul pulsante Accedi. Dovresti vedere la dashboard di Magento nella seguente schermata:

Proteggi Magento con Let's Encrypt SSL

È sempre una buona idea proteggere il tuo sito web con Let's Encrypt SSL. Sarà necessario installare il client Certbot per installare e gestire SSL. Puoi installarlo con il seguente comando:

apt-get install python3-certbot-apache -y

Una volta installato Certbot, esegui il comando seguente per proteggere il tuo sito web con Let's Encrypt SSL:

certbot --apache -d magento.example.com

Ti verrà chiesto di fornire la tua email e accettare i termini di servizio come mostrato di seguito:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Plugins selected: Authenticator apache, Installer apache
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for magento.example.com
Enabled Apache rewrite module
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/magento-le-ssl.conf
Enabled Apache socache_shmcb module
Enabled Apache ssl module
Deploying Certificate to VirtualHost /etc/apache2/sites-available/magento-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/magento-le-ssl.conf

Successivamente, seleziona se reindirizzare o meno il traffico HTTP su HTTPS come mostrato di seguito:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

Digita 2 e premi Invio per installare Let's Encrypt SSL per il tuo sito web:

Enabled Apache rewrite module
Redirecting vhost in /etc/apache2/sites-enabled/magento.conf to ssl vhost in /etc/apache2/sites-available/magento-le-ssl.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://magento.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=magento.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/magento.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/magento.example.com/privkey.pem
   Your cert will expire on 2023-01-23. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

Ora puoi accedere al tuo sito web in modo sicuro utilizzando l'URL https://magento.example.com.

Conclusione

Congratulazioni! hai installato con successo Magento con Apache su Ubuntu 22.04. Spero che questo post ti aiuti ad avviare il tuo sistema di carrello degli acquisti online. Sentiti libero di chiedermi se hai qualche domanda.

Articoli correlati: