Как запустить мой html файл с помощью apache2
Возможно, это не правильный канал, чтобы задать этот вопрос, но я не знаю, где спросить.
Я хочу знать, как запустить html файл с apache на моем ubuntu. Я написал программу с javascript и html файлом, и я хочу запустить его через apache. Я не мог найти папку htdocs, чтобы скопировать мои файлы там, кроме того, у меня нет разрешения на это в ubuntu. Мой apache установлен в /usr/share/apache2. Когда я пишу http://localhost он говорит:
It works! This is the default web page for this server. The web server software is running but no content has been added, yet.
но я не знаю, где добавить содержимое.
Папка по умолчанию, в которой он служит для файлов HTML, – это /var/www .
Например, /var/www/test.html будет http://localhost/test.html .
В моем случае, когда я установил и запустил Apache 2, и если я набрал в браузере localhost/ , откроется страница Apache 2 Ubuntu по умолчанию.
Теперь то, что я сделал, было:
Предположим, ваша HTML-страница Demo.html находится в каталоге веб-сайта, затем скопируйте HTML-страницу с помощью одной из команд:
:~$ cp ~/Website/Demo.html /var/www/html
Если эта команда не работает, попробуйте выполнить следующую команду:
:~$ sudo cp ~/Website/Demo.html /var/www/html
А затем откройте браузер и введите следующую строку поиска браузера:
локальный /demo.html
Откроется скопированный файл Demo.html.
Также, если вы хотите получить доступ к этой размещенной веб-странице с внешней стороны компьютера, на котором работает Apache 2, используйте его внешний IP-адрес вместо localhost следующим образом:
How to open a file in a browser?
To use your public IP address (i.e. your IP address on the WAN) you’ll probably need some kind of DNS service also, to take care of the possible disconnections / reconnections of your server from your ISP (unless you have a static IP). Do you just want to access the file locally or over the internet?
@kos, Yes, over the internet. I’m connecting my Linux from Windows 8.1 using PuTTY. But want to access index.html on Linux over the internet.
@DKBose: look at the answers first (excellent) and then edit out the off-topic part before VTC. (done already)
3 Answers 3
If you want a quick and dirty one-line command, use python SimpleHTTPServer
python -m SimpleHTTPServer
How to use it example for your case:
$ cd /var/www $ python -m SimpleHTTPServer
Thats it! This will be serving current directory /var/www . Default port is 8000, so your website will be accessible from http://ip-address:8000 or at your local machine http://localhost:8000
To access other files not named index.html , use their name http://ip-address:8000/other-name.html , if there is no index.html, you will see a directory listing with all the files in your folder.
To change port, you need to have sudo priviliges:
$ sudo python -m SimpleHTTPServer 80
This command will be serving your directory on port 80, if you have apache2 already installed, you can change this port to 81, so it does not have a port conflict and you will access your website from http://ip-address:81 , don’t forget to allow these ports in your firewall, to allow port 80 use:
No webserver
Another way is not to use any web-server, just install your favorite desktop environment and browser on your server and connect to it with remote desktop of your choice. Then your website will be available from the browser as you are used to at file:///var/www/index.html
How to add an html page in the apache2 root?
I’ve installed apache2 on ubuntu and I want to add an html page on it’s root to see if it works. How can I do it?
1 Answer 1
The default Apache2 Document Root directory on Ubuntu is: /var/www/html .
It is defined in the configuration file /etc/apache2/sites-available/000-default.conf . You can enable and disable this default virtual host via these commands:
sudo a2ensite 000-default.conf # which means Apache2 enable site sudo a2dissite 000-default.conf # which means Apache2 disable site
You can create other virtual hosts, that points to other directories. After each of these steps or after some edits in the configuration files you must reload (or restart) Apache2:
sudo systemctl reload apache2.service sudo systemctl restart apache2.service
By default, the directory /var/www/html is owned by root. This means when you want to edit a file in this directory you have to use sudo command.
For example. There is a file that contains the default welcome page. This file is called /var/www/html/index.php . To edit it, open a terminal window ( ctrl + alt + T ) and use this command:
sudo -i gedit /var/www/html/index.html
It is not good practice, but for testing purposes (while using the web server only locally) you can change the owner of this directory and files in it. Use this command:
sudo chown -R $USER /var/www/html/
After that you will be able to edit and create files there, with your current user (try the command echo $USER ).
About the permissions of this folder, please read this topic: How to avoid using sudo when working in /var/www?
How to upload HTML web pages on Apache2 web server
I have created some HTML files which I want to upload to the web. I am currently running the Apache2 server with my IP address. This is what I see in the browser when I enter my IP address: I am able to access my HTML files using the web browser, but I want to be able to upload them to the web with ordinary web addresses so users can see them. I hope someone can guide me in the right direction.
I am able to open my HTML files using the web browser, by opening the files in the browser from my file manager. How would you suggest I set up the apache?
1 Answer 1
When you type in your IP address or localhost or 127.0.0.1 in a URL of any web browser, it always searches for an index.php or an index.html to open. If it doesn’t exist, it just shows files on that folder.
What you see here is your screenshot is the index.html file located in the directory /var/www/html/
I believe you’re hosting a website on your local system. Since you’re running Apache2 as your Web Server. All your HTML files are needed to be placed within the directory /var/www/html/.
All you need to do the following:
- Delete or rename that index.html in that directory. (You’ll probably have to do it in sudo mode through terminal, else just give yourself access to read/write/execute all files within the html directory)
- Now place your desired HTML file which is to be your «Homepage» and make sure it is named as index.html or index.php
- That’s it, you’re good to go !
Now enter your IP address in your Browser URL, and you can see your HTML file.
- Make sure your Apache2 is running before you can view anything on your browser. If youre not sure, just type this command in the Terminal: sudo /etc/init.d/apache2 restart
- Make sure you, (the user) has read/write/execute privileges on all files and folders in the /var/www/html folder.