1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【2025年度版】WSL2のubuntuにapacheとphpをインストールしてバージョン管理

Posted at

この記事はWSL2のubuntuにapacheとphpをインストールしてバージョン管理の2025年度版となります。

はじめに

windowsならLaragon使え

  • 自動SSL
  • php、DB、apache、node、pythonのバージョン管理

[https://laragon.org/]

非商用なら年11ドルだ

それでも行くというのなら

前回の記事の加筆修正版となります。

この記事をもとにphpをインストール

ubuntu20.04.1 phpのバージョンを切り替える

ubuntu24.04でも使える模様。
phpは現状8.1~8.4くらいを入れよう。

sudo apt install -y software-properties-common
sudo add-apt-repository -y ppa:ondrej/php
sudo apt update
sudo apt install -y php8.1 php8.1-fpm php8.1-mbstring php8.1-zip php8.1-mysql php8.1-xml
sudo apt install -y php8.2 php8.2-fpm php8.2-mbstring php8.2-zip php8.2-mysql php8.2-xml
sudo apt install -y php8.3 php8.3-fpm php8.3-mbstring php8.3-zip php8.3-mysql php8.3-xml
sudo apt install -y php8.1 php8.4-fpm php8.4-mbstring php8.4-zip php8.4-mysql php8.4-xml

apacheインストール

sudo apt install -y apache2
sudo apt install -y libapache2-mod-php libapache2-mod-php8.1 libapache2-mod-php8.2 libapache2-mod-php8.3 libapache2-mod-php8.4

apacheのphpのバージョンを入れ替える場合

モジュールを無効化/有効化し、apacheを再起動

sudo a2dismod php8.4
sudo a2enmod php8.2
sudo systemctl restart apache2

ドキュメントルート変更はしない2025

どうもうまく動かなくなることが多い。ので、処理ディレクトリのパーミッション変更を行う。

cd /var/www/html
sudo chown -R [username] /var/www/html

これで、Ubuntuのデフォルトユーザーで/var/www/html内を自由に編集できる。

ssl設定

ssl設定はドメインを取ってcertbotを使おう?

どうもwslではうまくいかない模様

ローカル環境をSSL化する

default-ssl.conf 編集

/etc/apache2/sites-available/default-ssl.conf をviなりnanoなりで編集

SSLCertificateFile	/etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

SSLCertificateFile	/etc/ssl/myssl.crt
SSLCertificateKeyFile /etc/ssl/myssl.key

とかに変更。

openssl-san

XAMPP for WindowsでSSLを有効にするを参考に

/etc/ssl/内でopenssl.cnfをコピーしてopenssl-san.cnfを作成、

openssl-san.cnf
[ SAN ]
subjectAltName = @alt_names

[ alt_names ]
DNS.1 = localhost
DNS.2 = <他に認証したいドメインがあれば>
DNS.3 = <他に認証したいドメインがあれば>

と追加する。

opensslコマンド実行

これも/etc/ssl/内で

$ sudo openssl req \
    -newkey     rsa:4096 \
    -keyout     myssl.key \
    -x509 \
    -nodes \
    -out        myssl.crt \
    -subj       "/CN=localhost" \
    -reqexts    SAN \
    -extensions SAN \
    -config     openssl-san.cnf \
    -days       365000

エクスプローラからwindowsに証明書をインストールする。

apache再起動

sudo a2dissite default-ssl.conf
sudo a2ensite default-ssl.conf

のあと

sudo service apache2 restart

OKだ。

image.png

結論

laragon使おうぜ?

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?