Build Lamp environment for Ubuntu (Linux+apache+mysql+php)

Test environment:

System:				Youqilin 20.04
 Network:				127.0.0.1
 Domain name:				onlylinux.cn
 Site root:			/home/hollowman/web
apache: 			apache2.4.41
mysql-server: 		mysql-server8.0
php:				php7.4.3

1, Apache 2 build

1 installation

$ sudo apt-get install apache2  #install
$ sudo apache2 -v			#View Apache 2 version
$ systemctl status apache2		#View startup status
$ netstat -nltp				#View port listening

2 profile introduction

1) About configuration file structure

After ubuntu installs Apache 2, its configuration files are mainly placed in the following directory structure.

#	/etc/apache2/
#	|-- apache2.conf 		 Master profile, which combines its own configuration with other profiles.
#	| 	`--   ports.conf 		 The listening port configuration file is always included in the apache2.conf file.
#	|-- mods-enabled 		 The activated module configuration directory is activated by symbolic link to the mods available directory file
#	|	|-- *.load
#	|	`-- *.conf
#	|-- conf-enabled  		 The activated global configuration directory is activated by symbolic linking the conf available directory file
#	|	`-- *.conf
# 	`-- sites-enabled 		 The activated virtual host configuration directory is activated by symbolic link to the sites available directory file
#	 	`-- *.conf

2) Main configuration file apache2.conf

$ cat /etc/apache2/apache2.conf | grep -v "^#" | grep -v '^$'

DefaultRuntimeDir ${APACHE_RUN_DIR}
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load  	#Reference dynamic module configuration
IncludeOptional mods-enabled/*.conf		#Reference dynamic module configuration
Include ports.conf						#Reference port listening file
<Directory />
	Options FollowSymLinks
	AllowOverride None
	Require all denied
</Directory>
<Directory /usr/share>
	AllowOverride None
	Require all granted
</Directory>
<Directory /var/www/>
	Options Indexes FollowSymLinks
	AllowOverride None
	Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
	Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf		#Reference global common configuration
IncludeOptional sites-enabled/*.conf	#Reference virtual host configuration

3 website configuration

1) Modify profile (common)

If you need to configure the virtual host, open the configuration file of the virtual host: / etc / apache2 / sites enabled / 000-default.conf for configuration, and restart the service with systemctl restart apache2.

sudo vim /etc/apache2/sites-available/000-default.conf 

<VirtualHost *:80>
	ServerName onlylinux.cn
	DocumentRoot /home/hollowman/web
	<Directory /home/hollowman/web/>
		Options Indexes FollowSymLinks
		AllowOverride None
		Require all granted
	</Directory>
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Note: to test successfully, you need to modify the / etc/hosts file and add a line of code: 127.0.0.1 onlylinux.cn

2) Activate the profile.

Generally, the activated basic configuration files are included after the initial installation. Only the configuration files dynamically added later may need to be activated. Use ln -s, such as:

$ ln -s ../mods-available/ssl.conf ./mods-enabled/ssl.conf
$ ln -s ../mods-available/ssl.load ./mods-enabled/ssl.load

3) Assistant command

It can be managed through assistant commands such as a2enmod/a2dismod, a2ensite/a2disconf and a2enconf/a2disconf. For example:

$ sudo a2enmod rewrite		#Modify the AllowOverride attribute under the directory where the website is located in apache2.conf to all, and then restart apache2

2, mysql

1 installation

$ sudo apt install mysql-server

2 initialize the database

The initially installed database will have a root user by default and has no password. Initialization and password setting are required.

$ sudo mysql_secure_installation

Select the password setting rule [0,1,2] according to the prompt, enter the password that meets the rule, and select y or n according to the prompt. I disabled root remote access during initialization, and then I want to add a user and password.

3 create users for remote access

1) Enter mysql with root user

$ sudo mysql -u root -p

2) Create hollowman user

mysql>create user 'hollowman'@'%' identified by 'Your password';

The function of this statement is to create a new user data in the mysql.user table, where the user name is hollowman and the plugin is caching by default_ sha2_ Password, the permission is basically N, that is, whether or not.

mysql> select user,host,plugin from mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| hollowman        | %         | caching_sha2_password |
| debian-sys-maint | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | auth_socket           |
+------------------+-----------+-----------------------+
6 rows in set (0.00 sec)

3) Empower hollowman users

Because the newly created user permission is basically no, you need to give the user permission

mysql> grant all on *.* to 'hollowman'@'%';
Query OK, 0 rows affected (0.02 sec)

All indicates all permissions (insert,update,delete, etc.), and *. * indicates any table in any database.

4. Modify the configuration file

The MySQL service configuration file installed by Ubuntu 20.04 through apt was originally / etc/mysql/my.cnf. However, the file does not contain actual configuration data, but two connection files are added to the file, of which / etc/mysql/mysql.conf.d/mysqld.cnf is the real server configuration file.

The initially installed database configuration file does not allow remote access and needs to be configured

$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

[mysqld]
user		= mysql
pid-file	= /var/run/mysqld/mysqld.pid      # Remove the comments before this line
socket	= /var/run/mysqld/mysqld.sock      # Remove the comments before this line
port		= 3306          # Remove the comments before this line
datadir	= /var/lib/mysql         # Remove the comments before this line
#bind-address		= 127.0.0.1    #Comment this line
#mysqlx-bind-address	= 127.0.0.1    #Comment this line
key_buffer_size		= 16M
myisam-recover-options  = BACKUP
log_error = /var/log/mysql/error.log
max_binlog_size   = 100M

3, Install PHP and important support libraries

$ sudo apt install php   		#Install php
$ sudo apt install php7.4-gd	#Install the PHP GD library, which is used by many plug-ins
$ sudo apt install php7.4-mysql	#Install PHP MySQL extension
$ sudo apt install php-curl		#Install PHP curl extension
$ sudo apt install php-mbstring #Install PHP mbstring extension

So far, the Apache 2 service has been restarted and the environment has been built successfully.

4, Build a website

A simple and fast website template typecho , the effects after construction are as follows:

Tags: PHP Linux MySQL Apache Ubuntu

Posted on Sun, 07 Nov 2021 19:12:32 -0500 by Nuser