Ubuntu self-built and installed Nginx+MySQL+PHP environment

Posted: May 20, 2024

The process of self-building and installing a server environment such as Nginx, MySQL, and PHP on Ubuntu can be seen as a necessary skill for web developers. By compiling and installing these tools, users can have full control over versions and configurations, which is paramount to securing and optimizing the server. This article provides a comprehensive setup guide for building and installing Nginx, MySQL, and PHP on an Ubuntu server.

Step 1: Preliminary system setup

Before we start the process of building and installing the Nginx, MySQL, and PHP environment, it's crucial to ensure the system is up to date. Ordinary system operations involve updating software repositories and upgrading the existing software packages. This guarantees that all system dependencies are met and prevents possible installation roadblocks.

To update and upgrade the system, open your terminal and enter the following commands: sudo apt update and sudo apt upgrade. The apt (Advanced Package Tool) is the packaging tool used by Ubuntu, which manages packages and ensures that the latest versions of the required software are installed on your system.

Step 2: Installing Nginx

Assuming your system updates are complete, the first step of our LEMP stack setup (Linux, Nginx, MySQL, and PHP) is to install Nginx. Nginx is a high performance, open-source HTTP server and reverse proxy. First, use command 'sudo apt install nginx' in terminal to install Nginx. After installation, start the Nginx service with 'sudo service nginx start'.

Make sure the Nginx is running by opening a web browser and entering 'http://localhost' or 'http://your_server_ip'. If your Nginx installation was successful, you will see the Nginx welcome page.

Step 3: Installing MySQL

After confirming Nginx is running, we proceed to install MySQL. MySQL is an open-source relational database management system essential in managing data for your web applications. To install it, run 'sudo apt install mysql-server'. After installation, secure your MySQL installation by running 'sudo mysql_secure_installation' and follow the prompts as they appear.

Upon completion, make sure your MySQL service is running by typing 'systemctl status mysql.service'. If MySQL is running, terminal will display 'active (running)'.

Step 4: Installing PHP

The next step in our LEMP setup is to install PHP. PHP is a server-side scripting language designed for web development. To install PHP alongside the necessary PHP extensions, run 'sudo apt install php-fpm php-mysql' in the terminal.

By installing php-fpm (FastCGI Process Manager), you allow Nginx to process PHP files. After the installation, run 'systemctl status php7.2-fpm' to verify the PHP installation. If PHP is running successfully, terminal will display 'active (running)'.

Step 5: Configuring Nginx to use PHP

Once PHP is installed, you need to instruct Nginx to use it. To do this, you need to modify the Nginx server block configuration file to tell Nginx to process PHP files. Open the server block configuration file by running 'sudo nano /etc/nginx/sites-available/default' and make necessary changes as prescribed in the configuration guide.

After modifying, save and close the file, and test the configuration to ensure there are no syntax errors. Restart Nginx after confirming the configuration with 'sudo systemctl restart nginx'.

Step 6: Testing PHP with Nginx

The final step of our LEMP stack setup guide is to test PHP with Nginx. Create a new .php file in your Nginx document root directory and add the PHP code to it. Save and close the file, and open the file in your web browser using the URL 'http://localhost/info.php' or 'http://your_server_ip/info.php'.

If the PHP works correctly, you should see a web page displaying detailed information about the PHP version installed, current configuration settings, and more.

Please read this disclaimer carefully before you start to use the service. By using the service, you acknowledge that you have agreed to and accepted the content of this disclaimer in full. You may choose not to use the service if you do not agree to this disclaimer. This document is automatically generated based on public content on the Internet captured by Machine Learning Platform for AI. The copyright of the information in this document, such as web pages, images, and data, belongs to their respective author and publisher. Such automatically generated content does not reflect the views or opinions of Alibaba Cloud. It is your responsibility to determine the legality, accuracy, authenticity, practicality, and completeness of the content. We recommend that you consult a professional if you have any doubt in this regard. Alibaba Cloud accepts no responsibility for any consequences on account of your use of the content without verification. If you have feedback or you find that this document uses some content in which you have rights and interests, please contact us through this link: https://www.alibabacloud.com/campaign/contact-us-feedback. We will handle the matter according to relevant regulations.
phone Contact Us