1. Use Apache mod_expires caching
When a user visits your website, their browser will first check to see if it has a cached copy of the static content. If the browser has a cached copy of the content, it will display the cached copy instead of downloading the content from the server. This can save a lot of time and bandwidth, especially for users who are visiting your website for the first time.
- Enable the mod_expires module in Apache. You can do this by adding the following line to your Apache configuration file:
LoadModule expires_module modules/mod_expires.so
Here is an example of an Apache configuration file that uses mod_expires caching:
2. Use opcode cache
What is opcode cache?
Opcode cache is a mechanism used to improve the performance of PHP applications by storing the compiled opcode of PHP scripts in memory. This means that the PHP interpreter does not have to parse and compile the scripts every time they are requested, which can significantly improve the load time of PHP applications.
How to use opcode cache in LAMPP ?
To use opcode cache in LAMPP, you need to first enable the PHP OPcache extension. This can be done by adding the following line to the
Got to below path and uncommend below code
Once the extension is enabled, you need to restart the LAMPP server. You can do this by running the following command:
sudo /opt/lampp/lampp restart
3. Optimize your database queries
Database queries can have a significant impact on the performance of web applications. It is important to optimize your database queries to reduce the time it takes to retrieve data from the database.
Here is an example of an unoptimized database query:
SELECT id, username FROM users WHERE username = 'Amit Kumar' LIMIT 1;
This query will return all of the rows in the
users table where the
username column is equal to
johndoe. However, this is not very efficient, because it is likely that you only need to return a single row.
4. Enable Firewall
What is Firewall and Allow Necessary Ports ?
Enabling a firewall and allowing necessary ports are security measures to control and regulate network traffic to and from your computer or server. Firewalls act as a barrier between your system and the internet or other networks, helping to prevent unauthorized access and securing your system.
Why We need to do this Firewall Ports ?
Enabling a firewall and managing open ports is a crucial aspect of securing your computer or server. Here’s why managing firewall ports is important:
- Unauthorized Access Prevention: Firewalls help prevent unauthorized access to your system by blocking incoming connections that are not explicitly allowed.
- Network Segmentation: By controlling which ports are open, you can segment your network and limit exposure to potential security threats.
- Protection Against Exploits:
- Exploit Prevention: Firewalls protect your system from potential exploits and attacks by controlling which services and ports are accessible.
- Controlled Access to Services:
- Service Restriction: You can control which services are accessible from the network. For example, you may allow access to web services (HTTP and HTTPS) but restrict access to certain administrative services.
Security thread if i ignore this Firewall and Allow Necessary Ports ?
If you ignore your firewall and allow necessary ports, you could be putting your system at risk of a number of security threats, including:
- Malware attacks: Malware can exploit open ports to gain access to your system and steal your data, install malware, or launch denial-of-service (DoS) attacks.
- Unauthorized access: Attackers can use open ports to gain unauthorized access to your system and steal your data, or launch attacks against other systems on the network.
- DoS attacks: DoS attacks can overwhelm your system with traffic, making it unavailable to legitimate users.
- Man-in-the-middle attacks: Man-in-the-middle attacks allow attackers to intercept and modify communication between your system and other systems on the network.
sudo ufw allow 80 sudo ufw allow 443 sudo ufw enable
5. Adjust PHP Settings
memory_limit based on your requirements
Go to php.ini and put as below code
memory_limit = 512
Here are some other PHP settings that you may want to optimize for performance:
max_execution_time: This setting specifies the maximum amount of time that PHP scripts can execute before they are terminated. It is recommended to increase this value from the default of 30 seconds to at least 60 seconds, or higher if your application requires more time to execute.
post_max_size: This setting specifies the maximum size of POST requests that PHP will accept. It is recommended to increase this value from the default of 8M to at least 16M, or higher if your application is submitting large POST requests.
upload_max_filesize: This setting specifies the maximum size of uploaded files that PHP will accept. It is recommended to increase this value from the default of 2M to at least 4M, or higher if your application allows users to upload large files.
7. Optimize Session Handling
Session handling is the process of tracking and managing user sessions on a website. A session is a period of time during which a user is actively interacting with a website. Session handling is important because it allows websites to provide personalized experiences to users, such as remembering their login status and shopping cart items.
There are a number of ways to optimize session handling, including:
- Use a database to store session data. This is the most common and reliable way to store session data. It is also the most scalable way to store session data, as it can handle a large number of concurrent users.
- Use a caching mechanism to store session data in memory. This can improve the performance of your website by reducing the number of database queries that need to be made. However, it is important to note that caching session data in memory can be less reliable, as the data can be lost if the server crashes or restarts.
- Use a session timeout. A session timeout is a period of time after which a user’s session will be automatically terminated. This can help to improve security and reduce the load on your server.
return [ 'session' => [ // Use the database to store session data 'driver' => 'database', // Use the Memcached cache driver to store session data in memory 'cache_driver' => 'memcached', // Set the session timeout to 2 hours 'lifetime' => 120, ], ];
8. Enable GZIP compression in .htaccess
If you have setup Apache Virtual Host, you can open the .htaccess file for your website and add the following lines in it. Else you can create a blank .htaccess file under main document root (/var/www/html)
sudo vim /var/www/html/.htaccess
Add the following lines to it
What are the Benefits of using Enable Compression
- Reduced bandwidth usage: GZIP compression can reduce the size of HTTP responses by up to 90%, which can significantly reduce bandwidth usage. This can be especially beneficial for websites with a lot of text-based content, such as blogs and news websites.
- Improved performance: By reducing the size of HTTP responses, GZIP compression can also improve the performance of your website by reducing the time it takes for pages to load. This is because the browser will be able to download the smaller responses more quickly.
We use AddOutputFilterByType directive to tell Apache as to which file types to compress.
Next restart the apache server
sudo /opt/lampp/lampp restart
9. Config Caching
Config caching in Laravel is a way to improve the performance of your application by caching the configuration files. By caching the configuration files, Laravel can avoid having to load them from disk every time they are needed. This can significantly improve the performance of your application, especially if you are using a large number of configuration files.
php artisan config:cache php artisan config:clear
10. Optimise Mysql
Optimizing the database is a very effective performance boost that many of us overlook! You’ll want to analyse the database queries running on your server, and tweak the MySQL performance to match the server needs.
Database performance depends on several factors at the database level, such as tables, queries, and configuration settings. These software constructs result in CPU and I/O operations at the hardware level, which you must minimize and make as efficient as possible.
There are a number of ways to optimize MySQL for performance, including:
- Use the right storage engine. MySQL supports a number of different storage engines, each with its own strengths and weaknesses. For most applications, the InnoDB storage engine is the best choice. InnoDB is a transactional storage engine that provides high performance and reliability.
- Index your tables. Indexes allow MySQL to quickly find the data that it needs. For tables that are frequently queried, it is important to create indexes on the columns that are used in the queries.
- Normalize your database. Database normalization involves organizing your data into a set of tables in a way that reduces redundancy and improves data integrity. Normalizing your database can improve performance by reducing the amount of data that MySQL needs to access.
- Use a caching mechanism. A caching mechanism can store frequently accessed data in memory, which can improve performance by reducing the number of times that MySQL needs to access the database.
- Tune your MySQL configuration. There are a number of MySQL configuration parameters that can be tuned to improve performance. However, it is important to be careful when tuning MySQL configuration parameters, as incorrect changes can lead to performance problems or data corruption.
Create indexes on the columns that are used in frequently executed queries. To create an index on a column, you can use the following SQL statement:
CREATE INDEX index_name ON table_name (column_name);