Chapter 8: Configuring Nginx and Apache
Friends, in Chapter 6 we installed Nginx and Apache, and in Chapter 7 we hosted our first sites by copy-pasting a server block. That was enough to get started, but in real projects you will be asked to change things – move the website to another folder, run it on another port, host three sites on one server, redirect www or hide the version number. Mauli Dairy, Pune (a fictional business, only for learning) now wants an admin panel and a blog along with the order site. In this chapter we open the config files and understand them line by line. Once you can read a config file comfortably, you will handle web servers with confidence. Come on, let's start!
मित्रांनो, Chapter 6 मध्ये आपण Nginx आणि Apache इन्स्टॉल केले, आणि Chapter 7 मध्ये server ब्लॉक कॉपी-पेस्ट करून पहिल्या साइट्स होस्ट केल्या. सुरुवातीला इतकं पुरेसं होतं, पण खऱ्या प्रोजेक्ट्समध्ये तुम्हाला गोष्टी बदलायला सांगतात – वेबसाइट दुसऱ्या फोल्डरमध्ये न्यायची, दुसऱ्या पोर्टवर चालवायची, एकाच सर्व्हरवर तीन साइट्स, www redirect किंवा व्हर्जन नंबर लपवायचा. Mauli Dairy, Pune (काल्पनिक बिझनेस, फक्त शिकण्यासाठी) ला आता ऑर्डर साइटसोबत एक ॲडमिन पॅनल आणि ब्लॉग पण पाहिजे. या चॅप्टरमध्ये कॉन्फिग फाइल्स उघडून लाइन बाय लाइन समजून घेऊ. कॉन्फिग फाइल आरामात वाचता आली की वेब सर्व्हर्स कॉन्फिडन्सने हँडल कराल. चला, सुरू करूया!
दोस्तों, Chapter 6 में हमने Nginx और Apache इंस्टॉल किए, और Chapter 7 में server ब्लॉक कॉपी-पेस्ट करके पहली साइट्स होस्ट कीं. शुरुआत के लिए इतना काफ़ी था, लेकिन असली प्रोजेक्ट्स में आपसे चीज़ें बदलने को कहा जाता है – वेबसाइट दूसरे फ़ोल्डर में ले जाना, दूसरे पोर्ट पर चलाना, एक ही सर्वर पर तीन साइट्स, www redirect या वर्ज़न नंबर छुपाना. Mauli Dairy, Pune (काल्पनिक बिज़नेस, सिर्फ़ सीखने के लिए) को अब ऑर्डर साइट के साथ एक एडमिन पैनल और ब्लॉग भी चाहिए. इस चैप्टर में कॉन्फ़िग फ़ाइलें खोलकर लाइन बाय लाइन समझेंगे. कॉन्फ़िग फ़ाइल आराम से पढ़ना आ गया तो वेब सर्वर्स कॉन्फ़िडेंस से हैंडल करोगे. चलो, शुरू करते हैं!
What you will learn in this chapter
- Where each config file lives
- Reading an Nginx server block
- Reading an Apache virtual host
- Changing the document root
- Running a site on another port
- Many sites with virtual hosts
- Redirects, versions, uploads
- The safe change workflow
Browser request: GET /about.html Host: site1.example.com (port 80)
|
+---------------------v----------------------+
| 1. Which listen port? listen 80 / Listen 80
| 2. Which site? server_name / ServerName (Host header)
| 3. Which folder? root / DocumentRoot
| 4. Which file? index / DirectoryIndex, location / <Directory>
+---------------------+----------------------+
|
/var/www/site1/about.html --> 200 OK
Every web server answers these four questions for every request. Once you know where each answer is written, you can configure any site.
Concepts in this chapter
- 8.1Where the configuration lives
- 8.2Reading an Nginx configuration
- 8.3Task: change the Nginx document root
- 8.4Reading an Apache configuration
- 8.5Task: change the Apache DocumentRoot
- 8.6Task: run a site on a different port
- 8.7Multiple sites with name-based virtual hosts
- 8.8Useful everyday configurations
- 8.9The safe change workflow
- 8.10Troubleshooting configuration problems
Review and practice
Practice Tasks
Pandharpur Yatra Tours is a travel business in Pandharpur. Vitthal More looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.
0 / 12
Easy Restart a service after a change
Why: Config changes only apply after the service restarts or reloads.
What to do: Restart the nginx service.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Check if a service is running
Why: When a site is down, first check whether the web server process is running.
What to do: Show the status of the httpd service.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Install a package on Ubuntu
Why: Ubuntu uses apt. Package names differ: on Ubuntu Apache is apache2.
What to do: Install the apache2 package on Ubuntu without being asked to confirm.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Install a package on Amazon Linux
Why: dnf is the package manager on Amazon Linux 2023. -y answers yes for you.
What to do: Install the httpd package on Amazon Linux 2023 without being asked to confirm.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Test a website from the server
Why: curl shows whether the web server answers, even before DNS is ready.
What to do: Request http://localhost and show only the response headers.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Download code from GitHub
Why: git clone copies the whole repository to the server, ready to deploy.
What to do: Clone https://github.com/pandharpuryatra/website.git into the current folder.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Test the nginx config
Why: A test catches typos before a restart takes your site down.
What to do: Check the nginx configuration files for errors.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Start a service now and at boot
Why: enable makes it start after reboot; --now also starts it right away.
What to do: Enable the nginx service and start it immediately, in one command.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Open port 80 to the world
Why: Visitors can reach your website only if the security group allows HTTP.
What to do: Allow inbound TCP port 80 from 0.0.0.0/0 on security group sg-02b64222c4b14c3b2.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium CloudFormation: web security group
Why: Opening only 443 keeps the server safer.
What to do: Template with an AWS::EC2::SecurityGroup that allows TCP 443 from 0.0.0.0/0. Add a GroupDescription.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Fill in each property with the value from the task.
- Click Check answer. Key order does not matter.
Medium Create a security group
Why: A security group is the firewall around your instance.
What to do: Create a security group named pandharpuryatra-web-sg with description "Web servers" in vpc-0c4222e4f1deadbee.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Enable an nginx site on Ubuntu
Why: On Ubuntu, a site is enabled by a symbolic link from sites-available to sites-enabled.
What to do: Create a symbolic link to /etc/nginx/sites-available/pandharpuryatra inside /etc/nginx/sites-enabled/.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.