Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. PHPBeginner - Setting Up a PHP Environment with MAMP

Setting Up a PHP Environment with MAMP - Images: Japanese

Hey there, everyone!

Next up, let's go through installing MAMP and getting everything set up. MAMP works pretty much the same way on both Windows and macOS, so the installation process is similar regardless of which OS you're on.

Head over to the link below to get started.

https://www.mamp.info/en/

When you land on that page, you should see a screen like the one below. Click the DOWNLOAD button indicated by the red arrow. There's a paid version and a free version — the free version is perfectly fine to start with. That said, if you plan to use it heavily, consider picking up the paid version. Building this kind of software is no easy feat.

On the next page, you'll see another 'Download' button — go ahead and click that. The download will begin. It should automatically take you to the right page for your OS (Windows or macOS), so you don't need to manually select your platform.

One thing to note: MAMP is a fairly large download (around 250MB). If you're currently on a mobile hotspot or tethering, you might want to wait until you're on home Wi-Fi or a wired connection.

Once the download is done, just double-click the installer and follow the prompts to complete the installation.

After that, you should see the following icon on your desktop or in your Applications folder — it's an adorable little mammoth.

That's MAMP! Go ahead and double-click it to open it. You'll see a screen like the one below. This screenshot is from macOS, but Windows should look similar.

Now click the arrow indicated in the image below to open the settings panel.

In the settings, go to the 'Ports' tab and set the 'Apache Port' to '80'. This is where you specify the 'port number' used for development. It doesn't have to be '80' — if something else on your system is already using port 80, just enter a different number.

Keep in mind that if you use a port number other than '80', you'll need to append ':number' to the domain (or IP address) in the URL. For example, to connect to 'http://localhost/test/' using port '8888', you'd type 'http://localhost:8888/test/'. By the way, the default port number for browsing the web is '80', so when connecting on port 80, you don't need to specify it — just 'http://localhost' is enough.

A 'port number' is a number included in a data packet (the unit of data sent and received over TCP/IP — i.e., the internet) that tells the receiving machine which program should handle the incoming data.

Port numbers are made up of a 16-bit unsigned integer, so they range from 0 to 65535.

By using port numbers, a server can do things like: if an incoming request uses port 80, respond with website data; if it uses port 110, hand it off to an email processing program — and so on.

One thing to be aware of: some port numbers are already in use by common applications, so you can't freely pick any number you like.

When setting up a port for development, it's a good idea to search for a 'port number list' and avoid numbers that are already assigned to well-known services.

For PHP development, port numbers like 8080 or 8888 are commonly used, so if you're not sure what to pick, either of those is a solid choice.

Next, click the Web Server tab at the top, then click Document Root and select the folder (directory) where you placed index.php in the previous article — the one you want to use for PHP development.

For example, if you created a folder called 'test' on your desktop and placed 'index.php' there, select that 'test' folder on the desktop. Also, make sure the 'Web Server' option above it is set to 'Apache'.

The 'Document Root' (DocumentRoot) is the 'base directory' for your web server. You can change the document root location in the settings of web server software like 'Apache'.

You may recall from relative path notation that a path starting with / — like /test.html — is relative to a fixed base. The document root is where that / points.

For example, on our server, we've configured it so that: "When someone accesses 'wp-p.info', the document root is '/var/www/htdocs/wp-p/'."

That means you can write just '/' in an anchor's href and it becomes a link to '/var/www/htdocs/wp-p/index.php' — the top page of the site.

<a href="/">Top Page</a>

Similarly, if you want to display the image at /var/www/htdocs/wp-p/test.jpg, you can just write /test.jpg like this:

<img src="/test.jpg" alt="This is a test!">

When building dynamic websites with PHP, using paths that start with / makes it much easier to migrate servers down the line, which is why this approach is commonly used.

If this is a bit confusing, check out this article from the HTML beginner's course — it should help clear things up.

Once you've finished the settings, let's start up MAMP. Click 'Start Servers' indicated by the red arrow in the image below.

When the server starts successfully, the indicator shown by the red arrow in the image below will light up green — you'll know right away. To stop the server, just click 'Stop Servers'.

Alright, we're all set! Let's run the Hello world program we created in the previous article again.

Open any browser and type http://localhost into the address bar. If you're using a port number other than '80', make sure to include it in the URL. For example, if MAMP is set to port '8888', you'd go to 'http://localhost:8888'.

'localhost' refers to your own computer.

When you type http://localhost into your browser's address bar, it connects to the web server running on your local machine. That's just how it's designed to work.

You'll run into the word 'localhost' all the time — not just in PHP and browsers, but in software configuration, networking, and database setup too. It's worth remembering: "whenever I see 'localhost', it means this device itself."

You should see something like this:

Hello world is displayed — exactly as expected. Your PHP Hello world program is complete! Did it work on your machine too?

If something didn't work, double-check the URL, review your MAMP settings, and make sure you've selected the correct folder (directory). MAMP's settings aren't all that complicated, so it should start up without much trouble. Try a few different port numbers if needed — port '80' is the most reliable option. Also, watch out for any typos in your PHP code.

And with that, great work! Starting from the next article, we'll get hands-on with writing PHP. See you there!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .