Setting up a web server with Ubuntu Server 22.04.1 LTS

Setting up a web server with Ubuntu Server 22.04.1 LTS.

Initialisation

Connecting via SSH.

If installed from the Setup menu on a VPS, connect as root, add a general user and give sudo permission.

Log out and login via SSH with the added user.

SSH server configuration

Disable root user login.

Firewall configuration

Enable firewall.

Next, add rules for communication permissions on the port for SSH connections with ufw allow.

Check with the ufw status verbose command.

If there are no problems with the added rules, the settings can be reloaded.

Installing ‘Apache2’

Check with your browser.

If the default Ubuntu 22.04 Apache web page is displayed, OK.

Enable mod_rewrite

Virtual host configuration

If you want to assign a hostname that can be used by other users, via FTP or others, configure it in the /var/www directory as public/USER_NAME/DOMAIN_NAME.

*After creating the user, assign ownership of the USER_NAME directory.

If you are the only one using the domain of the virtual host, the following:

It is advisable to set up each web root under /var/www as this provides a certain level of security through AppArmor. If you wish to set it up under /home/{username}, for example, you will need to review the AppArmor configuration separately.

Create a new configuration file in /etc/apache2/sites-available/DOMAIN_NAME.conf.

In the example, create /var/www/vhosts/example.com/logs.

Use the a2ensite tool to enable the DOMAIN_NAME.conf file you have created.

Check for configuration errors.

If you see output as following, restart Apache.

Create index.html in /var/www/vhosts/example.com/httpdocs, and then go to http://example.com from a browser, and if the HTML file you created is displayed, you have succeeded.

E.g. index.html

Get a SSL certificate using Let’s Encrypt

Install certbot python3-certbot-apache

Enable SSL ports on the firewall.

To make Let’s Encrypt generate an SSL, enter the following command.

To automatically detect the relevant parts of the Apache configuration file and create a configuration file for https

Use the configuration file edit /etc/apache2/sites-available/vhost-le-ssl.conf.

For getting a certificate only

The certificate will be placed under /etc/letsencrypt/live/example.com/, specify the path in the SSL conf file.

Renewal

Install MariaDB

Install PHP

To install a version other than installed by default

Add repository.

Install phpMyAdmin

Select apache2 (press spacebar ‘*’), select [Ok] and press [Enter] on the keyboard.

When prompted to confirm database installation, select No & Enter.

The configuration file is /etc/phpmyadmin

Install PHP Composer

If you installed a version of PHP other than the default one.

Install vsftpd

Changes and additions

Create an empty file /etc/vsftpd.chroot_list.

Create /etc/vsftpd/vsftpd_user_conf, create USER_NAME file, describe user root directory.

In /etc/vsftpd, create the ftpuser_list file, a list of users to connect to via FTP.

Switching between multiple versions of PHP for each virtual host in Apache on Ubuntu

Install the Apache module for PHP-FPM FastCGI Process Manager.

Enables a few modules required to configure multiple PHP versions with Apache. These modules are necessary to integrate PHP FPM and FastCGI with the Apache server.

Use PHP FPM and FastCGI to use multiple PHP versions. Install the following packages on your system.
(e.g. PHP 7.4)

php7.4-fpm must be installed.

After installation, the php-fpm service is automatically started. Using the following commands, make sure that services are running.

Apache virtual host configuration

After all changes have been made, restart Apache and reload the new configuration changes.

Create a file named info.php with the following content

in the document root configured with PHP 7.4.

Open a web browser and access the site: php74.example.com should show PHP 7.4 and FastCGI configured.

PHP settings using FPM/FastCGI you can find the php.ini file under the path /etc/php/{version}/fpm/php.ini like /etc/php/7.4/fpm/php.ini.

Running PHP with different user

Normally, to run WordPress with a modular version of PHP, you specify the username and group as www-data, which is the user/group under which Apache runs, but if you want to run it as different user, you can use the mpm-itk module or use FastCGI.

This time, the steps are to use FastCGI.

Configuration of PHP-FPM

Use the following command to copy www.conf in /etc/php/7.4/fpm/pool.d and create user1.conf.

Rewrite the following in the file.

Restart PHP-FPM.

A socket file php7.4-fpm-user1.sock is created in /var/run/php.

Add the following to the Apache configuration file.

Restart Apache.

You can now run PHP as a different user.

コメント

タイトルとURLをコピーしました