My開発メモ

Ubuntuのapache2でユーザーフォルダを設置する

Ubuntuのapache2にユーザーフォルダを設置したい。

apache2での設定

$ sudo a2enmod userdir

ユーザーフォルダでphpの実行を可能にする

$ sudo vi /etc/apache2/mods-available/php7.4.conf

php7.4.conf

# Running PHP scripts in user directories is disabled by default
# 
# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
#<IfModule mod_userdir.c>
#    <Directory /home/*/public_html>
#        php_admin_flag engine Off              <=== ココ
#    </Directory>
#</IfModule>

この部分をコメントアウトする。

補足・PHPのバージョンが上がった (2024/10/20)

今まで使ってたPHPのバージョンが 8.1 で、今回 Ubuntu のバージョンアップにともない、PHP も 8.3 になったので、そのあたりのことも補足しておく。

$ cd /etc/apache2
$ sudo a2dismod php8.1
$ sudo a2enmod php8.3
$ sudo vi mods-available/php8.3.conf
php8.3.conf
# Running PHP scripts in user directories is disabled by default
# 
# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
# <IfModule mod_userdir.c>
#     <Directory /home/*/public_html>
#         php_admin_flag engine Off
#     </Directory>
# </IfModule>
$ sudo systemctl restart apache2

ユーザーの追加・管理

雛形をつくる

まず、ユーザーフォルダの雛形をつくる。

/etc/skel にて、作業。

$ sudo mkdir public_html
$ cd public_html
$ sudo touch index.html
$ sudo touch style.css

/etc/skel/index.html

<!doctype html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <title>Edit This</title>
    <link rel="stylesheet" href="style.css">
  
<script type="text/javascript" charset="UTF-8" src="//cache1.value-domain.com/xrea_header.js" async="async"></script>

<script type="text/javascript" charset="UTF-8" src="//cache1.value-domain.com/xrea_header.js" async="async"></script>

<script type="text/javascript" charset="UTF-8" src="//cache1.value-domain.com/xrea_header.js" async="async"></script>
</head>
  <body>
    <div id="wrap">
      <h1>My Page</h1>
      <p>This is My Page!</p>
      <footer>
        <small>© 2018 Your Name</small>
      </footer>
    </div>
  </body>
</html>

/etc/skel/style.css

@charset "utf-8";

body {
  background-color: orange;
}
#wrap {
  background-color: #fff;
  width: 90%;
  margin: 0 auto;
  height: 100vh;
}

@media screen and (min-width: 1024px) {
  width: 800px;
}

ユーザーの追加

$ sudo adduser <username>

パスワードとか聞かれるから、適宜入力していく。

自動的にpublic_htmlが作成される。

追記

このユーザーをftp専用ユーザーにするには、以下を参照のこと。

カテゴリー: Apache, memo, Ubuntu

タグ: Apache, ubuntu, userdir

カウント: 254