Updating the package manager cache and install Apache:
sudo apt update
sudo apt install apache2
Once the installation is finished, you’ll need to adjust your firewall settings to allow HTTP traffic. To list all currently available UFW application profiles, you can run:
Here’s what each of these profiles mean:
- Apache: This profile opens only port 80 (normal, unencrypted web traffic).
- Apache Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic).
- Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic).
For now, it’s best to allow only connections on port 80, since this is a fresh Apache installation and you still don’t have a TLS/SSL certificate configured to allow for HTTPS traffic on your server:
sudo ufw allow in "Apache"
Or
sudo ufw allow in "WWW Full" (Debian)
You can verify the change with:
sudo ufw status
Traffic on port 80 is now allowed through the firewall. You can do a spot check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser
If you see this page, then your web server is now correctly installed and accessible through your firewall.
You can change the apache port in Ubuntu if you have the same port in the system as follows: sudo nano /etc/apache2/ports.conf
Change port 80 to port 81 or whatever port number you want
If you have configured virtual host for your website (e.g www.mysite.com) at /etc/apache2/sites-enabled/mysite.conf then you can open that file instead.
sudo nano /etc/apache2/sites-enabled/mysite.conf
Otherwise, open the default virtual host configuration file at sudo nano /etc/apache2/sites-enabled/000-default.conf
. You find the following line <VirtualHost: *:80>
and change to <VirtualHost: *:81>
Restart Apache Server to apply changes:
sudo systemctl restart apache2
sudo service apache2 restart
Now that you have a web server up and running, you need to install the database system to be able to store and manage data for your site: sudo apt install mysql-server
When the installation is finished, it’s recommended that you run a security script that comes pre-installed with MySQL. This script will remove some insecure default settings and lock down access to your database system:
sudo mysql_secure_installation
.
To solve the above error, we do the following:
- Run command:
sudo mysql
which logged me in asroot
without a password - Enter command:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'mynewpassword';
Run the command again: sudo mysql_secure_installation
sudo mysql_secure_installation
[sudo] password for nguyen:
Securing the MySQL server deployment.
Enter password for user root:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.
Estimated strength of the password: 50
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
Run the following command to install PHP: sudo apt install php libapache2-mod-php php-mysql
Once the installation is finished, you can run the following command to confirm your PHP version:
php -v
sudo nano /var/www/html/info.php
Save the file and exit. Then run apache server to test the file