MDN wants to learn about developers like you: https://qsurvey.mozilla.com/s3/MDN-survey

Перевод не завершен. Пожалуйста, помогите перевести эту статью с английского.

Эта статья объясняет как установить простой локальный тестовый сервер на Вашем компьютере, а так же основы его использования.

Требования: Сначала Вам необходимо изучить как работает интернет, а также что такое веб-сервер.
Цель: Вы научитесь установке локального тестового сервера.

Локальные и удаленные файлы

На протяжении всего обучения Вы будете открывать примеры непосредственно в браузере — двойным кликом по HTML файлу, перетаскиванием файла в окно браузера, или через меню File > Open... и указывая необходимый HTML файл. Существует множество способов как это сделать.

If the web address path starts with file:// followed by the path to the file on your local hard drive, a local file is being used. In contrast, if you view one of our examples hosted on GitHub (or an example on some other remote server), the web address will start with http:// or https://, to show that the file has been received via HTTP.

Проблемы тестирования локальных файлов

Some examples won't run if you open them as local files. This can be due to a variety of reasons, the most likely being:

  • They feature asynchronous requests. Some browsers (including Chrome) will not run async requests (see Fetching data from the server) if you just run the example from a local file. This is because of security restrictions (for more on web security, read Website security).
  • They feature a server-side language. Server-side languages (such as PHP or Python) require a special server to interpret the code and deliver the results.

Запуск простого локального HTTP сервера

To get around the problem of async requests, we need to test such examples by running them through a local web server. One of the easiest ways to do this for our purposes is to use Python's SimpleHTTPServer module.

To do this:

  1. Install Python. If you are using Linux or Mac OS X, it should be available on your system already. If you are a Windows user, you can get an installer from the Python homepage and follow the instructions to install it:

    • Go to python.org
    • Under the Download section, click the link for Python "3.xxx".
    • At the bottom of the page, choose the Windows x86 executable installer and download it.
    • When it has downloaded, run it.
    • On the first installer page, make sure you check the "Add Python 3.xxx to PATH" checkbox.
    • Click Install, then click Close when the installation has finished.
  2. Open your command prompt (Windows)/terminal (OS X/Linux). To check Python is installed, enter the following command:

    python -V
  3. This should return a version number. If this is OK, navigate to the directory that your example is inside, using the cd command.

    # include the directory name to enter it, for example
    cd Desktop
    # use two dots to jump up one directory level if you need to
    cd ..
  4. Enter the command to start up the server in that directory:

    # If Python version returned above is 3.X
    python -m http.server
    # If Python version returned above is 2.X
    python -m SimpleHTTPServer
  5. By default, this will run the contents of the directory on a local web server, on port 8000. You can go to this server by going to the URL localhost:8000 in your web browser. Here you'll see the contents of the directory listed — click the HTML file you want to run.

Note: If you already have something running on port 8000, you can choose another port by running the server command followed by an alternative port number, e.g. python -m http.server 7800 (Python 3.x) or python -m SimpleHTTPServer 7800 (Python 2.x). You can then access your content at localhost:7800.

Running server-side languages locally

Python's SimpleHTTPServer module is useful, but it doesn't know how to run code written in languages such as PHP or Python. To handle that you'll need something more — exactly what you'll need depends on the server-side language you are trying to run. Here are a few examples:

  • To run Python server-side code, you'll need to use a Python web framework. You can find out how to use the Django framework by reading Django Web Framework (Python). Flask is also a good (slightly less heavyweight) alternative to Django. To run this you'll need to install Python/PIP, then install Flask using pip3 install flask. At this point you should be able to run the Python Flask examples using for example python3 python-example.py, then navigating to localhost:5000 in your browser.
  • To run Node.js (JavaScript) server-side code, you'll need to use raw node or a framework built on top of it. Express is a good choice — see Express Web Framework (Node.js/JavaScript).
  • To run PHP server-side code, you'll need a server setup that can interpret PHP. Good options for local PHP testing are MAMP (Mac and Windows) , AMPPS (Mac, Windows, Linux) and LAMP (Linux, Apache, MySQL, and PHP/Python/Perl). These are complete packages that create local setups to allow you to run the Apache server, PHP, and MySQL databases.

Метки документа и участники

 Внесли вклад в эту страницу: teedree
 Обновлялась последний раз: teedree,